#377 - Add back support for JsonWriteBeanVisitor

This commit is contained in:
Robin Bygrave committed 2015-08-21 08:20:47 +12:00
1 parent 2caeb4fff1
commit 7c705e2baa
44 files changed
+997 -204

No files matched your search

@@ -286,7 +286,8 @@ public class DJsonContext implements JsonContext {
private WriteJson createWriteJson(JsonGenerator gen, JsonWriteOptions options) {
PathProperties pathProps = (options == null) ? null : options.getPathProperties();
return new WriteJson(server, gen, pathProps, determineObjectMapper(options), determineInclude(options));
Map<String, JsonWriteBeanVisitor<?>> visitors = (options == null) ? null : options.getVisitorMap();
return new WriteJson(server, gen, pathProps, visitors, determineObjectMapper(options), determineInclude(options));
}
private <T> void toJsonFromCollection(Collection<T> collection, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException {
@@ -3,10 +3,12 @@ package com.avaje.ebeaninternal.server.text.json;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.config.JsonConfig;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebean.text.json.JsonIOException;
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
import com.avaje.ebean.text.json.JsonWriter;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import com.avaje.ebeaninternal.server.type.JsonWriter;
import com.avaje.ebeaninternal.server.util.ArrayStack;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -21,11 +23,13 @@ import java.util.Set;
public class WriteJson implements JsonWriter {
private final SpiEbeanServer server;
private final JsonGenerator generator;
private final PathProperties pathProperties;
private final Map<String, JsonWriteBeanVisitor<?>> visitors;
private final PathStack pathStack;
private final ArrayStack<Object> parentBeans;
@@ -37,10 +41,13 @@ public class WriteJson implements JsonWriter {
/**
* Construct for full bean use (normal).
*/
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties, Object objectMapper, JsonConfig.Include include){
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties,
Map<String, JsonWriteBeanVisitor<?>> visitors, Object objectMapper, JsonConfig.Include include) {
this.server = server;
this.generator = generator;
this.pathProperties = pathProperties;
this.visitors = visitors;
this.objectMapper = objectMapper;
this.include = include;
this.parentBeans = new ArrayStack<Object>();
@@ -50,9 +57,10 @@ public class WriteJson implements JsonWriter {
/**
* Construct for Json scalar use.
*/
public WriteJson(JsonGenerator generator, JsonConfig.Include include){
public WriteJson(JsonGenerator generator, JsonConfig.Include include) {
this.generator = generator;
this.include = include;
this.visitors = null;
this.server = null;
this.pathProperties = null;
this.objectMapper = null;
@@ -74,72 +82,253 @@ public class WriteJson implements JsonWriter {
return include != JsonConfig.Include.NON_EMPTY;
}
@Override
public JsonGenerator gen() {
return generator;
}
@Override
public void writeFieldName(String name) throws IOException {
generator.writeFieldName(name);
}
@Override
public void writeNullField(String name) throws IOException {
if (isIncludeNull()) {
generator.writeNullField(name);
public void writeStartObject(String key) {
try {
if (key != null) {
generator.writeFieldName(key);
}
generator.writeStartObject();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, Long value) throws IOException {
generator.writeNumberField(name, value);
public void writeStartObject() {
try {
generator.writeStartObject();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, Double value) throws IOException {
generator.writeNumberField(name, value);
public void writeEndObject() {
try {
generator.writeEndObject();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, int value) throws IOException {
generator.writeNumberField(name, value);
public void writeStartArray(String key) {
try {
if (key != null) {
generator.writeFieldName(key);
}
generator.writeStartArray();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, Short value) throws IOException {
generator.writeNumberField(name, value);
public void writeStartArray() {
try {
generator.writeStartArray();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeEndArray() {
try {
generator.writeEndArray();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeRaw(String text) {
try {
generator.writeRaw(text);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeRawValue(String text) {
try {
generator.writeRawValue(text);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeFieldName(String name) {
try {
generator.writeFieldName(name);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNullField(String name) {
if (isIncludeNull()) {
try {
generator.writeNullField(name);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
}
@Override
public void writeNumberField(String name, long value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, double value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, int value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, short value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, Float value) throws IOException {
generator.writeNumberField(name, value);
public void writeNumberField(String name, float value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumberField(String name, BigDecimal value) throws IOException {
generator.writeNumberField(name, value);
public void writeNumberField(String name, BigDecimal value) {
try {
generator.writeNumberField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeStringField(String name, String value) throws IOException {
generator.writeStringField(name, value);
public void writeStringField(String name, String value) {
try {
generator.writeStringField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeBinary(InputStream is, int length) throws IOException {
generator.writeBinary(is, length);
public void writeBinary(InputStream is, int length) {
try {
generator.writeBinary(is, length);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeBinaryField(String name, byte[] value) throws IOException {
generator.writeBinaryField(name, value);
public void writeBinaryField(String name, byte[] value) {
try {
generator.writeBinaryField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeBooleanField(String name, Boolean value) throws IOException {
generator.writeBooleanField(name, value);
public void writeBooleanField(String name, boolean value) {
try {
generator.writeBooleanField(name, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeBoolean(boolean value) {
try {
generator.writeBoolean(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeString(String value) {
try {
generator.writeString(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumber(int value) {
try {
generator.writeNumber(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumber(long value) {
try {
generator.writeNumber(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumber(BigDecimal value) {
try {
generator.writeNumber(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNull() {
try {
generator.writeNull();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
public boolean isParentBean(Object bean) {
@@ -164,65 +353,90 @@ public class WriteJson implements JsonWriter {
pathStack.pop();
}
public void beginAssocMany(String key) {
try {
pathStack.pushPathKey(key);
generator.writeFieldName(key);
generator.writeStartArray();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
public void endAssocMany() {
try {
pathStack.pop();
generator.writeEndArray();
} catch (IOException e) {
throw new JsonIOException(e);
}
}
public WriteBean createWriteBean(BeanDescriptor<?> desc, EntityBean bean) {
String path = pathStack.peekWithNull();
JsonWriteBeanVisitor visitor = (visitors == null) ? null : visitors.get(path);
if (pathProperties == null) {
return new WriteBean(desc, bean);
return new WriteBean(desc, bean, visitor);
}
boolean explicitAllProps = false;
Set<String> currentIncludeProps = pathProperties.get(pathStack.peekWithNull());
Set<String> currentIncludeProps = pathProperties.get(path);
if (currentIncludeProps != null) {
explicitAllProps = currentIncludeProps.contains("*");
if (explicitAllProps || currentIncludeProps.isEmpty()) {
currentIncludeProps = null;
}
}
return new WriteBean(desc, explicitAllProps, currentIncludeProps, bean);
return new WriteBean(desc, explicitAllProps, currentIncludeProps, bean, visitor);
}
public void writeValueUsingObjectMapper(String name, Object value) throws IOException {
public void writeValueUsingObjectMapper(String name, Object value) {
if (!isIncludeEmpty()) {
// check for suppression of empty collection or map
if (value instanceof Collection && ((Collection)value).isEmpty()) {
if (value instanceof Collection && ((Collection) value).isEmpty()) {
// suppress empty collection
return;
} else if (value instanceof Map && ((Map)value).isEmpty()) {
} else if (value instanceof Map && ((Map) value).isEmpty()) {
// suppress empty map
return;
}
}
generator.writeFieldName(name);
objectMapper().writeValue(generator, value);
try {
generator.writeFieldName(name);
objectMapper().writeValue(generator, value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
private ObjectMapper objectMapper() {
if (objectMapper == null) {
throw new IllegalStateException(
"Jackson ObjectMapper required but not set. Expected to be set on either"
+" serverConfig");
throw new IllegalStateException("Jackson ObjectMapper required but not set. Expected to be set on either serverConfig");
}
return (ObjectMapper)objectMapper;
return (ObjectMapper) objectMapper;
}
public static class WriteBean {
final boolean explicitAllProps;
final Set<String> currentIncludeProps;
final BeanDescriptor<?> desc;
final EntityBean currentBean;
WriteBean(BeanDescriptor<?> desc, EntityBean currentBean){
this(desc, false, null, currentBean);
final JsonWriteBeanVisitor visitor;
WriteBean(BeanDescriptor<?> desc, EntityBean currentBean, JsonWriteBeanVisitor visitor) {
this(desc, false, null, currentBean, visitor);
}
WriteBean(BeanDescriptor<?> desc, boolean explicitAllProps, Set<String> currentIncludeProps, EntityBean currentBean) {
WriteBean(BeanDescriptor<?> desc, boolean explicitAllProps, Set<String> currentIncludeProps, EntityBean currentBean, JsonWriteBeanVisitor visitor) {
super();
this.desc = desc;
this.currentBean = currentBean;
this.explicitAllProps = explicitAllProps;
this.currentIncludeProps = currentIncludeProps;
this.visitor = visitor;
}
private boolean isReferenceOnly() {
@@ -251,34 +465,43 @@ public class WriteJson implements JsonWriter {
}
}
public void write(WriteJson writeJson) throws IOException {
BeanProperty beanProp = desc.getIdProperty();
if (beanProp != null) {
if (isIncludeProperty(beanProp)) {
beanProp.jsonWrite(writeJson, currentBean);
}
}
if (!isReferenceOnly()) {
// render all the properties and invoke lazy loading if required
BeanProperty[] props = desc.propertiesNonTransient();
for (int j = 0; j < props.length; j++) {
if (isIncludeProperty(props[j])) {
props[j].jsonWrite(writeJson, currentBean);
@SuppressWarnings("unchecked")
public void write(WriteJson writeJson) {
try {
BeanProperty beanProp = desc.getIdProperty();
if (beanProp != null) {
if (isIncludeProperty(beanProp)) {
beanProp.jsonWrite(writeJson, currentBean);
}
}
props = desc.propertiesTransient();
for (int j = 0; j < props.length; j++) {
if (isIncludeTransientProperty(props[j])) {
props[j].jsonWrite(writeJson, currentBean);
if (!isReferenceOnly()) {
// render all the properties and invoke lazy loading if required
BeanProperty[] props = desc.propertiesNonTransient();
for (int j = 0; j < props.length; j++) {
if (isIncludeProperty(props[j])) {
props[j].jsonWrite(writeJson, currentBean);
}
}
props = desc.propertiesTransient();
for (int j = 0; j < props.length; j++) {
if (isIncludeTransientProperty(props[j])) {
props[j].jsonWrite(writeJson, currentBean);
}
}
}
if (visitor != null) {
visitor.visit(currentBean, writeJson);
}
} catch (IOException e) {
throw new JsonIOException(e);
}
}
}
public Boolean includeMany(String key) {
if (pathProperties != null) {
String fullPath = pathStack.peekFullPath(key);
@@ -287,15 +510,19 @@ public class WriteJson implements JsonWriter {
return null;
}
public void toJson(String name, Collection<?> c) throws IOException {
public void toJson(String name, Collection<?> c) {
beginAssocMany(name);
try {
beginAssocMany(name);
for (Object bean : c) {
BeanDescriptor<?> d = getDescriptor(bean.getClass());
d.jsonWrite(this, (EntityBean)bean, null);
for (Object bean : c) {
BeanDescriptor<?> d = getDescriptor(bean.getClass());
d.jsonWrite(this, (EntityBean) bean, null);
}
endAssocMany();
} catch (IOException e) {
throw new JsonIOException(e);
}
endAssocMany();
}
private <T> BeanDescriptor<T> getDescriptor(Class<T> cls) {
@@ -306,36 +533,5 @@ public class WriteJson implements JsonWriter {
return d;
}
public void beginAssocMany(String key) throws IOException {
pathStack.pushPathKey(key);
generator.writeFieldName(key);
generator.writeStartArray();
}
public void endAssocMany() throws IOException {
pathStack.pop();
generator.writeEndArray();
}
public void writeStartArray(String key) throws IOException {
if (key != null) {
generator.writeFieldName(key);
}
generator.writeStartArray();
}
public void writeStartObject(String key) throws IOException {
if (key != null) {
generator.writeFieldName(key);
}
generator.writeStartObject();
}
public void writeEndObject() throws IOException {
generator.writeEndObject();
}
public void writeEndArray() throws IOException {
generator.writeEndArray();
}
}