mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
WIP json refactor - initial tidy
This commit is contained in:
@@ -8,36 +8,92 @@ import java.util.Map;
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
|
||||
/**
|
||||
* Utility that converts between JSON content and java Maps/Lists.
|
||||
*/
|
||||
public class EJson {
|
||||
|
||||
/**
|
||||
* Write the nested Map/List as json.
|
||||
*/
|
||||
public static String write(Object object) {
|
||||
return EJsonWriter.write(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the nested Map/List as json to the writer.
|
||||
*/
|
||||
public static void write(Object object, Writer writer) {
|
||||
EJsonWriter.write(object, writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the nested Map/List as json to the jsonGenerator.
|
||||
*/
|
||||
public static void write(Object object, JsonGenerator jsonGenerator) {
|
||||
EJsonWriter.write(object, jsonGenerator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a Map.
|
||||
*/
|
||||
public static Map<String,Object> parseObject(String json) {
|
||||
return EJsonReader.parseObject(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a Map taking a reader.
|
||||
*/
|
||||
public static Map<String,Object> parseObject(Reader reader) {
|
||||
return EJsonReader.parseObject(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a Map taking a JsonParser.
|
||||
*/
|
||||
public static Map<String,Object> parseObject(JsonParser parser) {
|
||||
return EJsonReader.parseObject(parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List.
|
||||
*/
|
||||
public static List<Object> parseList(String json) {
|
||||
return EJsonReader.parseList(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List taking a Reader.
|
||||
*/
|
||||
public static List<Object> parseList(Reader reader) {
|
||||
return EJsonReader.parseList(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List taking a JsonParser.
|
||||
*/
|
||||
public static List<Object> parseList(JsonParser parser) {
|
||||
return EJsonReader.parseList(parser);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List or Map.
|
||||
*/
|
||||
public static Object parse(String json) {
|
||||
return EJsonReader.parse(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List or Map.
|
||||
*/
|
||||
public static Object parse(Reader reader) {
|
||||
return EJsonReader.parse(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the json and return as a List or Map.
|
||||
*/
|
||||
public static Object parse(JsonParser parser) {
|
||||
return EJsonReader.parse(parser);
|
||||
}
|
||||
|
||||
@@ -12,27 +12,47 @@ import javax.json.Json;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
public class EJsonReader {
|
||||
class EJsonReader {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> parseObject(String json) {
|
||||
static Map<String, Object> parseObject(String json) {
|
||||
return (Map<String, Object>) parse(json);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Object> parseList(String json) {
|
||||
return (List<Object>) parse(json);
|
||||
static Map<String, Object> parseObject(Reader reader) {
|
||||
return (Map<String, Object>) parse(reader);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static Map<String, Object> parseObject(JsonParser parser) {
|
||||
return (Map<String, Object>) parse(parser);
|
||||
}
|
||||
|
||||
public static Object parse(String json) {
|
||||
@SuppressWarnings("unchecked")
|
||||
static List<Object> parseList(String json) {
|
||||
return (List<Object>) parse(json);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static List<Object> parseList(Reader reader) {
|
||||
return (List<Object>) parse(reader);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static List<Object> parseList(JsonParser parser) {
|
||||
return (List<Object>) parse(parser);
|
||||
}
|
||||
|
||||
static Object parse(String json) {
|
||||
return parse(new StringReader(json));
|
||||
}
|
||||
|
||||
public static Object parse(Reader reader) {
|
||||
static Object parse(Reader reader) {
|
||||
return parse(Json.createParser(reader));
|
||||
}
|
||||
|
||||
public static Object parse(JsonParser parser) {
|
||||
static Object parse(JsonParser parser) {
|
||||
return new EJsonReader(parser).parseJson();
|
||||
}
|
||||
|
||||
@@ -67,7 +87,11 @@ public class EJsonReader {
|
||||
|
||||
private void end() {
|
||||
if (!stack.isEmpty()) {
|
||||
currentContext = stack.pop();
|
||||
|
||||
//if (currentContext != null) {
|
||||
// Object value = currentContext.getValue();
|
||||
//}
|
||||
currentContext = stack.pop(currentContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +157,7 @@ public class EJsonReader {
|
||||
return Boolean.FALSE;
|
||||
|
||||
default:
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,12 +227,13 @@ public class EJsonReader {
|
||||
}
|
||||
}
|
||||
|
||||
private Context pop() {
|
||||
private Context pop(Context endingContext) {
|
||||
if (head == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
Context temp = head;
|
||||
head = head.next;
|
||||
temp.popContext(endingContext);
|
||||
return temp;
|
||||
}
|
||||
|
||||
@@ -219,6 +244,7 @@ public class EJsonReader {
|
||||
|
||||
private static abstract class Context {
|
||||
Context next;
|
||||
abstract void popContext(Context temp);
|
||||
abstract Object getValue();
|
||||
abstract void setKey(String key);
|
||||
abstract void setValue(Object value);
|
||||
@@ -231,6 +257,10 @@ public class EJsonReader {
|
||||
|
||||
private String key;
|
||||
|
||||
public void popContext(Context temp) {
|
||||
setValue(temp.getValue());
|
||||
}
|
||||
|
||||
Object getValue() {
|
||||
return map;
|
||||
}
|
||||
@@ -252,6 +282,10 @@ public class EJsonReader {
|
||||
|
||||
private final List<Object> values = new ArrayList<Object>();
|
||||
|
||||
public void popContext(Context temp) {
|
||||
values.add(temp.getValue());
|
||||
}
|
||||
|
||||
Object getValue() {
|
||||
return values;
|
||||
}
|
||||
|
||||
@@ -15,19 +15,19 @@ import javax.json.stream.JsonGenerator;
|
||||
|
||||
class EJsonWriter {
|
||||
|
||||
public static String write(Object object) {
|
||||
static String write(Object object) {
|
||||
StringWriter writer = new StringWriter(200);
|
||||
write(object, writer);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
public static void write(Object object, Writer writer) {
|
||||
static void write(Object object, Writer writer) {
|
||||
JsonGenerator generator = Json.createGenerator(writer);
|
||||
write(object, generator);
|
||||
generator.close();
|
||||
}
|
||||
|
||||
public static void write(Object object, JsonGenerator jsonGenerator) {
|
||||
static void write(Object object, JsonGenerator jsonGenerator) {
|
||||
new EJsonWriter(jsonGenerator).writeJson(object);
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ class EJsonWriter {
|
||||
|
||||
private void writeCollection(String name, Collection<Object> collection) {
|
||||
if (name == null) {
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeStartArray();
|
||||
} else {
|
||||
jsonGenerator.writeStartArray(name);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ class PathPropertiesParser {
|
||||
case '(':
|
||||
return currentWord();
|
||||
default:
|
||||
if (pos == 1) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
} while (pos < eof);
|
||||
throw new RuntimeException("Hit EOF while reading sectionTitle from " + startPos);
|
||||
@@ -91,6 +94,10 @@ class PathPropertiesParser {
|
||||
}
|
||||
|
||||
} while (pos < eof);
|
||||
if (startPos < pos) {
|
||||
String currentWord = source.substring(startPos, pos);
|
||||
currentPathProps.addProperty(currentWord);
|
||||
}
|
||||
}
|
||||
|
||||
private void addSubpath() {
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.bean.BeanCollectionAdd;
|
||||
import com.avaje.ebean.bean.BeanCollectionLoader;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Helper functions for performing tasks on Lists Sets or Maps.
|
||||
@@ -62,6 +62,6 @@ public interface BeanCollectionHelp<T> {
|
||||
/**
|
||||
* Write the collection out as json.
|
||||
*/
|
||||
public void jsonWrite(WriteJsonContext ctx, String name, Object collection, boolean explicitInclude);
|
||||
public void jsonWrite(WriteJson ctx, String name, Object collection, boolean explicitInclude);
|
||||
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ import com.avaje.ebean.event.BeanPersistListener;
|
||||
import com.avaje.ebean.event.BeanQueryAdapter;
|
||||
import com.avaje.ebean.meta.MetaBeanInfo;
|
||||
import com.avaje.ebean.meta.MetaQueryPlanStatistic;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlan;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
@@ -63,10 +61,7 @@ import com.avaje.ebeaninternal.server.query.CQueryPlan;
|
||||
import com.avaje.ebeaninternal.server.query.CQueryPlanStats.Snapshot;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
import com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext.ReadBeanState;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext.WriteBeanState;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
import com.avaje.ebeaninternal.server.type.TypeManager;
|
||||
import com.avaje.ebeaninternal.util.SortByClause;
|
||||
@@ -2121,84 +2116,16 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
return propertiesLocal;
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, EntityBean bean) {
|
||||
|
||||
if (bean != null) {
|
||||
|
||||
ctx.appendObjectBegin();
|
||||
WriteBeanState prevState = ctx.pushBeanState(bean);
|
||||
|
||||
if (inheritInfo != null) {
|
||||
InheritInfo localInheritInfo = inheritInfo.readType(bean.getClass());
|
||||
String discValue = localInheritInfo.getDiscriminatorStringValue();
|
||||
String discColumn = localInheritInfo.getDiscriminatorColumn();
|
||||
ctx.appendDiscriminator(discColumn, discValue);
|
||||
|
||||
BeanDescriptor<?> localDescriptor = localInheritInfo.getBeanDescriptor();
|
||||
localDescriptor.jsonWriteProperties(ctx, bean);
|
||||
|
||||
} else {
|
||||
jsonWriteProperties(ctx, bean);
|
||||
}
|
||||
|
||||
ctx.pushPreviousState(prevState);
|
||||
ctx.appendObjectEnd();
|
||||
}
|
||||
public void jsonWrite(WriteJson writeJson, EntityBean bean) {
|
||||
jsonHelp.jsonWrite(writeJson, bean, null);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJson writeJson, EntityBean bean, String key) {
|
||||
jsonHelp.jsonWrite(writeJson, bean, key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void jsonWriteProperties(WriteJsonContext ctx, EntityBean bean) {
|
||||
|
||||
JsonWriteBeanVisitor<T> beanVisitor = (JsonWriteBeanVisitor<T>) ctx.getBeanVisitor();
|
||||
|
||||
Set<String> props = ctx.getIncludeProperties();
|
||||
|
||||
boolean explicitAllProps;
|
||||
if (props == null) {
|
||||
explicitAllProps = false;
|
||||
} else {
|
||||
explicitAllProps = props.contains("*");
|
||||
if (explicitAllProps || props.isEmpty()) {
|
||||
props = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (idProperty != null) {
|
||||
Object idValue = idProperty.getValue(bean);
|
||||
if (idValue != null) {
|
||||
if (props == null || props.contains(idProperty.getName())) {
|
||||
idProperty.jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!explicitAllProps && props == null) {
|
||||
// just render the loaded properties
|
||||
props = ((EntityBean)bean)._ebean_getIntercept().getLoadedPropertyNames();
|
||||
}
|
||||
if (props != null) {
|
||||
// render only the appropriate properties (when not all properties)
|
||||
for (String prop : props) {
|
||||
BeanProperty p = getBeanProperty(prop);
|
||||
if (p != null && !p.isId()) {
|
||||
p.jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (explicitAllProps || !isReference(bean._ebean_getIntercept())) {
|
||||
// render all the properties and invoke lazy loading if required
|
||||
for (int j = 0; j < propertiesNonTransient.length; j++) {
|
||||
propertiesNonTransient[j].jsonWrite(ctx, bean);
|
||||
}
|
||||
for (int j = 0; j < propertiesTransient.length; j++) {
|
||||
propertiesTransient[j].jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (beanVisitor != null) {
|
||||
beanVisitor.visit((T) bean, ctx);
|
||||
}
|
||||
protected void jsonWriteProperties(WriteJson writeJson, EntityBean bean) {
|
||||
jsonHelp.jsonWriteProperties(writeJson, bean);
|
||||
}
|
||||
|
||||
public T jsonRead(JsonParser parser, String path) {
|
||||
|
||||
@@ -1,111 +1,56 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext.WriteBeanState;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson.WriteBean;
|
||||
|
||||
public class BeanDescriptorJsonHelp<T> {
|
||||
|
||||
private final BeanDescriptor<T> desc;
|
||||
|
||||
private final InheritInfo inheritInfo;
|
||||
|
||||
public BeanDescriptorJsonHelp(BeanDescriptor<T> desc) {
|
||||
this.desc = desc;
|
||||
this.inheritInfo = desc.inheritInfo;
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJson writeJson, EntityBean bean, String key) {
|
||||
|
||||
// if (writeJson.hasBean()) {
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, EntityBean bean) {
|
||||
|
||||
if (bean != null) {
|
||||
|
||||
ctx.writeStartObject();
|
||||
writeJson.writeStartObject(key);
|
||||
//WriteBeanState prevState = ctx.pushBeanState(bean);
|
||||
|
||||
if (inheritInfo != null) {
|
||||
if (inheritInfo == null) {
|
||||
jsonWriteProperties(writeJson, bean);
|
||||
|
||||
} else {
|
||||
InheritInfo localInheritInfo = inheritInfo.readType(bean.getClass());
|
||||
String discValue = localInheritInfo.getDiscriminatorStringValue();
|
||||
String discColumn = localInheritInfo.getDiscriminatorColumn();
|
||||
ctx.write(discColumn, discValue);
|
||||
//ctx.appendDiscriminator(discColumn, discValue);
|
||||
writeJson.gen().write(discColumn, discValue);
|
||||
|
||||
BeanDescriptor<?> localDescriptor = localInheritInfo.getBeanDescriptor();
|
||||
localDescriptor.jsonWriteProperties(ctx, bean);
|
||||
|
||||
} else {
|
||||
jsonWriteProperties(ctx, bean);
|
||||
}
|
||||
localDescriptor.jsonWriteProperties(writeJson, bean);
|
||||
}
|
||||
|
||||
//ctx.pushPreviousState(prevState);
|
||||
//ctx.appendObjectEnd();
|
||||
ctx.writeEnd();
|
||||
}
|
||||
writeJson.gen().writeEnd();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void jsonWriteProperties(JsonGenerator ctx, EntityBean bean) {
|
||||
protected void jsonWriteProperties(WriteJson writeJson, EntityBean bean) {
|
||||
|
||||
//JsonWriteBeanVisitor<T> beanVisitor = (JsonWriteBeanVisitor<T>) ctx.getBeanVisitor();
|
||||
|
||||
Set<String> props = ctx.getIncludeProperties();
|
||||
|
||||
boolean explicitAllProps;
|
||||
if (props == null) {
|
||||
explicitAllProps = false;
|
||||
} else {
|
||||
explicitAllProps = props.contains("*");
|
||||
if (explicitAllProps || props.isEmpty()) {
|
||||
props = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (desc.idProperty != null) {
|
||||
Object idValue = desc.idProperty.getValue(bean);
|
||||
if (idValue != null) {
|
||||
if (props == null || props.contains(idProperty.getName())) {
|
||||
idProperty.jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!explicitAllProps && props == null) {
|
||||
// just render the loaded properties
|
||||
props = ((EntityBean)bean)._ebean_getIntercept().getLoadedPropertyNames();
|
||||
}
|
||||
if (props != null) {
|
||||
// render only the appropriate properties (when not all properties)
|
||||
for (String prop : props) {
|
||||
BeanProperty p = getBeanProperty(prop);
|
||||
if (p != null && !p.isId()) {
|
||||
p.jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (explicitAllProps || !isReference(bean._ebean_getIntercept())) {
|
||||
// render all the properties and invoke lazy loading if required
|
||||
for (int j = 0; j < propertiesNonTransient.length; j++) {
|
||||
propertiesNonTransient[j].jsonWrite(ctx, bean);
|
||||
}
|
||||
for (int j = 0; j < propertiesTransient.length; j++) {
|
||||
propertiesTransient[j].jsonWrite(ctx, bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (beanVisitor != null) {
|
||||
beanVisitor.visit((T) bean, ctx);
|
||||
}
|
||||
|
||||
WriteBean writeBean = writeJson.createWriteBean(desc, bean);
|
||||
writeBean.write(writeJson);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T jsonRead(JsonParser parser, String path) {
|
||||
|
||||
@@ -124,7 +69,6 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
return jsonReadObject(parser, path);
|
||||
}
|
||||
|
||||
|
||||
// check for the discriminator value to determine the correct sub type
|
||||
String discColumn = inheritInfo.getRoot().getDiscriminatorColumn();
|
||||
|
||||
@@ -135,6 +79,13 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
|
||||
String propName = parser.getString();
|
||||
if (!propName.equalsIgnoreCase(discColumn)) {
|
||||
// just try to assume this is the correct bean type in the inheritance
|
||||
BeanProperty property = desc.getBeanProperty(propName);
|
||||
if (property != null) {
|
||||
EntityBean bean = desc.createEntityBean();
|
||||
property.jsonRead(parser, bean);
|
||||
return jsonReadProperties(parser, bean);
|
||||
}
|
||||
String msg = "Error reading inheritance discriminator, expected property ["+discColumn+"] but got [" + propName + "] ?";
|
||||
throw new TextException(msg);
|
||||
}
|
||||
@@ -152,12 +103,17 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
return (T) localDescriptor.jsonReadObject(parser, path);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T jsonReadObject(JsonParser parser, String path) {
|
||||
|
||||
EntityBean bean = desc.createEntityBean();
|
||||
//ctx.pushBean(bean, path, this);
|
||||
|
||||
return jsonReadProperties(parser, bean);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T jsonReadProperties(JsonParser parser, EntityBean bean) {
|
||||
|
||||
do {
|
||||
|
||||
if (parser.hasNext()) {
|
||||
@@ -169,7 +125,7 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
p.jsonRead(parser, bean);
|
||||
|
||||
} else {
|
||||
Object rawValue = EJson.parse(parser);
|
||||
//Object rawValue = EJson.parse(parser);
|
||||
// unknown property key ...
|
||||
//ctx.readUnmappedJson(propName);
|
||||
}
|
||||
@@ -183,7 +139,6 @@ public class BeanDescriptorJsonHelp<T> {
|
||||
}
|
||||
|
||||
} while (true);
|
||||
|
||||
return (T)bean;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.avaje.ebean.bean.BeanCollectionAdd;
|
||||
import com.avaje.ebean.bean.BeanCollectionLoader;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.common.BeanList;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Helper object for dealing with Lists.
|
||||
@@ -128,7 +128,7 @@ public final class BeanListHelp<T> implements BeanCollectionHelp<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, String name, Object collection, boolean explicitInclude) {
|
||||
public void jsonWrite(WriteJson ctx, String name, Object collection, boolean explicitInclude) {
|
||||
|
||||
List<?> list;
|
||||
if (collection instanceof BeanCollection<?>) {
|
||||
@@ -147,15 +147,11 @@ public final class BeanListHelp<T> implements BeanCollectionHelp<T> {
|
||||
list = (List<?>) collection;
|
||||
}
|
||||
|
||||
ctx.beginAssocMany(name);
|
||||
ctx.gen().writeStartArray(name);
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if (j > 0) {
|
||||
ctx.appendComma();
|
||||
}
|
||||
Object detailBean = list.get(j);
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean)detailBean);
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean)list.get(j));
|
||||
}
|
||||
ctx.endAssocMany();
|
||||
ctx.gen().writeEnd();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import com.avaje.ebean.bean.BeanCollectionAdd;
|
||||
import com.avaje.ebean.bean.BeanCollectionLoader;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.common.BeanMap;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Helper specifically for dealing with Maps.
|
||||
@@ -156,7 +156,7 @@ public final class BeanMapHelp<T> implements BeanCollectionHelp<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, String name, Object collection, boolean explicitInclude) {
|
||||
public void jsonWrite(WriteJson ctx, String name, Object collection, boolean explicitInclude) {
|
||||
|
||||
Map<?,?> map;
|
||||
if (collection instanceof BeanCollection<?>){
|
||||
@@ -175,19 +175,14 @@ public final class BeanMapHelp<T> implements BeanCollectionHelp<T> {
|
||||
map = (Map<?,?>)collection;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
ctx.beginAssocMany(name);
|
||||
ctx.gen().writeStartArray(name);
|
||||
Iterator<?> it = map.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Entry<?, ?> entry = (Entry<?, ?>)it.next();
|
||||
if (count++ > 0){
|
||||
ctx.appendComma();
|
||||
}
|
||||
//FIXME: json write map key ...
|
||||
Object detailBean = entry.getValue();
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean)detailBean);
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean) entry.getValue());
|
||||
}
|
||||
ctx.endAssocMany();
|
||||
ctx.gen().writeEnd();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.sql.Types;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
import javax.persistence.PersistenceException;
|
||||
@@ -32,7 +31,7 @@ import com.avaje.ebeaninternal.server.query.SqlBeanLoad;
|
||||
import com.avaje.ebeaninternal.server.query.SqlJoinType;
|
||||
import com.avaje.ebeaninternal.server.reflect.BeanReflectGetter;
|
||||
import com.avaje.ebeaninternal.server.reflect.BeanReflectSetter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.type.DataBind;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
|
||||
@@ -1174,54 +1173,18 @@ public class BeanProperty implements ElPropertyValue {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void jsonWrite(JsonGenerator ctx, EntityBean bean) {
|
||||
if(!jsonSerialize){
|
||||
return;
|
||||
}
|
||||
Object value = getValueIntercept(bean);
|
||||
if (value == null) {
|
||||
ctx.writeNull(name);
|
||||
} else {
|
||||
scalarType.jsonWrite(ctx, name, value);
|
||||
//ctx.appendNameValue(name, scalarType, value);
|
||||
}
|
||||
public void jsonWrite(WriteJson writeJson, EntityBean bean) {
|
||||
if (!jsonSerialize) {
|
||||
return;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void jsonWrite(WriteJsonContext ctx, EntityBean bean) {
|
||||
if(!jsonSerialize){
|
||||
return;
|
||||
}
|
||||
Object value = getValueIntercept(bean);
|
||||
if (value == null) {
|
||||
ctx.appendNull(name);
|
||||
} else {
|
||||
ctx.appendNameValue(name, scalarType, value);
|
||||
}
|
||||
Object value = getValueIntercept(bean);
|
||||
if (value == null) {
|
||||
writeJson.gen().writeNull(name);
|
||||
} else {
|
||||
scalarType.jsonWrite(writeJson.gen(), name, value);
|
||||
}
|
||||
}
|
||||
|
||||
// public void jsonRead(ReadJsonContext ctx, EntityBean bean) {
|
||||
// if(!jsonDeserialize){
|
||||
// return;
|
||||
// }
|
||||
// String jsonValue;
|
||||
// try {
|
||||
// jsonValue = ctx.readScalarValue();
|
||||
// } catch (TextException e){
|
||||
// throw new TextException("Error reading property "+getFullBeanName(), e);
|
||||
// }
|
||||
// Object objValue;
|
||||
// if (jsonValue == null) {
|
||||
// objValue = null;
|
||||
// } else {
|
||||
// objValue = scalarType.jsonFromString(jsonValue, ctx.getValueAdapter());
|
||||
// }
|
||||
// setValue(bean, objValue);
|
||||
// }
|
||||
|
||||
public void jsonRead(JsonParser ctx, EntityBean bean) {
|
||||
if (!jsonDeserialize) {
|
||||
return;
|
||||
|
||||
@@ -30,9 +30,7 @@ import com.avaje.ebeaninternal.server.el.ElPropertyChainBuilder;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
|
||||
import com.avaje.ebeaninternal.server.query.SqlBeanLoad;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext.ReadBeanState;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Property mapped to a List Set or Map.
|
||||
@@ -883,7 +881,7 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
|
||||
return null != targetDescriptor.getId(otherBean);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, EntityBean bean) {
|
||||
public void jsonWrite(WriteJson ctx, EntityBean bean) {
|
||||
if(!this.jsonSerialize){
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
import com.avaje.ebeaninternal.server.query.SqlBeanLoad;
|
||||
import com.avaje.ebeaninternal.server.query.SqlJoinType;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Property mapped to a joined bean.
|
||||
@@ -832,25 +831,23 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(WriteJsonContext ctx, EntityBean bean) {
|
||||
public void jsonWrite(WriteJson writeJson, EntityBean bean) {
|
||||
|
||||
Object value = getValueIntercept(bean);
|
||||
if (value == null){
|
||||
ctx.beginAssocOneIsNull(name);
|
||||
writeJson.gen().writeNull(name);
|
||||
|
||||
} else {
|
||||
if (ctx.isParentBean(value)){
|
||||
if (writeJson.isParentBean(value)){
|
||||
// bi-directional and already rendered parent
|
||||
|
||||
} else {
|
||||
// Hmmm, not writing complex non-entity bean
|
||||
if (value instanceof EntityBean) {
|
||||
ctx.pushParentBean(bean);
|
||||
ctx.beginAssocOne(name);
|
||||
writeJson.beginAssocOne(name, bean);
|
||||
BeanDescriptor<?> refDesc = descriptor.getBeanDescriptor(value.getClass());
|
||||
refDesc.jsonWrite(ctx, (EntityBean)value);
|
||||
ctx.endAssocOne();
|
||||
ctx.popParentBean();
|
||||
refDesc.jsonWrite(writeJson, (EntityBean)value, name);
|
||||
writeJson.endAssocOne();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.avaje.ebeaninternal.server.el.ElPropertyChainBuilder;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.ebeaninternal.server.query.SqlBeanLoad;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundProperty;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundPropertyElAdapter;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundType;
|
||||
@@ -177,10 +177,10 @@ public class BeanPropertyCompound extends BeanProperty {
|
||||
return bean;
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, EntityBean bean) {
|
||||
public void jsonWrite(WriteJson ctx, EntityBean bean) {
|
||||
|
||||
Object valueObject = getValueIntercept(bean);
|
||||
compoundType.jsonWrite(ctx, valueObject, name);
|
||||
//FIXME: compoundType.jsonWrite(ctx, valueObject, name);
|
||||
}
|
||||
|
||||
public void jsonRead(ReadJsonContext ctx, EntityBean bean){
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.avaje.ebean.bean.BeanCollectionAdd;
|
||||
import com.avaje.ebean.bean.BeanCollectionLoader;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.common.BeanSet;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* Helper specifically for dealing with Sets.
|
||||
@@ -129,7 +129,7 @@ public final class BeanSetHelp<T> implements BeanCollectionHelp<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, String name, Object collection, boolean explicitInclude) {
|
||||
public void jsonWrite(WriteJson ctx, String name, Object collection, boolean explicitInclude) {
|
||||
|
||||
Set<?> set;
|
||||
if (collection instanceof BeanCollection<?>){
|
||||
@@ -148,16 +148,11 @@ public final class BeanSetHelp<T> implements BeanCollectionHelp<T> {
|
||||
set = (Set<?>)collection;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
ctx.beginAssocMany(name);
|
||||
ctx.gen().writeStartArray(name);
|
||||
Iterator<?> it = set.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object detailBean = it.next();
|
||||
if (count++ > 0){
|
||||
ctx.appendComma();
|
||||
}
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean)detailBean);
|
||||
targetDescriptor.jsonWrite(ctx, (EntityBean)it.next());
|
||||
}
|
||||
ctx.endAssocMany();
|
||||
ctx.gen().writeEnd();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
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;
|
||||
@@ -13,10 +14,12 @@ import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
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.JsonElement;
|
||||
@@ -25,7 +28,6 @@ import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.type.EscapeJson;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.ManyType;
|
||||
import com.avaje.ebeaninternal.util.ParamTypeHelper.TypeInfo;
|
||||
@@ -37,293 +39,263 @@ import com.avaje.ebeaninternal.util.ParamTypeHelper.TypeInfo;
|
||||
*/
|
||||
public class DJsonContext implements JsonContext {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
private final JsonValueAdapter dfltValueAdapter;
|
||||
|
||||
private final boolean dfltPretty;
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonValueAdapter dfltValueAdapter, boolean dfltPretty){
|
||||
this.server = server;
|
||||
this.dfltValueAdapter = dfltValueAdapter;
|
||||
this.dfltPretty = dfltPretty;
|
||||
}
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
return server.isSupportedType(genericType);
|
||||
}
|
||||
//private final JsonValueAdapter dfltValueAdapter;
|
||||
|
||||
private JsonParser createReader(Reader jsonReader) {
|
||||
return Json.createParser(jsonReader);
|
||||
//return new ReadJsonSourceReader(jsonReader, 256, 512);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json){
|
||||
return toBean(cls, new StringReader(json), null);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader) {
|
||||
return toBean(cls, createReader(jsonReader), null);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json, JsonReadOptions options){
|
||||
return toBean(cls, new StringReader(json), options);
|
||||
}
|
||||
private final boolean dfltPretty;
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader, JsonReadOptions options) {
|
||||
return toBean(cls, createReader(jsonReader), options);
|
||||
}
|
||||
public DJsonContext(SpiEbeanServer server, JsonValueAdapter dfltValueAdapter, boolean dfltPretty) {
|
||||
this.server = server;
|
||||
//this.dfltValueAdapter = dfltValueAdapter;
|
||||
this.dfltPretty = dfltPretty;
|
||||
}
|
||||
|
||||
// private <T> T toBean(Class<T> cls, ReadJsonSource src, JsonReadOptions options){
|
||||
//
|
||||
// BeanDescriptor<T> d = getDecriptor(cls);
|
||||
// ReadJsonContext ctx = new ReadJsonContext(src, dfltValueAdapter, options);
|
||||
// return d.jsonReadBean(ctx, null);
|
||||
// }
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
return server.isSupportedType(genericType);
|
||||
}
|
||||
|
||||
private <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) {
|
||||
|
||||
private JsonParser createReader(Reader jsonReader) {
|
||||
return Json.createParser(jsonReader);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json) {
|
||||
return toBean(cls, new StringReader(json), null);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader) {
|
||||
return toBean(cls, createReader(jsonReader), null);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json, JsonReadOptions options) {
|
||||
return toBean(cls, new StringReader(json), options);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader, JsonReadOptions options) {
|
||||
return toBean(cls, createReader(jsonReader), options);
|
||||
}
|
||||
|
||||
private <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) {
|
||||
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
return d.jsonRead(parser, null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json) {
|
||||
return toList(cls, new StringReader(json), null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json, JsonReadOptions options) {
|
||||
return toList(cls, new StringReader(json), options);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader) {
|
||||
return toList(cls, createReader(jsonReader), null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader, JsonReadOptions options) {
|
||||
return toList(cls, createReader(jsonReader), options);
|
||||
}
|
||||
|
||||
private <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options) {
|
||||
|
||||
try {
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
// ReadJsonContext ctx = new ReadJsonContext(src, dfltValueAdapter, options);
|
||||
return d.jsonRead(parser, null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json){
|
||||
return toList(cls, new StringReader(json), null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json, JsonReadOptions options){
|
||||
return toList(cls, new StringReader(json), options);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader){
|
||||
return toList(cls, createReader(jsonReader), null);
|
||||
}
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader, JsonReadOptions options){
|
||||
return toList(cls, createReader(jsonReader), options);
|
||||
}
|
||||
|
||||
private <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options){
|
||||
if (!src.hasNext()) {
|
||||
return list;
|
||||
}
|
||||
Event event = src.next();
|
||||
if (event != Event.START_ARRAY) {
|
||||
throw new TextException("Expecting start_array event but got [" + event + "] at [" + src.getLocation() + "]");
|
||||
}
|
||||
|
||||
try {
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
//ReadJsonContext ctx = new ReadJsonContext(src, dfltValueAdapter, options);
|
||||
if (!src.hasNext()) {
|
||||
return list;
|
||||
}
|
||||
Event event = src.next();
|
||||
if (event != Event.START_ARRAY) {
|
||||
throw new TextException("Expecting start_array event but got ["+event+"] at ["+src.getLocation()+"]");
|
||||
}
|
||||
//ctx.readArrayBegin();
|
||||
do {
|
||||
T bean = d.jsonRead(src, null);
|
||||
if (bean == null){
|
||||
break;
|
||||
} else {
|
||||
list.add(bean);
|
||||
}
|
||||
// if (!ctx.readArrayNext()){
|
||||
// break;
|
||||
// }
|
||||
} while(true);
|
||||
|
||||
return list;
|
||||
|
||||
} catch (RuntimeException e){
|
||||
throw new TextException("Error parsing "+src, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object toObject(Type genericType, String json, JsonReadOptions options) {
|
||||
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)){
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
|
||||
default:
|
||||
String msg = "ManyType "+manyType+" not supported yet";
|
||||
throw new TextException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public Object toObject(Type genericType, Reader json, JsonReadOptions options) {
|
||||
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)){
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
|
||||
default:
|
||||
String msg = "ManyType "+manyType+" not supported yet";
|
||||
throw new TextException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer) {
|
||||
toJsonWriter(o, writer, dfltPretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty) {
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options){
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options, String callback) {
|
||||
toJsonInternal(o, new WriteJsonBufferWriter(writer), pretty, options, callback);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o){
|
||||
return toJsonString(o, dfltPretty, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty){
|
||||
return toJsonString(o, pretty, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options){
|
||||
return toJsonString(o, pretty, options, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options, String callback){
|
||||
WriteJsonBufferString b = new WriteJsonBufferString();
|
||||
toJsonInternal(o, b, pretty, options, callback);
|
||||
return b.getBufferOutput();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void toJsonInternal(Object o, WriteJsonBuffer buffer, boolean pretty, JsonWriteOptions options, String requestCallback){
|
||||
|
||||
if (o == null){
|
||||
buffer.append("null");
|
||||
} else if (o instanceof Number) {
|
||||
buffer.append(o.toString());
|
||||
} else if (o instanceof Boolean) {
|
||||
buffer.append(o.toString());
|
||||
} else if (o instanceof String) {
|
||||
EscapeJson.escapeQuote(o.toString(), buffer);
|
||||
} else if (o instanceof JsonElement) {
|
||||
|
||||
} else if (o instanceof Map<?,?>){
|
||||
toJsonFromMap((Map<Object,Object>)o, buffer, pretty, options, requestCallback);
|
||||
|
||||
} else if (o instanceof Collection<?>){
|
||||
toJsonFromCollection((Collection<?>)o, buffer, pretty, options, requestCallback);
|
||||
|
||||
do {
|
||||
T bean = d.jsonRead(src, null);
|
||||
if (bean == null) {
|
||||
break;
|
||||
} else {
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
|
||||
d.jsonWrite(ctx, (EntityBean)o);
|
||||
ctx.end();
|
||||
list.add(bean);
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return list;
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
throw new TextException("Error parsing " + src, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> c, WriteJsonBuffer buffer, boolean pretty, JsonWriteOptions options, String requestCallback){
|
||||
|
||||
Iterator<T> it = c.iterator();
|
||||
if (!it.hasNext()){
|
||||
buffer.append("[]");
|
||||
return;
|
||||
}
|
||||
|
||||
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
|
||||
public Object toObject(Type genericType, String json, JsonReadOptions options) {
|
||||
|
||||
Object o = it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
|
||||
ctx.appendArrayBegin();
|
||||
d.jsonWrite(ctx, (EntityBean)o);
|
||||
while (it.hasNext()) {
|
||||
ctx.appendComma();
|
||||
T t = it.next();
|
||||
d.jsonWrite(ctx, (EntityBean)t);
|
||||
}
|
||||
ctx.appendArrayEnd();
|
||||
ctx.end();
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)) {
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
private void toJsonFromMap(Map<Object,Object> map, WriteJsonBuffer buffer, boolean pretty, JsonWriteOptions options, String requestCallback){
|
||||
|
||||
if (map.isEmpty()){
|
||||
buffer.append("{}");
|
||||
return;
|
||||
}
|
||||
|
||||
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
|
||||
Set<Entry<Object,Object>> entrySet = map.entrySet();
|
||||
Iterator<Entry<Object, Object>> it = entrySet.iterator();
|
||||
|
||||
Entry<Object, Object> entry = it.next();
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
|
||||
ctx.appendObjectBegin();
|
||||
toJsonMapKey(buffer, false, entry.getKey());
|
||||
toJsonMapValue(buffer, pretty, options, requestCallback, entry.getValue());
|
||||
|
||||
while (it.hasNext()) {
|
||||
entry = it.next();
|
||||
ctx.appendComma();
|
||||
toJsonMapKey(buffer, pretty, entry.getKey());
|
||||
toJsonMapValue(buffer, pretty, options, requestCallback, entry.getValue());
|
||||
}
|
||||
ctx.appendObjectEnd();
|
||||
ctx.end();
|
||||
default:
|
||||
String msg = "ManyType " + manyType + " not supported yet";
|
||||
throw new TextException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public Object toObject(Type genericType, Reader json, JsonReadOptions options) {
|
||||
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)) {
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
private void toJsonMapKey(WriteJsonBuffer buffer, boolean pretty, Object key) {
|
||||
if (pretty){
|
||||
buffer.append("\n");
|
||||
}
|
||||
buffer.append("\"");
|
||||
buffer.append(key.toString());
|
||||
buffer.append("\":");
|
||||
}
|
||||
|
||||
private void toJsonMapValue(WriteJsonBuffer buffer, boolean pretty, JsonWriteOptions options, String requestCallback,
|
||||
Object value) {
|
||||
|
||||
if (value == null){
|
||||
buffer.append("null");
|
||||
} else {
|
||||
toJsonInternal(value, buffer, pretty, options, requestCallback);
|
||||
}
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
|
||||
default:
|
||||
throw new TextException("ManyType " + manyType + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer) {
|
||||
toJsonWriter(o, writer, dfltPretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty) {
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options) {
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options, String callback) {
|
||||
JsonGenerator generator = Json.createGenerator(writer);
|
||||
toJsonInternal(o, generator, pretty, options, callback);
|
||||
generator.close();
|
||||
}
|
||||
|
||||
public String toJsonString(Object o) {
|
||||
return toJsonString(o, dfltPretty, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty) {
|
||||
return toJsonString(o, pretty, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options) {
|
||||
return toJsonString(o, pretty, options, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options, String callback) {
|
||||
StringWriter writer = new StringWriter(500);
|
||||
JsonGenerator gen = Json.createGenerator(writer);
|
||||
toJsonInternal(o, gen, pretty, options, callback);
|
||||
gen.close();
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void toJsonInternal(Object o, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
|
||||
if (o == null) {
|
||||
gen.writeNull();
|
||||
} else if (o instanceof Number) {
|
||||
gen.write(((Number) o).doubleValue());
|
||||
} else if (o instanceof Boolean) {
|
||||
gen.write(((Boolean) o).booleanValue());
|
||||
} else if (o instanceof String) {
|
||||
gen.write((String) o);
|
||||
|
||||
// } else if (o instanceof JsonElement) {
|
||||
|
||||
} else if (o instanceof Map<?, ?>) {
|
||||
toJsonFromMap((Map<Object, Object>) o, gen, pretty, options, requestCallback);
|
||||
|
||||
} else if (o instanceof Collection<?>) {
|
||||
toJsonFromCollection((Collection<?>) o, null, gen, pretty, options, requestCallback);
|
||||
|
||||
} else if (o instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
WriteJson writeJson = createWriteJson(gen, options);
|
||||
d.jsonWrite(writeJson, (EntityBean)o, null);
|
||||
}
|
||||
}
|
||||
|
||||
private WriteJson createWriteJson(JsonGenerator gen, JsonWriteOptions options) {
|
||||
PathProperties pathProps = (options == null) ? null : options.getPathProperties();
|
||||
return new WriteJson(server, gen, pathProps);
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> c, String key, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
|
||||
if (key == null) {
|
||||
gen.writeStartArray();
|
||||
} else {
|
||||
gen.writeStartArray(key);
|
||||
}
|
||||
|
||||
WriteJson writeJson = createWriteJson(gen, options);
|
||||
|
||||
Iterator<T> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
T t = it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(t.getClass());
|
||||
//writeJson.setBean();
|
||||
d.jsonWrite(writeJson, (EntityBean)t, null);
|
||||
}
|
||||
gen.writeEnd();
|
||||
}
|
||||
|
||||
private void toJsonFromMap(Map<Object, Object> map, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
|
||||
Set<Entry<Object, Object>> entrySet = map.entrySet();
|
||||
Iterator<Entry<Object, Object>> it = entrySet.iterator();
|
||||
|
||||
WriteJson writeJson = createWriteJson(gen, options);
|
||||
gen.writeStartObject();
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null){
|
||||
String msg = "No BeanDescriptor found for "+cls;
|
||||
throw new RuntimeException(msg);
|
||||
while (it.hasNext()) {
|
||||
Entry<Object, Object> entry = it.next();
|
||||
String key = entry.getKey().toString();
|
||||
Object value = entry.getValue();
|
||||
if (value == null) {
|
||||
gen.writeNull(key);
|
||||
} else {
|
||||
if (value instanceof Collection<?>) {
|
||||
toJsonFromCollection((Collection<?>) value, key, gen, pretty, options, requestCallback);
|
||||
|
||||
} else if (value instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(value.getClass());
|
||||
d.jsonWrite(writeJson,(EntityBean) value, key);
|
||||
|
||||
} else {
|
||||
throw new RuntimeException("TODO process primitive");
|
||||
}
|
||||
return d;
|
||||
}
|
||||
}
|
||||
gen.writeEnd();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null) {
|
||||
throw new RuntimeException("No BeanDescriptor found for " + cls);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,23 +4,23 @@ import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
|
||||
public class PathStack extends ArrayStack<String> {
|
||||
|
||||
public String peekFullPath(String key){
|
||||
|
||||
String prefix = peekWithNull();
|
||||
if (prefix != null){
|
||||
return prefix+"."+key;
|
||||
} else {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public void pushPathKey(String key) {
|
||||
public String peekFullPath(String key) {
|
||||
|
||||
String prefix = peekWithNull();
|
||||
if (prefix != null){
|
||||
key = prefix+"."+key;
|
||||
}
|
||||
push(key);
|
||||
String prefix = peekWithNull();
|
||||
if (prefix != null) {
|
||||
return prefix + "." + key;
|
||||
} else {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public void pushPathKey(String key) {
|
||||
|
||||
String prefix = peekWithNull();
|
||||
if (prefix != null) {
|
||||
key = prefix + "." + key;
|
||||
}
|
||||
push(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
|
||||
import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
|
||||
public class WriteJson {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
private final JsonGenerator generator;
|
||||
private final PathProperties pathProperties;
|
||||
|
||||
private final PathStack pathStack = new PathStack();
|
||||
|
||||
private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
|
||||
public WriteJson(SpiEbeanServer server, JsonGenerator generator, PathProperties pathProperties){
|
||||
this.server = server;
|
||||
this.generator = generator;
|
||||
this.pathProperties = pathProperties;
|
||||
}
|
||||
|
||||
public JsonGenerator gen() {
|
||||
return generator;
|
||||
}
|
||||
|
||||
public boolean isParentBean(Object bean) {
|
||||
if (parentBeans.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
return parentBeans.contains(bean);
|
||||
}
|
||||
}
|
||||
|
||||
public void pushParentBeanMany(Object parentBean) {
|
||||
parentBeans.push(parentBean);
|
||||
}
|
||||
|
||||
public void popParentBeanMany() {
|
||||
parentBeans.pop();
|
||||
}
|
||||
|
||||
public void beginAssocOne(String key, EntityBean bean) {
|
||||
parentBeans.push(bean);
|
||||
pathStack.pushPathKey(key);
|
||||
}
|
||||
|
||||
public void endAssocOne() {
|
||||
parentBeans.pop();
|
||||
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) {
|
||||
return new WriteBean(desc, bean);
|
||||
}
|
||||
|
||||
boolean explicitAllProps = false;
|
||||
Set<String> currentIncludeProps = pathProperties.get(pathStack.peekWithNull());
|
||||
if (currentIncludeProps != null) {
|
||||
explicitAllProps = currentIncludeProps.contains("*");
|
||||
if (explicitAllProps || currentIncludeProps.isEmpty()) {
|
||||
currentIncludeProps = null;
|
||||
}
|
||||
}
|
||||
return new WriteBean(desc, explicitAllProps, currentIncludeProps, bean);
|
||||
}
|
||||
|
||||
public class WriteBean {
|
||||
|
||||
final boolean explicitAllProps;
|
||||
final Set<String> currentIncludeProps;
|
||||
final BeanDescriptor<?> desc;
|
||||
final EntityBean currentBean;
|
||||
|
||||
WriteBean(BeanDescriptor<?> desc, EntityBean currentBean){
|
||||
this(desc, false, null, currentBean);
|
||||
}
|
||||
|
||||
WriteBean(BeanDescriptor<?> desc, boolean explicitAllProps, Set<String> currentIncludeProps, EntityBean currentBean) {
|
||||
super();
|
||||
this.desc = desc;
|
||||
this.currentBean = currentBean;
|
||||
this.explicitAllProps = explicitAllProps;
|
||||
this.currentIncludeProps = currentIncludeProps;
|
||||
}
|
||||
|
||||
private boolean isReferenceOnly() {
|
||||
return !explicitAllProps && currentIncludeProps == null && currentBean._ebean_getIntercept().isReference();
|
||||
}
|
||||
|
||||
private boolean isIncludeProperty(BeanProperty prop) {
|
||||
if (explicitAllProps)
|
||||
return true;
|
||||
if (currentIncludeProps != null) {
|
||||
// explicitly controlled by pathProperties
|
||||
return currentIncludeProps.contains(prop.getName());
|
||||
} else {
|
||||
// include only loaded properties
|
||||
return currentBean._ebean_getIntercept().isLoadedProperty(prop.getPropertyIndex());
|
||||
}
|
||||
}
|
||||
|
||||
public void write(WriteJson writeJson) {
|
||||
//EntityBean bean = writeJson.getBean();
|
||||
BeanProperty beanProp = desc.getIdProperty();
|
||||
if (beanProp != null) {
|
||||
if (isIncludeProperty(beanProp)) {
|
||||
beanProp.jsonWrite(writeJson, currentBean);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isReferenceOnly()) {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
props = desc.propertiesTransient();
|
||||
for (int j = 0; j < props.length; j++) {
|
||||
if (isIncludeProperty(props[j])) {
|
||||
props[j].jsonWrite(writeJson, currentBean);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Boolean includeMany(String key) {
|
||||
if (pathProperties != null) {
|
||||
String fullPath = pathStack.peekFullPath(key);
|
||||
return pathProperties.hasPath(fullPath);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void toJson(String name, Collection<?> c) {
|
||||
|
||||
beginAssocMany(name);
|
||||
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
EntityBean o = (EntityBean) it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
d.jsonWrite(this, o, null);
|
||||
}
|
||||
endAssocMany();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null) {
|
||||
String msg = "No BeanDescriptor found for " + cls;
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
public void beginAssocMany(String key) {
|
||||
pathStack.pushPathKey(key);
|
||||
generator.writeStartArray(key);
|
||||
}
|
||||
|
||||
public void endAssocMany() {
|
||||
pathStack.pop();
|
||||
generator.writeEnd();
|
||||
}
|
||||
|
||||
public void writeStartObject(String key) {
|
||||
if (key == null) {
|
||||
generator.writeStartObject();
|
||||
} else {
|
||||
generator.writeStartObject(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,376 +1,376 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebean.text.json.JsonWriter;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.type.EscapeJson;
|
||||
import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
//import java.util.Collection;
|
||||
//import java.util.Iterator;
|
||||
//import java.util.Map;
|
||||
//import java.util.Set;
|
||||
//
|
||||
//import com.avaje.ebean.bean.EntityBean;
|
||||
//import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
//import com.avaje.ebean.text.PathProperties;
|
||||
//import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
//import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
//import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
//import com.avaje.ebean.text.json.JsonWriter;
|
||||
//import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
//import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
//import com.avaje.ebeaninternal.server.type.EscapeJson;
|
||||
//import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
//import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
|
||||
|
||||
public class WriteJsonContext implements JsonWriter {
|
||||
public class WriteJsonContext {//implements JsonWriter {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
private final WriteJsonBuffer buffer;
|
||||
|
||||
private final boolean pretty;
|
||||
|
||||
private final JsonValueAdapter valueAdapter;
|
||||
|
||||
private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
|
||||
private final PathProperties pathProperties;
|
||||
|
||||
private final Map<String, JsonWriteBeanVisitor<?>> visitorMap;
|
||||
|
||||
private final String callback;
|
||||
|
||||
private final PathStack pathStack;
|
||||
|
||||
private WriteBeanState beanState;
|
||||
|
||||
private int depthOffset;
|
||||
|
||||
boolean assocOne;
|
||||
|
||||
public WriteJsonContext(WriteJsonBuffer buffer, boolean pretty, JsonValueAdapter dfltValueAdapter,
|
||||
JsonWriteOptions options, String requestCallback, SpiEbeanServer server){
|
||||
|
||||
this.server = server;
|
||||
this.buffer = buffer;
|
||||
this.pretty = pretty;
|
||||
this.pathStack = new PathStack();
|
||||
this.callback = getCallback(requestCallback, options);
|
||||
if (options == null){
|
||||
this.valueAdapter = dfltValueAdapter;
|
||||
this.visitorMap = null;
|
||||
this.pathProperties = null;
|
||||
|
||||
} else {
|
||||
this.valueAdapter = getValueAdapter(dfltValueAdapter, options.getValueAdapter());
|
||||
this.visitorMap = emptyToNull(options.getVisitorMap());
|
||||
this.pathProperties = emptyToNull(options.getPathProperties());
|
||||
}
|
||||
|
||||
if (callback != null){
|
||||
buffer.append(requestCallback).append("(");
|
||||
}
|
||||
}
|
||||
|
||||
public void toJson(String name, Collection<?> c) {
|
||||
|
||||
beginAssocMany(name);
|
||||
|
||||
Iterator<?> it = c.iterator();
|
||||
if (!it.hasNext()){
|
||||
endAssocMany();
|
||||
return;
|
||||
}
|
||||
|
||||
EntityBean o = (EntityBean)it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
|
||||
d.jsonWrite(this, o);
|
||||
while (it.hasNext()) {
|
||||
appendComma();
|
||||
EntityBean t = (EntityBean)it.next();
|
||||
d.jsonWrite(this, t);
|
||||
}
|
||||
endAssocMany();
|
||||
}
|
||||
|
||||
private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
if (d == null){
|
||||
String msg = "No BeanDescriptor found for "+cls;
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
public void appendRawValue(String key, String rawJsonValue) {
|
||||
appendKeyWithComma(key, true);
|
||||
buffer.append(rawJsonValue);
|
||||
}
|
||||
|
||||
public void appendQuoteEscapeValue(String key, String valueToEscape) {
|
||||
appendKeyWithComma(key, true);
|
||||
EscapeJson.escapeQuote(valueToEscape, buffer);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
if (callback != null){
|
||||
buffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
private <MK,MV> Map<MK,MV> emptyToNull(Map<MK,MV> m){
|
||||
if ( m == null || m.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
private PathProperties emptyToNull(PathProperties m){
|
||||
if ( m == null || m.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
private String getCallback(String requestCallback, JsonWriteOptions options) {
|
||||
if (requestCallback != null){
|
||||
return requestCallback;
|
||||
}
|
||||
if (options != null){
|
||||
return options.getCallback();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private JsonValueAdapter getValueAdapter(JsonValueAdapter dfltValueAdapter, JsonValueAdapter valueAdapter) {
|
||||
return valueAdapter == null ? dfltValueAdapter : valueAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set of properties to write to JSON. If null is returned then
|
||||
* the default will output the properties loaded for this bean.
|
||||
*/
|
||||
public Set<String> getIncludeProperties() {
|
||||
if (pathProperties != null){
|
||||
String path = pathStack.peekWithNull();
|
||||
return pathProperties.get(path);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public JsonWriteBeanVisitor<?> getBeanVisitor() {
|
||||
if (visitorMap != null){
|
||||
String path = pathStack.peekWithNull();
|
||||
return visitorMap.get(path);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getJson() {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private void appendIndent(){
|
||||
|
||||
buffer.append("\n");
|
||||
int depth = depthOffset + parentBeans.size();
|
||||
for (int i = 0; i < depth; i++) {
|
||||
buffer.append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
public void appendObjectBegin(){
|
||||
if (pretty && !assocOne){
|
||||
appendIndent();
|
||||
}
|
||||
buffer.append("{");
|
||||
}
|
||||
public void appendObjectEnd(){
|
||||
buffer.append("}");
|
||||
}
|
||||
|
||||
public void appendArrayBegin(){
|
||||
if (pretty){
|
||||
appendIndent();
|
||||
}
|
||||
buffer.append("[");
|
||||
depthOffset++;
|
||||
}
|
||||
|
||||
public void appendArrayEnd(){
|
||||
depthOffset--;
|
||||
if (pretty){
|
||||
appendIndent();
|
||||
}
|
||||
buffer.append("]");
|
||||
}
|
||||
|
||||
public void appendComma(){
|
||||
buffer.append(",");
|
||||
}
|
||||
|
||||
public void addDepthOffset(int offset){
|
||||
depthOffset += offset;
|
||||
}
|
||||
|
||||
public void beginAssocOneIsNull(String key) {
|
||||
depthOffset++;
|
||||
internalAppendKeyBegin(key);
|
||||
appendNull();
|
||||
depthOffset--;
|
||||
}
|
||||
|
||||
public void beginAssocOne(String key) {
|
||||
pathStack.pushPathKey(key);
|
||||
|
||||
internalAppendKeyBegin(key);
|
||||
assocOne = true;
|
||||
}
|
||||
|
||||
public void endAssocOne() {
|
||||
|
||||
pathStack.pop();
|
||||
assocOne = false;
|
||||
}
|
||||
|
||||
public Boolean includeMany(String key) {
|
||||
if (pathProperties != null){
|
||||
String fullPath = pathStack.peekFullPath(key);
|
||||
return pathProperties.hasPath(fullPath);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void beginAssocMany(String key) {
|
||||
|
||||
pathStack.pushPathKey(key);
|
||||
|
||||
depthOffset--;
|
||||
internalAppendKeyBegin(key);
|
||||
depthOffset++;
|
||||
buffer.append("[");
|
||||
}
|
||||
|
||||
public void endAssocMany(){
|
||||
|
||||
pathStack.pop();
|
||||
|
||||
if (pretty){
|
||||
depthOffset--;
|
||||
appendIndent();
|
||||
depthOffset++;
|
||||
}
|
||||
buffer.append("]");
|
||||
}
|
||||
|
||||
private void internalAppendKeyBegin(String key) {
|
||||
if (!beanState.isFirstKey()){
|
||||
buffer.append(",");
|
||||
}
|
||||
if (pretty){
|
||||
appendIndent();
|
||||
}
|
||||
appendKeyWithComma(key, false);
|
||||
}
|
||||
|
||||
public <T> void appendNameValue(String key, ScalarType<T> scalarType, T value) {
|
||||
appendKeyWithComma(key, true);
|
||||
scalarType.jsonWrite(buffer, value, getValueAdapter());
|
||||
}
|
||||
|
||||
public void appendDiscriminator(String key, String discValue) {
|
||||
appendKeyWithComma(key, true);
|
||||
buffer.append("\"");
|
||||
buffer.append(discValue);
|
||||
buffer.append("\"");
|
||||
}
|
||||
|
||||
private void appendKeyWithComma(String key, boolean withComma) {
|
||||
if (withComma){
|
||||
if (!beanState.isFirstKey()){
|
||||
buffer.append(",");
|
||||
}
|
||||
}
|
||||
buffer.append("\"");
|
||||
if(key == null) {
|
||||
buffer.append("null");
|
||||
} else {
|
||||
buffer.append(key);
|
||||
}
|
||||
buffer.append("\":");
|
||||
}
|
||||
|
||||
public void appendNull(String key) {
|
||||
appendKeyWithComma(key, true);
|
||||
buffer.append("null");
|
||||
}
|
||||
|
||||
public void appendNull() {
|
||||
buffer.append("null");
|
||||
}
|
||||
|
||||
public JsonValueAdapter getValueAdapter() {
|
||||
return valueAdapter;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void popParentBean(){
|
||||
parentBeans.pop();
|
||||
}
|
||||
|
||||
public void pushParentBean(Object parentBean){
|
||||
parentBeans.push(parentBean);
|
||||
}
|
||||
|
||||
public void popParentBeanMany(){
|
||||
parentBeans.pop();
|
||||
depthOffset--;
|
||||
}
|
||||
|
||||
public void pushParentBeanMany(Object parentBean){
|
||||
parentBeans.push(parentBean);
|
||||
depthOffset++;
|
||||
}
|
||||
|
||||
public boolean isParentBean(Object bean){
|
||||
if (parentBeans.isEmpty()){
|
||||
return false;
|
||||
} else {
|
||||
return parentBeans.contains(bean);
|
||||
}
|
||||
}
|
||||
|
||||
public WriteBeanState pushBeanState(Object bean) {
|
||||
WriteBeanState newState = new WriteBeanState();//bean);
|
||||
WriteBeanState prevState = beanState;
|
||||
beanState = newState;
|
||||
return prevState;
|
||||
}
|
||||
|
||||
public void pushPreviousState(WriteBeanState previousState) {
|
||||
this.beanState = previousState;
|
||||
}
|
||||
|
||||
|
||||
public static class WriteBeanState {
|
||||
|
||||
private boolean firstKeyOut;
|
||||
|
||||
public WriteBeanState() {
|
||||
|
||||
}
|
||||
|
||||
public boolean isFirstKey() {
|
||||
if (!firstKeyOut){
|
||||
firstKeyOut = true;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// private final SpiEbeanServer server;
|
||||
//
|
||||
// private final WriteJsonBuffer buffer;
|
||||
//
|
||||
// private final boolean pretty;
|
||||
//
|
||||
// private final JsonValueAdapter valueAdapter;
|
||||
//
|
||||
// private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
//
|
||||
// private final PathProperties pathProperties;
|
||||
//
|
||||
// private final Map<String, JsonWriteBeanVisitor<?>> visitorMap;
|
||||
//
|
||||
// private final String callback;
|
||||
//
|
||||
// private final PathStack pathStack;
|
||||
//
|
||||
// private WriteBeanState beanState;
|
||||
//
|
||||
// private int depthOffset;
|
||||
//
|
||||
// boolean assocOne;
|
||||
//
|
||||
// public WriteJsonContext(WriteJsonBuffer buffer, boolean pretty, JsonValueAdapter dfltValueAdapter,
|
||||
// JsonWriteOptions options, String requestCallback, SpiEbeanServer server){
|
||||
//
|
||||
// this.server = server;
|
||||
// this.buffer = buffer;
|
||||
// this.pretty = pretty;
|
||||
// this.pathStack = new PathStack();
|
||||
// this.callback = getCallback(requestCallback, options);
|
||||
// if (options == null){
|
||||
// this.valueAdapter = dfltValueAdapter;
|
||||
// this.visitorMap = null;
|
||||
// this.pathProperties = null;
|
||||
//
|
||||
// } else {
|
||||
// this.valueAdapter = getValueAdapter(dfltValueAdapter, options.getValueAdapter());
|
||||
// this.visitorMap = emptyToNull(options.getVisitorMap());
|
||||
// this.pathProperties = emptyToNull(options.getPathProperties());
|
||||
// }
|
||||
//
|
||||
// if (callback != null){
|
||||
// buffer.append(requestCallback).append("(");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void toJson(String name, Collection<?> c) {
|
||||
//
|
||||
// beginAssocMany(name);
|
||||
//
|
||||
// Iterator<?> it = c.iterator();
|
||||
// if (!it.hasNext()){
|
||||
// endAssocMany();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// EntityBean o = (EntityBean)it.next();
|
||||
// BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
//
|
||||
// d.jsonWrite(this, o);
|
||||
// while (it.hasNext()) {
|
||||
// appendComma();
|
||||
// EntityBean t = (EntityBean)it.next();
|
||||
// d.jsonWrite(this, t);
|
||||
// }
|
||||
// endAssocMany();
|
||||
// }
|
||||
//
|
||||
// private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
// BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
// if (d == null){
|
||||
// String msg = "No BeanDescriptor found for "+cls;
|
||||
// throw new RuntimeException(msg);
|
||||
// }
|
||||
// return d;
|
||||
// }
|
||||
//
|
||||
// public void appendRawValue(String key, String rawJsonValue) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append(rawJsonValue);
|
||||
// }
|
||||
//
|
||||
// public void appendQuoteEscapeValue(String key, String valueToEscape) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// EscapeJson.escapeQuote(valueToEscape, buffer);
|
||||
// }
|
||||
//
|
||||
// public void end() {
|
||||
// if (callback != null){
|
||||
// buffer.append(")");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private <MK,MV> Map<MK,MV> emptyToNull(Map<MK,MV> m){
|
||||
// if ( m == null || m.isEmpty()) {
|
||||
// return null;
|
||||
// } else {
|
||||
// return m;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private PathProperties emptyToNull(PathProperties m){
|
||||
// if ( m == null || m.isEmpty()) {
|
||||
// return null;
|
||||
// } else {
|
||||
// return m;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private String getCallback(String requestCallback, JsonWriteOptions options) {
|
||||
// if (requestCallback != null){
|
||||
// return requestCallback;
|
||||
// }
|
||||
// if (options != null){
|
||||
// return options.getCallback();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// private JsonValueAdapter getValueAdapter(JsonValueAdapter dfltValueAdapter, JsonValueAdapter valueAdapter) {
|
||||
// return valueAdapter == null ? dfltValueAdapter : valueAdapter;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Return the set of properties to write to JSON. If null is returned then
|
||||
// * the default will output the properties loaded for this bean.
|
||||
// */
|
||||
// public Set<String> getIncludeProperties() {
|
||||
// if (pathProperties != null){
|
||||
// String path = pathStack.peekWithNull();
|
||||
// return pathProperties.get(path);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public JsonWriteBeanVisitor<?> getBeanVisitor() {
|
||||
// if (visitorMap != null){
|
||||
// String path = pathStack.peekWithNull();
|
||||
// return visitorMap.get(path);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public String getJson() {
|
||||
// return buffer.toString();
|
||||
// }
|
||||
//
|
||||
// private void appendIndent(){
|
||||
//
|
||||
// buffer.append("\n");
|
||||
// int depth = depthOffset + parentBeans.size();
|
||||
// for (int i = 0; i < depth; i++) {
|
||||
// buffer.append(" ");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void appendObjectBegin(){
|
||||
// if (pretty && !assocOne){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("{");
|
||||
// }
|
||||
// public void appendObjectEnd(){
|
||||
// buffer.append("}");
|
||||
// }
|
||||
//
|
||||
// public void appendArrayBegin(){
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("[");
|
||||
// depthOffset++;
|
||||
// }
|
||||
//
|
||||
// public void appendArrayEnd(){
|
||||
// depthOffset--;
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("]");
|
||||
// }
|
||||
//
|
||||
// public void appendComma(){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
//
|
||||
// public void addDepthOffset(int offset){
|
||||
// depthOffset += offset;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocOneIsNull(String key) {
|
||||
// depthOffset++;
|
||||
// internalAppendKeyBegin(key);
|
||||
// appendNull();
|
||||
// depthOffset--;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocOne(String key) {
|
||||
// pathStack.pushPathKey(key);
|
||||
//
|
||||
// internalAppendKeyBegin(key);
|
||||
// assocOne = true;
|
||||
// }
|
||||
//
|
||||
// public void endAssocOne() {
|
||||
//
|
||||
// pathStack.pop();
|
||||
// assocOne = false;
|
||||
// }
|
||||
//
|
||||
// public Boolean includeMany(String key) {
|
||||
// if (pathProperties != null){
|
||||
// String fullPath = pathStack.peekFullPath(key);
|
||||
// return pathProperties.hasPath(fullPath);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocMany(String key) {
|
||||
//
|
||||
// pathStack.pushPathKey(key);
|
||||
//
|
||||
// depthOffset--;
|
||||
// internalAppendKeyBegin(key);
|
||||
// depthOffset++;
|
||||
// buffer.append("[");
|
||||
// }
|
||||
//
|
||||
// public void endAssocMany(){
|
||||
//
|
||||
// pathStack.pop();
|
||||
//
|
||||
// if (pretty){
|
||||
// depthOffset--;
|
||||
// appendIndent();
|
||||
// depthOffset++;
|
||||
// }
|
||||
// buffer.append("]");
|
||||
// }
|
||||
//
|
||||
// private void internalAppendKeyBegin(String key) {
|
||||
// if (!beanState.isFirstKey()){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// appendKeyWithComma(key, false);
|
||||
// }
|
||||
//
|
||||
// public <T> void appendNameValue(String key, ScalarType<T> scalarType, T value) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// scalarType.jsonWrite(buffer, value, getValueAdapter());
|
||||
// }
|
||||
//
|
||||
// public void appendDiscriminator(String key, String discValue) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append("\"");
|
||||
// buffer.append(discValue);
|
||||
// buffer.append("\"");
|
||||
// }
|
||||
//
|
||||
// private void appendKeyWithComma(String key, boolean withComma) {
|
||||
// if (withComma){
|
||||
// if (!beanState.isFirstKey()){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
// }
|
||||
// buffer.append("\"");
|
||||
// if(key == null) {
|
||||
// buffer.append("null");
|
||||
// } else {
|
||||
// buffer.append(key);
|
||||
// }
|
||||
// buffer.append("\":");
|
||||
// }
|
||||
//
|
||||
// public void appendNull(String key) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append("null");
|
||||
// }
|
||||
//
|
||||
// public void appendNull() {
|
||||
// buffer.append("null");
|
||||
// }
|
||||
//
|
||||
// public JsonValueAdapter getValueAdapter() {
|
||||
// return valueAdapter;
|
||||
// }
|
||||
//
|
||||
// public String toString() {
|
||||
// return buffer.toString();
|
||||
// }
|
||||
//
|
||||
// public void popParentBean(){
|
||||
// parentBeans.pop();
|
||||
// }
|
||||
//
|
||||
// public void pushParentBean(Object parentBean){
|
||||
// parentBeans.push(parentBean);
|
||||
// }
|
||||
//
|
||||
// public void popParentBeanMany(){
|
||||
// parentBeans.pop();
|
||||
// depthOffset--;
|
||||
// }
|
||||
//
|
||||
// public void pushParentBeanMany(Object parentBean){
|
||||
// parentBeans.push(parentBean);
|
||||
// depthOffset++;
|
||||
// }
|
||||
//
|
||||
// public boolean isParentBean(Object bean){
|
||||
// if (parentBeans.isEmpty()){
|
||||
// return false;
|
||||
// } else {
|
||||
// return parentBeans.contains(bean);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public WriteBeanState pushBeanState(Object bean) {
|
||||
// WriteBeanState newState = new WriteBeanState();//bean);
|
||||
// WriteBeanState prevState = beanState;
|
||||
// beanState = newState;
|
||||
// return prevState;
|
||||
// }
|
||||
//
|
||||
// public void pushPreviousState(WriteBeanState previousState) {
|
||||
// this.beanState = previousState;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static class WriteBeanState {
|
||||
//
|
||||
// private boolean firstKeyOut;
|
||||
//
|
||||
// public WriteBeanState() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public boolean isFirstKey() {
|
||||
// if (!firstKeyOut){
|
||||
// firstKeyOut = true;
|
||||
// return true;
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import com.avaje.ebean.config.CompoundTypeProperty;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonElementObject;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonContext.WriteBeanState;
|
||||
//import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
//import com.avaje.ebeaninternal.server.text.json.WriteJsonContext.WriteBeanState;
|
||||
|
||||
/**
|
||||
* The internal representation of a Compound Type (Immutable Compound Value
|
||||
@@ -223,40 +223,40 @@ public final class CtCompoundType<V> implements ScalarDataReader<V> {
|
||||
}
|
||||
|
||||
|
||||
public void jsonWrite(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
|
||||
if (valueObject == null){
|
||||
ctx.beginAssocOneIsNull(propertyName);
|
||||
|
||||
} else {
|
||||
ctx.pushParentBean(valueObject);
|
||||
ctx.beginAssocOne(propertyName);
|
||||
jsonWriteProps(ctx, valueObject, propertyName);
|
||||
ctx.endAssocOne();
|
||||
ctx.popParentBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void jsonWriteProps(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
|
||||
ctx.appendObjectBegin();
|
||||
WriteBeanState prevState = ctx.pushBeanState(valueObject);
|
||||
|
||||
for (int i = 0; i < properties.length; i++) {
|
||||
String propName = properties[i].getName();
|
||||
Object value = properties[i].getValue((V)valueObject);
|
||||
if (propReaders[i] instanceof CtCompoundType<?>) {
|
||||
((CtCompoundType)propReaders[i]).jsonWrite(ctx, value, propName);
|
||||
|
||||
} else {
|
||||
ctx.appendNameValue(propName, (ScalarType)propReaders[i], value);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.pushPreviousState(prevState);
|
||||
ctx.appendObjectEnd();
|
||||
}
|
||||
// public void jsonWrite(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
//
|
||||
// if (valueObject == null){
|
||||
// ctx.beginAssocOneIsNull(propertyName);
|
||||
//
|
||||
// } else {
|
||||
// ctx.pushParentBean(valueObject);
|
||||
// ctx.beginAssocOne(propertyName);
|
||||
// jsonWriteProps(ctx, valueObject, propertyName);
|
||||
// ctx.endAssocOne();
|
||||
// ctx.popParentBean();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
// private void jsonWriteProps(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
//
|
||||
// ctx.appendObjectBegin();
|
||||
// WriteBeanState prevState = ctx.pushBeanState(valueObject);
|
||||
//
|
||||
// for (int i = 0; i < properties.length; i++) {
|
||||
// String propName = properties[i].getName();
|
||||
// Object value = properties[i].getValue((V)valueObject);
|
||||
// if (propReaders[i] instanceof CtCompoundType<?>) {
|
||||
// ((CtCompoundType)propReaders[i]).jsonWrite(ctx, value, propName);
|
||||
//
|
||||
// } else {
|
||||
// ctx.appendNameValue(propName, (ScalarType)propReaders[i], value);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// ctx.pushPreviousState(prevState);
|
||||
// ctx.appendObjectEnd();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
@@ -40,9 +41,12 @@ public class ScalarTypeByte extends ScalarTypeBase<Byte> {
|
||||
public Byte toBeanType(Object value) {
|
||||
return BasicTypeConverter.toByte(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
throw new TextException("Not supported");
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
@@ -45,6 +46,11 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
@@ -69,6 +70,11 @@ public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
|
||||
baseType.loadIgnore(dataReader);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
throw new TextException("Not supported");
|
||||
|
||||
@@ -7,10 +7,10 @@ import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Types;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import org.joda.time.DateMidnight;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalTime;
|
||||
|
||||
@@ -66,7 +66,10 @@ public class ScalarTypeJodaLocalTime extends ScalarTypeBase<LocalTime> {
|
||||
return new LocalTime(value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
ctx.write(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
@@ -67,6 +68,11 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
return (Map)value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Reference in New Issue
Block a user