mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
JSON refactor - tidy up
This commit is contained in:
1 parent
221272328c
commit
eaee3a9c02
31 files changed
+935
-1033
No files matched your search
@@ -1,14 +1,14 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson.WriteBean;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BeanDescriptorJsonHelp<T> {
|
||||
|
||||
private final BeanDescriptor<T> desc;
|
||||
@@ -56,7 +56,7 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
return null;
|
||||
}
|
||||
if (JsonToken.START_OBJECT != token) {
|
||||
throw new IOException("Unexpected token "+token+" - expecting start_object at: "+parser.getCurrentLocation());
|
||||
throw new JsonParseException("Unexpected token "+token+" - expecting start_object", parser.getCurrentLocation());
|
||||
}
|
||||
|
||||
if (desc.inheritInfo == null) {
|
||||
@@ -69,7 +69,7 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
token = parser.nextToken();
|
||||
if (token != JsonToken.FIELD_NAME) {
|
||||
String msg = "Error reading inheritance discriminator - expected [" + discColumn + "] but no json key?";
|
||||
throw new TextException(msg);
|
||||
throw new JsonParseException(msg, parser.getCurrentLocation());
|
||||
}
|
||||
|
||||
String propName = parser.getCurrentName();
|
||||
@@ -82,7 +82,7 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
return jsonReadProperties(parser, bean);
|
||||
}
|
||||
String msg = "Error reading inheritance discriminator, expected property ["+discColumn+"] but got [" + propName + "] ?";
|
||||
throw new TextException(msg);
|
||||
throw new JsonParseException(msg, parser.getCurrentLocation());
|
||||
}
|
||||
|
||||
String discValue = parser.nextTextValue();
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,23 +1,8 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
@@ -25,16 +10,15 @@ import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.ManyType;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.TypeInfo;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.core.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Default implementation of JsonContext.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public class DJsonContext implements JsonContext {
|
||||
|
||||
@@ -69,7 +53,7 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private <T> T toBean(Class<T> cls, JsonParser parser) throws IOException {
|
||||
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
BeanDescriptor<T> d = getDescriptor(cls);
|
||||
return d.jsonRead(parser, null);
|
||||
}
|
||||
|
||||
@@ -84,30 +68,25 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private <T> List<T> toList(Class<T> cls, JsonParser src) throws IOException {
|
||||
|
||||
try {
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
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);
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return list;
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
throw new TextException("Error parsing " + src, e);
|
||||
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);
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public Object toObject(Type genericType, String json) throws IOException {
|
||||
@@ -122,7 +101,7 @@ public class DJsonContext implements JsonContext {
|
||||
return toList(info.getBeanType(), json);
|
||||
|
||||
default:
|
||||
throw new TextException("Type " + manyType + " not supported");
|
||||
throw new IOException("Type " + manyType + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,26 +117,30 @@ public class DJsonContext implements JsonContext {
|
||||
return toList(info.getBeanType(), json);
|
||||
|
||||
default:
|
||||
throw new TextException("Type " + manyType + " not supported");
|
||||
throw new IOException("Type " + manyType + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer) throws IOException {
|
||||
toJsonWriter(o, writer, null);
|
||||
public void toJson(Object o, Writer writer) throws IOException {
|
||||
toJson(o, writer, null);
|
||||
}
|
||||
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, JsonWriteOptions options) throws IOException {
|
||||
public void toJson(Object o, Writer writer, JsonWriteOptions options) throws IOException {
|
||||
JsonGenerator generator = createGenerator(writer);
|
||||
toJsonInternal(o, generator, options);
|
||||
generator.close();
|
||||
}
|
||||
|
||||
public String toJsonString(Object o) throws IOException {
|
||||
public String toJson(Object o) throws IOException {
|
||||
return toJsonString(o, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, JsonWriteOptions options) throws IOException {
|
||||
public String toJson(Object o, JsonWriteOptions options) throws IOException {
|
||||
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);
|
||||
@@ -173,7 +156,7 @@ public class DJsonContext implements JsonContext {
|
||||
} else if (o instanceof Number) {
|
||||
gen.writeNumber(((Number) o).doubleValue());
|
||||
} else if (o instanceof Boolean) {
|
||||
gen.writeBoolean(((Boolean) o).booleanValue());
|
||||
gen.writeBoolean((Boolean) o);
|
||||
} else if (o instanceof String) {
|
||||
gen.writeString((String) o);
|
||||
|
||||
@@ -186,7 +169,7 @@ public class DJsonContext implements JsonContext {
|
||||
toJsonFromCollection((Collection<?>) o, null, gen, options);
|
||||
|
||||
} else if (o instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
BeanDescriptor<?> d = getDescriptor(o.getClass());
|
||||
WriteJson writeJson = createWriteJson(gen, options);
|
||||
d.jsonWrite(writeJson, (EntityBean)o, null);
|
||||
}
|
||||
@@ -197,7 +180,7 @@ public class DJsonContext implements JsonContext {
|
||||
return new WriteJson(server, gen, pathProps);
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> c, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException {
|
||||
private <T> void toJsonFromCollection(Collection<T> collection, String key, JsonGenerator gen, JsonWriteOptions options) throws IOException {
|
||||
|
||||
if (key != null) {
|
||||
gen.writeFieldName(key);
|
||||
@@ -206,11 +189,9 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
WriteJson writeJson = createWriteJson(gen, options);
|
||||
|
||||
Iterator<T> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
T t = it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(t.getClass());
|
||||
d.jsonWrite(writeJson, (EntityBean)t, null);
|
||||
for (T bean : collection) {
|
||||
BeanDescriptor<?> d = getDescriptor(bean.getClass());
|
||||
d.jsonWrite(writeJson, (EntityBean) bean, null);
|
||||
}
|
||||
gen.writeEndArray();
|
||||
}
|
||||
@@ -234,7 +215,7 @@ public class DJsonContext implements JsonContext {
|
||||
toJsonFromCollection((Collection<?>) value, key, gen, options);
|
||||
|
||||
} else if (value instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(value.getClass());
|
||||
BeanDescriptor<?> d = getDescriptor(value.getClass());
|
||||
d.jsonWrite(writeJson,(EntityBean) value, key);
|
||||
|
||||
} else {
|
||||
@@ -245,7 +226,7 @@ public class DJsonContext implements JsonContext {
|
||||
gen.writeEndObject();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
private <T> BeanDescriptor<T> getDescriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null) {
|
||||
throw new RuntimeException("No BeanDescriptor found for " + cls);
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
@@ -13,6 +8,10 @@ import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
public class WriteJson {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
@@ -36,11 +35,7 @@ public class WriteJson {
|
||||
}
|
||||
|
||||
public boolean isParentBean(Object bean) {
|
||||
if (parentBeans.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return parentBeans.contains(bean);
|
||||
}
|
||||
return !parentBeans.isEmpty() && parentBeans.contains(bean);
|
||||
}
|
||||
|
||||
public void pushParentBeanMany(Object parentBean) {
|
||||
@@ -61,15 +56,6 @@ public class WriteJson {
|
||||
pathStack.pop();
|
||||
}
|
||||
|
||||
public Set<String> getIncludeProperties() {
|
||||
|
||||
if (pathProperties == null) {
|
||||
return null;
|
||||
} else {
|
||||
return pathProperties.get(pathStack.peekWithNull());
|
||||
}
|
||||
}
|
||||
|
||||
public WriteBean createWriteBean(BeanDescriptor<?> desc, EntityBean bean) {
|
||||
|
||||
if (pathProperties == null) {
|
||||
@@ -135,7 +121,6 @@ public class WriteJson {
|
||||
// render all the properties and invoke lazy loading if required
|
||||
BeanProperty[] props = desc.propertiesNonTransient();
|
||||
for (int j = 0; j < props.length; j++) {
|
||||
System.out.println("bean "+ currentBean+" prop:"+props[j]);
|
||||
if (isIncludeProperty(props[j])) {
|
||||
props[j].jsonWrite(writeJson, currentBean);
|
||||
}
|
||||
@@ -163,20 +148,17 @@ public class WriteJson {
|
||||
|
||||
beginAssocMany(name);
|
||||
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
EntityBean o = (EntityBean) it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
d.jsonWrite(this, o, null);
|
||||
for (Object bean : c) {
|
||||
BeanDescriptor<?> d = getDescriptor(bean.getClass());
|
||||
d.jsonWrite(this, (EntityBean)bean, null);
|
||||
}
|
||||
endAssocMany();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
private <T> BeanDescriptor<T> getDescriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null) {
|
||||
String msg = "No BeanDescriptor found for " + cls;
|
||||
throw new RuntimeException(msg);
|
||||
throw new RuntimeException("No BeanDescriptor found for " + cls);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -42,13 +42,13 @@ public class ScalarTypeByte extends ScalarTypeBase<Byte> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
throw new IOException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
throw new TextException("Not supported");
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
throw new IOException("Not supported");
|
||||
}
|
||||
|
||||
public String formatValue(Byte t) {
|
||||
|
||||
Reference in new issue
Block a user