Modify JsonContext added support for toBean() toList() and toObject() using JsonParser

This commit is contained in:
rbygrave
2014-12-18 22:07:47 +13:00
parent 423066ada7
commit e0159ccb4f
2 changed files with 32 additions and 18 deletions
@@ -28,6 +28,13 @@ public interface JsonContext {
*/
public <T> T toBean(Class<T> rootType, Reader json) throws JsonIOException;
/**
* Convert json parser input into a Bean of a specific type.
*
* @throws JsonIOException When IOException occurs
*/
public <T> T toBean(Class<T> 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 <T> List<T> toList(Class<T> rootType, Reader json) throws JsonIOException;
/**
* Convert json parser input into a list of beans of a specific type.
*
* @throws JsonIOException When IOException occurs
*/
public <T> List<T> toList(Class<T> 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;
@@ -60,7 +60,7 @@ public class DJsonContext implements JsonContext {
return toBean(cls, createParser(jsonReader));
}
private <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException {
public <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException {
try {
BeanDescriptor<T> d = getDescriptor(cls);
@@ -79,7 +79,7 @@ public class DJsonContext implements JsonContext {
return toList(cls, createParser(jsonReader));
}
private <T> List<T> toList(Class<T> cls, JsonParser src) throws JsonIOException {
public <T> List<T> toList(Class<T> cls, JsonParser src) throws JsonIOException {
try {
BeanDescriptor<T> 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");