use of siena.Json in project siena by mandubian.
the class BaseTest method testDumpQueryOption.
public void testDumpQueryOption() {
Query<PersonLongAutoID> query = pm.createQuery(PersonLongAutoID.class);
QueryOption opt = query.option(QueryOptionPage.ID);
Json dump = opt.dump();
String str = JsonSerializer.serialize(dump).toString();
assertNotNull(str);
assertEquals("{\"value\": {\"pageType\": \"TEMPORARY\", \"state\": \"PASSIVE\", \"pageSize\": 0, \"type\": 1}, \"type\": \"" + QueryOptionPage.class.getName() + "\"}", str);
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method serializeList.
public static Json serializeList(Object obj) throws Exception {
Field[] fields = obj.getClass().getDeclaredFields();
Json result = list();
for (Field f : fields) {
if (mustIgnore(f))
continue;
At at = f.getAnnotation(At.class);
if (at == null)
throw new SienaException("Field " + obj.getClass() + "." + f.getName() + " must be annotated with @At(n)");
result.addAt(at.value(), serialize(f.get(obj), f));
}
// TEST
// serializes super classes
Class<?> clazz = obj.getClass().getSuperclass();
while (clazz != null) {
fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
At at = f.getAnnotation(At.class);
if (at == null)
throw new SienaException("Field " + obj.getClass() + "." + f.getName() + " must be annotated with @At(n)");
result.addAt(at.value(), serialize(f.get(obj), f));
}
clazz = clazz.getSuperclass();
}
// TEST
return result;
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method deserialize.
public static Object deserialize(Class<?> clazz, Json data) {
try {
EmbeddedMap map = clazz.getAnnotation(EmbeddedMap.class);
if (map != null) {
if (!data.isMap()) {
throw new SienaException("Error while deserializating class " + clazz + ". A Json map is needed but found: " + data);
}
Object obj = Util.createObjectInstance(clazz);
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
Key key = f.getAnnotation(Key.class);
if (key != null)
Util.setField(obj, f, deserialize(f, data.get(key.value())));
else
Util.setField(obj, f, deserialize(f, data.get(f.getName())));
}
// deserializes super classes
Class<?> superclazz = obj.getClass().getSuperclass();
while (superclazz != null) {
fields = superclazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
Key key = f.getAnnotation(Key.class);
if (key != null)
Util.setField(obj, f, deserialize(f, data.get(key.value())));
else
Util.setField(obj, f, deserialize(f, data.get(f.getName())));
}
superclazz = superclazz.getSuperclass();
}
return obj;
}
EmbeddedList list = clazz.getAnnotation(EmbeddedList.class);
if (list != null) {
if (!data.isList()) {
throw new SienaException("Error while deserializating class " + clazz + ". A Json list is needed but found: " + data);
}
Object obj = Util.createObjectInstance(clazz);
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
At at = f.getAnnotation(At.class);
if (at == null)
throw new SienaException("Field " + obj.getClass() + "." + f.getName() + " must be annotated with @At(n)");
Json value = data.at(at.value());
Util.setField(obj, f, deserialize(f, value));
}
// deserializes super classes
Class<?> superclazz = obj.getClass().getSuperclass();
while (superclazz != null) {
fields = superclazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
At at = f.getAnnotation(At.class);
if (at == null)
throw new SienaException("Field " + obj.getClass() + "." + f.getName() + " must be annotated with @At(n)");
Json value = data.at(at.value());
Util.setField(obj, f, deserialize(f, value));
}
superclazz = superclazz.getSuperclass();
}
return obj;
}
if (Json.class.isAssignableFrom(clazz)) {
return data;
}
JsonDeserializeAs as = clazz.getAnnotation(JsonDeserializeAs.class);
if (as != null) {
if (as.value() == String.class) {
return data.asString();
} else {
Class<?> asClazz = as.value();
Object ret = deserialize(as.value(), data);
if (JsonRestorable.class.isAssignableFrom(asClazz)) {
return ((JsonRestorable<?>) ret).restore();
}
return ret;
}
}
return deserializePlain(clazz, data);
} catch (Exception e) {
throw new SienaException(e);
}
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method serialize.
public static Json serialize(Object obj, Field f) {
if (obj == null)
return new Json(null);
Class<?> clazz = obj.getClass();
// if(obj instanceof Map<?, ?>) {
if (Map.class.isAssignableFrom(clazz)) {
Map<?, ?> map = (Map<?, ?>) obj;
Json result = map();
for (Map.Entry<?, ?> entry : map.entrySet()) {
String key = entry.getKey().toString();
Json value = serialize(entry.getValue(), null);
result.put(key, value);
}
return result;
}
// if(obj instanceof Collection<?>) {
if (Collection.class.isAssignableFrom(clazz)) {
Json result = list();
Collection<?> col = (Collection<?>) obj;
for (Object object : col) {
result.add(serialize(object));
}
return result;
}
if (Json.class.isAssignableFrom(clazz)) {
return new Json(obj);
}
if (Field.class.isAssignableFrom(clazz)) {
return new Json(obj);
}
if (clazz.isArray()) {
return new Json(obj);
}
if (clazz == Class.class) {
return new Json(obj);
}
if (JsonDumpable.class.isAssignableFrom(obj.getClass())) {
return ((JsonDumpable) obj).dump();
}
try {
EmbeddedList list = obj.getClass().getAnnotation(EmbeddedList.class);
if (list != null) {
return serializeList(obj);
}
EmbeddedMap map = obj.getClass().getAnnotation(EmbeddedMap.class);
if (map != null) {
return serializeMap(obj);
}
} catch (SienaException e) {
throw e;
} catch (Exception e) {
throw new SienaException(e);
}
if (f != null) {
Format format = f.getAnnotation(Format.class);
if (format != null) {
if (obj.getClass() == Date.class) {
Date date = (Date) obj;
SimpleDateFormat sdf = new SimpleDateFormat(format.value());
return new Json(sdf.format(date));
}
}
}
return new Json(obj);
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method serializeMap.
public static Json serializeMap(Object obj) throws Exception {
Field[] fields = obj.getClass().getDeclaredFields();
Json result = map();
for (Field f : fields) {
if (mustIgnore(f))
continue;
Key k = f.getAnnotation(Key.class);
if (k != null) {
result.put(k.value(), serialize(f.get(obj), f));
} else {
result.put(f.getName(), serialize(f.get(obj), f));
}
}
// TEST
// serializes super classes
Class<?> clazz = obj.getClass().getSuperclass();
while (clazz != null) {
fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (mustIgnore(f))
continue;
Key k = f.getAnnotation(Key.class);
if (k != null) {
result.put(k.value(), serialize(Util.readField(obj, f), f));
} else {
result.put(f.getName(), serialize(Util.readField(obj, f), f));
}
}
clazz = clazz.getSuperclass();
}
// TEST
return result;
}
Aggregations