Fix for Issue 43 - Support for JSON unmarshalling of @XmlRootElement

beans.
This commit is contained in:
Rob Bygrave
2013-07-29 23:24:30 +12:00
parent 6f579ba890
commit 44d95dc06c
5 changed files with 74 additions and 16 deletions
@@ -71,6 +71,7 @@ import com.avaje.ebeaninternal.server.type.TypeManager;
import com.avaje.ebeaninternal.util.SortByClause;
import com.avaje.ebeaninternal.util.SortByClause.Property;
import com.avaje.ebeaninternal.util.SortByClauseParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -1318,10 +1319,8 @@ public class BeanDescriptor<T> {
public EntityBean createEntityBean() {
try {
// Note factoryType is used indirectly via beanReflect
EntityBean eb = (EntityBean) beanReflect.createEntityBean();
return eb;
return (EntityBean) beanReflect.createEntityBean();
} catch (Exception ex) {
throw new PersistenceException(ex);
}
@@ -2308,6 +2307,9 @@ public class BeanDescriptor<T> {
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);
}
}
}
@@ -2372,9 +2374,20 @@ public class BeanDescriptor<T> {
}
@SuppressWarnings("unchecked")
private T createJsonBean() {
if (EntityType.XMLELEMENT.equals(entityType)) {
try {
return beanType.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return (T)createEntityBean();
}
private ReadBeanState jsonReadObject(ReadJsonContext ctx, String path) {
T bean = (T) createEntityBean();
T bean = createJsonBean();
ctx.pushBean(bean, path, this);
do {
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.deploy;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -752,7 +753,11 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
Object value = getValueIntercept(bean);
if (value != null){
ctx.pushParentBeanMany(bean);
help.jsonWrite(ctx, name, value, include != null);
if (help != null){
help.jsonWrite(ctx, name, value, include != null);
} else {
ctx.toJson(name, (Collection<?>)value);
}
ctx.popParentBeanMany();
}
}
@@ -259,10 +259,14 @@ public class DeployCreateProperties {
@SuppressWarnings({ "unchecked", "rawtypes" })
private DeployBeanProperty createManyType(DeployBeanDescriptor<?> desc, Class<?> targetType, ManyType manyType) {
try {
ScalarType<?> scalarType = typeManager.getScalarType(targetType);
if (scalarType != null) {
return new DeployBeanPropertySimpleCollection(desc, targetType, scalarType, manyType);
}
} catch (NullPointerException e) {
logger.debug("expected non-scalar type"+e.getMessage());
}
//TODO: Handle Collection of CompoundType and Embedded Type
return new DeployBeanPropertyAssocMany(desc, targetType, manyType);
}
@@ -216,23 +216,21 @@ public class DJsonContext implements JsonContext {
} else {
BeanDescriptor<?> d = getDecriptor(o.getClass());
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback);
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
d.jsonWrite(ctx, o);
ctx.end();
}
}
private <T> void toJsonFromCollection(Collection<T> c, WriteJsonBuffer buffer, boolean pretty,
JsonWriteOptions options, String requestCallback){
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;
return;
}
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback);
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
Object o = it.next();
BeanDescriptor<?> d = getDecriptor(o.getClass());
@@ -248,15 +246,14 @@ public class DJsonContext implements JsonContext {
ctx.end();
}
private void toJsonFromMap(Map<Object,Object> map, WriteJsonBuffer buffer, boolean pretty,
JsonWriteOptions options, String requestCallback){
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);
WriteJsonContext ctx = new WriteJsonContext(buffer, pretty, dfltValueAdapter, options, requestCallback, server);
Set<Entry<Object,Object>> entrySet = map.entrySet();
Iterator<Entry<Object, Object>> it = entrySet.iterator();
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.text.json;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@@ -10,6 +12,8 @@ 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;
@@ -17,6 +21,8 @@ import com.avaje.ebeaninternal.server.util.ArrayStack;
public class WriteJsonContext implements JsonWriter {
private final SpiEbeanServer server;
private final WriteJsonBuffer buffer;
private final boolean pretty;
@@ -40,8 +46,9 @@ public class WriteJsonContext implements JsonWriter {
boolean assocOne;
public WriteJsonContext(WriteJsonBuffer buffer, boolean pretty, JsonValueAdapter dfltValueAdapter,
JsonWriteOptions options, String requestCallback){
JsonWriteOptions options, String requestCallback, SpiEbeanServer server){
this.server = server;
this.buffer = buffer;
this.pretty = pretty;
this.pathStack = new PathStack();
@@ -61,6 +68,38 @@ public class WriteJsonContext implements JsonWriter {
buffer.append(requestCallback).append("(");
}
}
public void toJson(String name, Collection<?> c) {
internalAppendKeyBegin(name);
Iterator<?> it = c.iterator();
if (!it.hasNext()){
buffer.append("[]");
return;
}
Object o = it.next();
BeanDescriptor<?> d = getDecriptor(o.getClass());
appendArrayBegin();
d.jsonWrite(this, o);
while (it.hasNext()) {
appendComma();
Object t = it.next();
d.jsonWrite(this, t);
}
appendArrayEnd();
}
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);