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 38b095ae5..b79b3f0a8 100644 --- a/src/main/java/com/avaje/ebean/text/json/JsonContext.java +++ b/src/main/java/com/avaje/ebean/text/json/JsonContext.java @@ -1,14 +1,13 @@ package com.avaje.ebean.text.json; -import java.io.IOException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; + import java.io.Reader; import java.io.Writer; import java.lang.reflect.Type; import java.util.List; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; - /** * Converts objects to and from JSON format. */ @@ -16,70 +15,82 @@ public interface JsonContext { /** * Convert json string input into a Bean of a specific type. + * + * @throws JsonIOException When IOException occurs */ - public T toBean(Class rootType, String json) throws IOException; + public T toBean(Class rootType, String json) throws JsonIOException; /** * Convert json reader input into a Bean of a specific type. + * + * @throws JsonIOException When IOException occurs */ - public T toBean(Class rootType, Reader json) throws IOException; + public T toBean(Class rootType, Reader json) throws JsonIOException; /** * Convert json string input into a list of beans of a specific type. + * + * @throws JsonIOException When IOException occurs */ - public List toList(Class rootType, String json) throws IOException; + public List toList(Class rootType, String json) throws JsonIOException; /** * Convert json reader input into a list of beans of a specific type. - * @throws IOException + * + * @throws JsonIOException When IOException occurs */ - public List toList(Class rootType, Reader json) throws IOException; + public List toList(Class rootType, Reader 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, Reader json) throws IOException; + public Object toObject(Type genericType, Reader 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, String json) throws IOException; + public Object toObject(Type genericType, String json) throws JsonIOException; /** * Write the bean or collection in JSON format to the writer with default * options. - * - * @param value - * the bean or collection of beans to write - * @param writer - * used to write the json output to + * + * @param value the bean or collection of beans to write + * @param writer used to write the json output to + * @throws JsonIOException When IOException occurs */ - public void toJson(Object value, Writer writer) throws IOException; + public void toJson(Object value, Writer writer) throws JsonIOException; /** * With additional options to specify JsonValueAdapter and * JsonWriteBeanVisitor's. - * - * @param value - * the bean or collection of beans to write - * @param writer - * used to write the json output to - * @param options - * additional options to control the JSON output + * + * @param value the bean or collection of beans to write + * @param writer used to write the json output to + * @param options additional options to control the JSON output + * @throws JsonIOException When IOException occurs */ - public void toJson(Object value, Writer writer, JsonWriteOptions options) throws IOException; + public void toJson(Object value, Writer writer, JsonWriteOptions options) throws JsonIOException; /** * Convert a bean or collection to json string using default options. + * + * @throws JsonIOException When IOException occurs */ - public String toJson(Object value) throws IOException; + public String toJson(Object value) throws JsonIOException; /** * Convert a bean or collection to json string. + * + * @throws JsonIOException When IOException occurs */ - public String toJson(Object value, JsonWriteOptions options) throws IOException; + public String toJson(Object value, JsonWriteOptions options) throws JsonIOException; /** * Return true if the type is known as an Entity bean or a List Set or @@ -89,11 +100,15 @@ public interface JsonContext { /** * Create and return a new JsonGenerator for the given writer. + * + * @throws JsonIOException When IOException occurs */ - public JsonGenerator createGenerator(Writer writer) throws IOException; - + public JsonGenerator createGenerator(Writer writer) throws JsonIOException; + /** * Create and return a new JsonParser for the given reader. + * + * @throws JsonIOException When IOException occurs */ - public JsonParser createParser(Reader reader) throws IOException; + public JsonParser createParser(Reader reader) throws JsonIOException; } \ No newline at end of file diff --git a/src/main/java/com/avaje/ebean/text/json/JsonIOException.java b/src/main/java/com/avaje/ebean/text/json/JsonIOException.java new file mode 100644 index 000000000..d14fc7c06 --- /dev/null +++ b/src/main/java/com/avaje/ebean/text/json/JsonIOException.java @@ -0,0 +1,24 @@ +package com.avaje.ebean.text.json; + +/** + * Unchecked exception thrown when an IOException occurs in json processing. + *

+ * Typically wraps the checked IOException. + *

+ */ +public class JsonIOException extends RuntimeException { + + /** + * Construct with an underlying cause. + */ + public JsonIOException(Throwable cause) { + super(cause); + } + + /** + * Construct with a message. + */ + public JsonIOException(String message) { + super(message); + } +} 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 b71c90318..9dcd241fa 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 @@ -4,6 +4,7 @@ import com.avaje.ebean.bean.EntityBean; import com.avaje.ebean.text.json.EJson; 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; @@ -23,7 +24,7 @@ import java.util.Map.Entry; public class DJsonContext implements JsonContext { private final SpiEbeanServer server; - + private final JsonFactory jsonFactory; public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory) { @@ -35,117 +36,141 @@ public class DJsonContext implements JsonContext { return server.isSupportedType(genericType); } - public JsonGenerator createGenerator(Writer writer) throws IOException { - return jsonFactory.createGenerator(writer); - } - - public JsonParser createParser(Reader reader) throws IOException { - return jsonFactory.createParser(reader); + public JsonGenerator createGenerator(Writer writer) throws JsonIOException { + try { + return jsonFactory.createGenerator(writer); + } catch (IOException e) { + throw new JsonIOException(e); + } } - public T toBean(Class cls, String json) throws IOException { + public JsonParser createParser(Reader reader) throws JsonIOException { + try { + return jsonFactory.createParser(reader); + } catch (IOException e) { + throw new JsonIOException(e); + } + } + + public T toBean(Class cls, String json) throws JsonIOException { return toBean(cls, new StringReader(json)); } - public T toBean(Class cls, Reader jsonReader) throws IOException { + public T toBean(Class cls, Reader jsonReader) throws JsonIOException { return toBean(cls, createParser(jsonReader)); } - private T toBean(Class cls, JsonParser parser) throws IOException { + private T toBean(Class cls, JsonParser parser) throws JsonIOException { - BeanDescriptor d = getDescriptor(cls); - return d.jsonRead(parser, null); + try { + BeanDescriptor d = getDescriptor(cls); + return d.jsonRead(parser, null); + } catch (IOException e) { + throw new JsonIOException(e); + } } - public List toList(Class cls, String json) throws IOException { + public List toList(Class cls, String json) throws JsonIOException { return toList(cls, new StringReader(json)); } - public List toList(Class cls, Reader jsonReader) throws IOException { + public List toList(Class cls, Reader jsonReader) throws JsonIOException { return toList(cls, createParser(jsonReader)); } - private List toList(Class cls, JsonParser src) throws IOException { + private List toList(Class cls, JsonParser src) throws JsonIOException { - BeanDescriptor d = getDescriptor(cls); + try { + BeanDescriptor d = getDescriptor(cls); - List list = new ArrayList(); + List list = new ArrayList(); - JsonToken event = src.nextToken(); - if (event != JsonToken.START_ARRAY) { - throw new JsonParseException("Expecting start_array event but got " + event ,src.getCurrentLocation()); - } - - do { - T bean = d.jsonRead(src, null); - if (bean == null) { - break; - } else { - list.add(bean); + JsonToken event = src.nextToken(); + if (event != JsonToken.START_ARRAY) { + throw new JsonParseException("Expecting start_array event but got " + event, src.getCurrentLocation()); } - } while (true); - return list; + do { + T bean = d.jsonRead(src, null); + if (bean == null) { + break; + } else { + list.add(bean); + } + } while (true); + + return list; + } catch (IOException e) { + throw new JsonIOException(e); + } } - public Object toObject(Type genericType, String json) throws IOException { + 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 NONE: + return toBean(info.getBeanType(), json); - case LIST: - return toList(info.getBeanType(), json); + case LIST: + return toList(info.getBeanType(), json); - default: - throw new IOException("Type " + manyType + " not supported"); + default: + throw new JsonIOException("Type " + manyType + " not supported"); } } - public Object toObject(Type genericType, Reader json) throws IOException { + public Object toObject(Type genericType, Reader json) throws JsonIOException { TypeInfo info = ParamTypeHelper.getTypeInfo(genericType); ManyType manyType = info.getManyType(); switch (manyType) { - case NONE: - return toBean(info.getBeanType(), json); + case NONE: + return toBean(info.getBeanType(), json); - case LIST: - return toList(info.getBeanType(), json); + case LIST: + return toList(info.getBeanType(), json); - default: - throw new IOException("Type " + manyType + " not supported"); + default: + throw new JsonIOException("Type " + manyType + " not supported"); } } - public void toJson(Object o, Writer writer) throws IOException { + public void toJson(Object o, Writer writer) throws JsonIOException { toJson(o, writer, null); } - public void toJson(Object o, Writer writer, JsonWriteOptions options) throws IOException { - JsonGenerator generator = createGenerator(writer); - toJsonInternal(o, generator, options); - generator.close(); + public void toJson(Object o, Writer writer, JsonWriteOptions options) throws JsonIOException { + try { + JsonGenerator generator = createGenerator(writer); + toJsonInternal(o, generator, options); + generator.close(); + } catch (IOException e) { + throw new JsonIOException(e); + } } - public String toJson(Object o) throws IOException { + public String toJson(Object o) throws JsonIOException { return toJsonString(o, null); } - public String toJson(Object o, JsonWriteOptions options) throws IOException { + public String toJson(Object o, JsonWriteOptions options) throws JsonIOException { return toJsonString(o, options); } - private String toJsonString(Object o, JsonWriteOptions options) throws IOException { - StringWriter writer = new StringWriter(500); - JsonGenerator gen = createGenerator(writer); - toJsonInternal(o, gen, options); - gen.close(); - return writer.toString(); + private String toJsonString(Object o, JsonWriteOptions options) throws JsonIOException { + try { + StringWriter writer = new StringWriter(500); + JsonGenerator gen = createGenerator(writer); + toJsonInternal(o, gen, options); + gen.close(); + return writer.toString(); + } catch (IOException e) { + throw new JsonIOException(e); + } } @SuppressWarnings("unchecked") @@ -171,7 +196,7 @@ public class DJsonContext implements JsonContext { } else if (o instanceof EntityBean) { BeanDescriptor d = getDescriptor(o.getClass()); WriteJson writeJson = createWriteJson(gen, options); - d.jsonWrite(writeJson, (EntityBean)o, null); + d.jsonWrite(writeJson, (EntityBean) o, null); } } @@ -183,7 +208,7 @@ public class DJsonContext implements JsonContext { private void toJsonFromCollection(Collection collection, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException { if (key != null) { - gen.writeFieldName(key); + gen.writeFieldName(key); } gen.writeStartArray(); @@ -203,7 +228,7 @@ public class DJsonContext implements JsonContext { WriteJson writeJson = createWriteJson(gen, options); gen.writeStartObject(); - + while (it.hasNext()) { Entry entry = it.next(); String key = entry.getKey().toString(); @@ -216,7 +241,7 @@ public class DJsonContext implements JsonContext { } else if (value instanceof EntityBean) { BeanDescriptor d = getDescriptor(value.getClass()); - d.jsonWrite(writeJson,(EntityBean) value, key); + d.jsonWrite(writeJson, (EntityBean) value, key); } else { EJson.write(entry, gen);