mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
This commit is contained in:
1 parent
a00980c61f
commit
160e4d7dd1
25 files changed
+633
-1358
No files matched your search
@@ -24,9 +24,12 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private final JsonFactory jsonFactory;
|
||||
|
||||
private final Object defaultObjectMapper;
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory) {
|
||||
this.server = server;
|
||||
this.jsonFactory = (jsonFactory != null) ? jsonFactory : new JsonFactory();
|
||||
this.defaultObjectMapper = this.server.getServerConfig().getObjectMapper();
|
||||
}
|
||||
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
@@ -72,7 +75,7 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
public <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) throws JsonIOException {
|
||||
|
||||
ReadJson readJson = new ReadJson(parser, options);
|
||||
ReadJson readJson = new ReadJson(parser, options, determineObjectMapper(options));
|
||||
try {
|
||||
BeanDescriptor<T> d = getDescriptor(cls);
|
||||
return d.jsonRead(readJson, null);
|
||||
@@ -104,7 +107,7 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options) throws JsonIOException {
|
||||
|
||||
ReadJson readJson = new ReadJson(src, options);
|
||||
ReadJson readJson = new ReadJson(src, options, determineObjectMapper(options));
|
||||
try {
|
||||
BeanDescriptor<T> d = getDescriptor(cls);
|
||||
|
||||
@@ -271,7 +274,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);
|
||||
return new WriteJson(server, gen, pathProps, determineObjectMapper(options));
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> collection, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException {
|
||||
@@ -320,11 +323,36 @@ public class DJsonContext implements JsonContext {
|
||||
gen.writeEndObject();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDescriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
/**
|
||||
* Return the BeanDescriptor for the given bean type.
|
||||
*/
|
||||
private <T> BeanDescriptor<T> getDescriptor(Class<T> beanType) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(beanType);
|
||||
if (d == null) {
|
||||
throw new RuntimeException("No BeanDescriptor found for " + cls);
|
||||
throw new RuntimeException("No BeanDescriptor found for " + beanType);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the object mapper to use for a JSON read request.
|
||||
*/
|
||||
private Object determineObjectMapper(JsonReadOptions options) {
|
||||
if (options == null) {
|
||||
return defaultObjectMapper;
|
||||
}
|
||||
Object mapper = options.getObjectMapper();
|
||||
return (mapper != null) ? mapper : defaultObjectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the object mapper to use for a JSON write request.
|
||||
*/
|
||||
private Object determineObjectMapper(JsonWriteOptions options) {
|
||||
if (options == null) {
|
||||
return defaultObjectMapper;
|
||||
}
|
||||
Object mapper = options.getObjectMapper();
|
||||
return (mapper != null) ? mapper : defaultObjectMapper;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.avaje.ebean.text.json.JsonReadBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonReadOptions;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
@@ -28,17 +29,33 @@ public class ReadJson {
|
||||
*/
|
||||
final Map<String, JsonReadBeanVisitor<?>> visitorMap;
|
||||
|
||||
final Object objectMapper;
|
||||
|
||||
/**
|
||||
* Construct with parser and readOptions.
|
||||
*/
|
||||
public ReadJson(JsonParser parser, JsonReadOptions readOptions) {
|
||||
public ReadJson(JsonParser parser, JsonReadOptions readOptions, Object objectMapper) {
|
||||
|
||||
this.parser = parser;
|
||||
this.objectMapper = objectMapper;
|
||||
|
||||
// only create visitorMap, pathStack if needed ...
|
||||
this.visitorMap = (readOptions == null) ? null : readOptions.getVisitorMap();
|
||||
this.pathStack = (visitorMap == null) ? null : new PathStack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the objectMapper used for this request.
|
||||
*/
|
||||
public ObjectMapper getObjectMapper() {
|
||||
if (objectMapper == null) {
|
||||
throw new IllegalStateException(
|
||||
"Jackson ObjectMapper required but has not set. The ObjectMapper can be set on"
|
||||
+" either the ServerConfig or on JsonReadOptions.");
|
||||
}
|
||||
return (ObjectMapper)objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the JsonParser.
|
||||
*/
|
||||
@@ -84,4 +101,13 @@ public class ReadJson {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the property value using Jackson ObjectMapper.
|
||||
* <p/>
|
||||
* Typically this is used to read Transient properties where the type is unknown to Ebean.
|
||||
*/
|
||||
public Object readValueUsingObjectMapper(Class<?> propertyType) throws IOException {
|
||||
return getObjectMapper().readValue(parser, propertyType);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
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.util.Collection;
|
||||
@@ -24,10 +25,13 @@ public class WriteJson {
|
||||
|
||||
private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties){
|
||||
private final Object objectMapper;
|
||||
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties, Object objectMapper){
|
||||
this.server = server;
|
||||
this.generator = generator;
|
||||
this.pathProperties = pathProperties;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public JsonGenerator gen() {
|
||||
@@ -73,6 +77,20 @@ public class WriteJson {
|
||||
return new WriteBean(desc, explicitAllProps, currentIncludeProps, bean);
|
||||
}
|
||||
|
||||
public void writeValueUsingObjectMapper(String name, Object value) throws IOException {
|
||||
generator.writeFieldName(name);
|
||||
objectMapper().writeValue(generator, value);
|
||||
}
|
||||
|
||||
private ObjectMapper objectMapper() {
|
||||
if (objectMapper == null) {
|
||||
throw new IllegalStateException(
|
||||
"Jackson ObjectMapper required but not set. Expected to be set on either"
|
||||
+" serverConfig");
|
||||
}
|
||||
return (ObjectMapper)objectMapper;
|
||||
}
|
||||
|
||||
public static class WriteBean {
|
||||
|
||||
final boolean explicitAllProps;
|
||||
|
||||
Reference in new issue
Block a user