#307 - Add back support for JsonValueAdapter and JsonReadOptions into 4.x

This commit is contained in:
rbygrave committed 2015-06-21 01:46:23 +12:00
1 parent f0b8270e5e
commit 721a50219d
15 files changed
+407 -47

No files matched your search

@@ -1,11 +1,8 @@
package com.avaje.ebeaninternal.server.text.json;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebean.text.json.EJson;
import com.avaje.ebean.text.json.*;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebean.text.json.JsonContext;
import com.avaje.ebean.text.json.JsonIOException;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.util.ParamTypeHelper;
@@ -56,15 +53,29 @@ public class DJsonContext implements JsonContext {
return toBean(cls, new StringReader(json));
}
@Override
public <T> T toBean(Class<T> cls, String json, JsonReadOptions options) throws JsonIOException {
return toBean(cls, new StringReader(json), options);
}
public <T> T toBean(Class<T> cls, Reader jsonReader) throws JsonIOException {
return toBean(cls, createParser(jsonReader));
}
public <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException {
public <T> T toBean(Class<T> cls, Reader jsonReader, JsonReadOptions options) throws JsonIOException {
return toBean(cls, createParser(jsonReader), options);
}
public <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException {
return toBean(cls, parser, null);
}
public <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) throws JsonIOException {
ReadJson readJson = new ReadJson(parser, options);
try {
BeanDescriptor<T> d = getDescriptor(cls);
return d.jsonRead(parser, null);
return d.jsonRead(readJson, null);
} catch (IOException e) {
throw new JsonIOException(e);
}
@@ -74,13 +85,26 @@ public class DJsonContext implements JsonContext {
return toList(cls, new StringReader(json));
}
@Override
public <T> List<T> toList(Class<T> cls, String json, JsonReadOptions options) throws JsonIOException {
return toList(cls, new StringReader(json), options);
}
public <T> List<T> toList(Class<T> cls, Reader jsonReader) throws JsonIOException {
return toList(cls, createParser(jsonReader));
}
public <T> List<T> toList(Class<T> cls, JsonParser src) throws JsonIOException {
public <T> List<T> toList(Class<T> cls, Reader jsonReader, JsonReadOptions options) throws JsonIOException {
return toList(cls, createParser(jsonReader), options);
}
public <T> List<T> toList(Class<T> cls, JsonParser src) throws JsonIOException {
return toList(cls, src, null);
}
public <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options) throws JsonIOException {
ReadJson readJson = new ReadJson(src, options);
try {
BeanDescriptor<T> d = getDescriptor(cls);
@@ -92,7 +116,7 @@ public class DJsonContext implements JsonContext {
}
do {
T bean = d.jsonRead(src, null);
T bean = d.jsonRead(readJson, null);
if (bean == null) {
break;
} else {
@@ -0,0 +1,87 @@
package com.avaje.ebeaninternal.server.text.json;
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 java.io.IOException;
import java.util.Map;
/**
* Context for JSON read processing.
*/
public class ReadJson {
/**
* Jackson parser.
*/
final JsonParser parser;
/**
* Stack of the path - used to find the appropriate JsonReadBeanVisitor.
*/
final PathStack pathStack;
/**
* Map of the JsonReadBeanVisitor keyed by path.
*/
final Map<String, JsonReadBeanVisitor<?>> visitorMap;
/**
* Construct with parser and readOptions.
*/
public ReadJson(JsonParser parser, JsonReadOptions readOptions) {
this.parser = parser;
// only create visitorMap, pathStack if needed ...
this.visitorMap = (readOptions == null) ? null : readOptions.getVisitorMap();
this.pathStack = (visitorMap == null) ? null : new PathStack();
}
/**
* Return the JsonParser.
*/
public JsonParser getParser() {
return parser;
}
/**
* Return the next JsonToken from the underlying parser.
*/
public JsonToken nextToken() throws IOException {
return parser.nextToken();
}
/**
* Push the path onto the stack (traversing a 1-M or M-1 etc)
*/
public void pushPath(String path) {
if (pathStack != null) {
pathStack.pushPathKey(path);
}
}
/**
* Pop the path stack.
*/
public void popPath() {
if (pathStack != null) {
pathStack.pop();
}
}
/**
* If there is a JsonReadBeanVisitor registered to the current path then
* call it's visit method with the bean and unmappedProperties.
*/
@SuppressWarnings(value = "unchecked")
public void beanVisitor(Object bean, Map<String, Object> unmappedProperties) {
if (visitorMap != null) {
JsonReadBeanVisitor visitor = visitorMap.get(pathStack.peekWithNull());
if (visitor != null) {
visitor.visit(bean, unmappedProperties);
}
}
}
}