From e0159ccb4fedf5e047fac5ca257f89e54720c4aa Mon Sep 17 00:00:00 2001 From: rbygrave Date: Thu, 18 Dec 2014 22:07:47 +1300 Subject: [PATCH] Modify JsonContext added support for toBean() toList() and toObject() using JsonParser --- .../avaje/ebean/text/json/JsonContext.java | 24 +++++++++++++++-- .../server/text/json/DJsonContext.java | 26 +++++++------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/avaje/ebean/text/json/JsonContext.java b/src/main/java/com/avaje/ebean/text/json/JsonContext.java index 4115a2401..653519f63 100644 --- a/src/main/java/com/avaje/ebean/text/json/JsonContext.java +++ b/src/main/java/com/avaje/ebean/text/json/JsonContext.java @@ -28,6 +28,13 @@ public interface JsonContext { */ public T toBean(Class rootType, Reader json) throws JsonIOException; + /** + * Convert json parser input into a Bean of a specific type. + * + * @throws JsonIOException When IOException occurs + */ + public T toBean(Class cls, JsonParser parser) throws JsonIOException; + /** * Convert json string input into a list of beans of a specific type. * @@ -42,6 +49,13 @@ public interface JsonContext { */ public List toList(Class rootType, Reader json) throws JsonIOException; + /** + * Convert json parser input into a list of beans of a specific type. + * + * @throws JsonIOException When IOException occurs + */ + public List toList(Class cls, JsonParser src) throws JsonIOException; + /** * Use the genericType to determine if this should be converted into a List or * bean. @@ -58,6 +72,14 @@ public interface JsonContext { */ public Object toObject(Type genericType, String json) throws JsonIOException; + /** + * Use the genericType to determine if this should be converted into a List or + * bean. + * + * @throws JsonIOException When IOException occurs + */ + public Object toObject(Type genericType, JsonParser jsonParser) throws JsonIOException; + /** * Return the bean or collection as JSON string. * @@ -109,7 +131,6 @@ public interface JsonContext { * With additional options. * * @throws JsonIOException When IOException occurs - * * @deprecated */ public void toJson(Object value, Writer writer, JsonWriteOptions options) throws JsonIOException; @@ -119,7 +140,6 @@ public interface JsonContext { * Convert a bean or collection to json string. * * @throws JsonIOException When IOException occurs - * * @deprecated */ public String toJson(Object value, JsonWriteOptions options) throws JsonIOException; diff --git a/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java b/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java index 84e8e92b8..0f47b1202 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java @@ -60,7 +60,7 @@ public class DJsonContext implements JsonContext { return toBean(cls, createParser(jsonReader)); } - private T toBean(Class cls, JsonParser parser) throws JsonIOException { + public T toBean(Class cls, JsonParser parser) throws JsonIOException { try { BeanDescriptor d = getDescriptor(cls); @@ -79,7 +79,7 @@ public class DJsonContext implements JsonContext { return toList(cls, createParser(jsonReader)); } - private List toList(Class cls, JsonParser src) throws JsonIOException { + public List toList(Class cls, JsonParser src) throws JsonIOException { try { BeanDescriptor d = getDescriptor(cls); @@ -108,30 +108,24 @@ public class DJsonContext implements JsonContext { public Object toObject(Type genericType, String json) throws JsonIOException { - TypeInfo info = ParamTypeHelper.getTypeInfo(genericType); - ManyType manyType = info.getManyType(); - switch (manyType) { - case NONE: - return toBean(info.getBeanType(), json); - - case LIST: - return toList(info.getBeanType(), json); - - default: - throw new JsonIOException("Type " + manyType + " not supported"); - } + return toObject(genericType, createParser(new StringReader(json))); } public Object toObject(Type genericType, Reader json) throws JsonIOException { + return toObject(genericType, createParser(json)); + } + + public Object toObject(Type genericType, JsonParser jsonParser) throws JsonIOException { + TypeInfo info = ParamTypeHelper.getTypeInfo(genericType); ManyType manyType = info.getManyType(); switch (manyType) { case NONE: - return toBean(info.getBeanType(), json); + return toBean(info.getBeanType(), jsonParser); case LIST: - return toList(info.getBeanType(), json); + return toList(info.getBeanType(), jsonParser); default: throw new JsonIOException("Type " + manyType + " not supported");