Fix for #205: For JsonContext API wrap checked IOException in runtime JsonIOException

This commit is contained in:
rbygrave
2014-11-19 20:00:31 +13:00
parent c95d76d6f6
commit 353f662d70
3 changed files with 155 additions and 91 deletions
@@ -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> T toBean(Class<T> rootType, String json) throws IOException;
public <T> T toBean(Class<T> rootType, String json) throws JsonIOException;
/**
* Convert json reader input into a Bean of a specific type.
*
* @throws JsonIOException When IOException occurs
*/
public <T> T toBean(Class<T> rootType, Reader json) throws IOException;
public <T> T toBean(Class<T> rootType, Reader json) throws JsonIOException;
/**
* Convert json string input into a list of beans of a specific type.
*
* @throws JsonIOException When IOException occurs
*/
public <T> List<T> toList(Class<T> rootType, String json) throws IOException;
public <T> List<T> toList(Class<T> 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 <T> List<T> toList(Class<T> rootType, Reader json) throws IOException;
public <T> List<T> toList(Class<T> 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;
}
@@ -0,0 +1,24 @@
package com.avaje.ebean.text.json;
/**
* Unchecked exception thrown when an IOException occurs in json processing.
* <p>
* Typically wraps the checked IOException.
* </p>
*/
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);
}
}
@@ -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> T toBean(Class<T> 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> T toBean(Class<T> cls, String json) throws JsonIOException {
return toBean(cls, new StringReader(json));
}
public <T> T toBean(Class<T> cls, Reader jsonReader) throws IOException {
public <T> T toBean(Class<T> cls, Reader jsonReader) throws JsonIOException {
return toBean(cls, createParser(jsonReader));
}
private <T> T toBean(Class<T> cls, JsonParser parser) throws IOException {
private <T> T toBean(Class<T> cls, JsonParser parser) throws JsonIOException {
BeanDescriptor<T> d = getDescriptor(cls);
return d.jsonRead(parser, null);
try {
BeanDescriptor<T> d = getDescriptor(cls);
return d.jsonRead(parser, null);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
public <T> List<T> toList(Class<T> cls, String json) throws IOException {
public <T> List<T> toList(Class<T> cls, String json) throws JsonIOException {
return toList(cls, new StringReader(json));
}
public <T> List<T> toList(Class<T> cls, Reader jsonReader) throws IOException {
public <T> List<T> toList(Class<T> cls, Reader jsonReader) throws JsonIOException {
return toList(cls, createParser(jsonReader));
}
private <T> List<T> toList(Class<T> cls, JsonParser src) throws IOException {
private <T> List<T> toList(Class<T> cls, JsonParser src) throws JsonIOException {
BeanDescriptor<T> d = getDescriptor(cls);
try {
BeanDescriptor<T> d = getDescriptor(cls);
List<T> list = new ArrayList<T>();
List<T> list = new ArrayList<T>();
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 <T> void toJsonFromCollection(Collection<T> 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<Object, Object> 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);