mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
#280 - ENH: Add ServerConfig.setJsonInclude(...) ALL, NON_NULL, NON_EMPTY
This commit is contained in:
1 parent
160e4d7dd1
commit
f6466d3211
55 files changed
+616
-109
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebean.text.json.*;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
@@ -26,10 +27,13 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private final Object defaultObjectMapper;
|
||||
|
||||
private final JsonConfig.Include defaultInclude;
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory) {
|
||||
this.server = server;
|
||||
this.jsonFactory = (jsonFactory != null) ? jsonFactory : new JsonFactory();
|
||||
this.defaultObjectMapper = this.server.getServerConfig().getObjectMapper();
|
||||
this.defaultInclude = this.server.getServerConfig().getJsonInclude();
|
||||
}
|
||||
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
@@ -274,7 +278,7 @@ 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));
|
||||
return new WriteJson(server, gen, pathProps, determineObjectMapper(options), determineInclude(options));
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> collection, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException {
|
||||
@@ -355,4 +359,15 @@ public class DJsonContext implements JsonContext {
|
||||
Object mapper = options.getObjectMapper();
|
||||
return (mapper != null) ? mapper : defaultObjectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the include mode to use for a JSON write request.
|
||||
*/
|
||||
private JsonConfig.Include determineInclude(JsonWriteOptions options) {
|
||||
if (options == null) {
|
||||
return defaultInclude;
|
||||
}
|
||||
JsonConfig.Include include = options.getInclude();
|
||||
return (include != null) ? include : defaultInclude;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,24 @@
|
||||
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.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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class WriteJson {
|
||||
public class WriteJson implements JsonWriter {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
@@ -27,17 +32,105 @@ public class WriteJson {
|
||||
|
||||
private final Object objectMapper;
|
||||
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties, Object objectMapper){
|
||||
private final JsonConfig.Include include;
|
||||
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties, Object objectMapper, JsonConfig.Include include){
|
||||
this.server = server;
|
||||
this.generator = generator;
|
||||
this.pathProperties = pathProperties;
|
||||
this.objectMapper = objectMapper;
|
||||
this.include = include;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct for testing purposes only.
|
||||
*/
|
||||
public WriteJson(JsonGenerator generator, JsonConfig.Include include) {
|
||||
this(null, generator, null, null, include);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if null values should be included in JSON output.
|
||||
*/
|
||||
public boolean isIncludeNull() {
|
||||
return include == JsonConfig.Include.ALL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if empty collections should be included in the JSON output.
|
||||
*/
|
||||
public boolean isIncludeEmpty() {
|
||||
return include != JsonConfig.Include.NON_EMPTY;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, Long value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, Double value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, int value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, Short value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, Float value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void writeNumberField(String name, BigDecimal value) throws IOException {
|
||||
generator.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeStringField(String name, String value) throws IOException {
|
||||
generator.writeStringField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBinary(InputStream is, int length) throws IOException {
|
||||
generator.writeBinary(is, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBinaryField(String name, byte[] value) throws IOException {
|
||||
generator.writeBinaryField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBooleanField(String name, Boolean value) throws IOException {
|
||||
generator.writeBooleanField(name, value);
|
||||
}
|
||||
|
||||
public boolean isParentBean(Object bean) {
|
||||
return !parentBeans.isEmpty() && parentBeans.contains(bean);
|
||||
}
|
||||
@@ -78,6 +171,17 @@ public class WriteJson {
|
||||
}
|
||||
|
||||
public void writeValueUsingObjectMapper(String name, Object value) throws IOException {
|
||||
|
||||
if (!isIncludeEmpty()) {
|
||||
// check for suppression of empty collection or map
|
||||
if (value instanceof Collection && ((Collection)value).isEmpty()) {
|
||||
// suppress empty collection
|
||||
return;
|
||||
} else if (value instanceof Map && ((Map)value).isEmpty()) {
|
||||
// suppress empty map
|
||||
return;
|
||||
}
|
||||
}
|
||||
generator.writeFieldName(name);
|
||||
objectMapper().writeValue(generator, value);
|
||||
}
|
||||
@@ -215,10 +319,6 @@ public class WriteJson {
|
||||
}
|
||||
generator.writeStartObject();
|
||||
}
|
||||
|
||||
public void writeNull(String name) throws IOException {
|
||||
generator.writeNullField(name);
|
||||
}
|
||||
|
||||
public void writeEndObject() throws IOException {
|
||||
generator.writeEndObject();
|
||||
|
||||
Reference in new issue
Block a user