use of siena.Json in project siena by mandubian.
the class JsonSerializer method deserialize.
public static Object deserialize(Field f, Json data) {
if (data == null || data.isNull())
return deserializePlain(f.getType(), data);
Class<?> clazz = f.getType();
if (Map.class.isAssignableFrom(clazz)) {
if (!data.isMap()) {
throw new SienaException("Error while deserializating field " + f.getDeclaringClass() + "." + f.getName() + " of type " + clazz + ". A Json map is needed but found: " + data);
}
Map<String, Object> map = new HashMap<String, Object>();
for (String key : data.keys()) {
map.put(key, deserialize(Util.getGenericClass(f, 1), data.get(key)));
}
return map;
} else if (Collection.class.isAssignableFrom(clazz)) {
if (!data.isList()) {
throw new SienaException("Error while deserializating field " + f.getDeclaringClass() + "." + f.getName() + " of type " + clazz + ". A Json list is needed but found: " + data);
}
Collection<Object> collection = null;
if (clazz == List.class) {
collection = new ArrayList<Object>(data.size());
} else {
collection = new HashSet<Object>();
}
for (Json value : data) {
collection.add(deserialize(Util.getGenericClass(f, 0), value));
}
return collection;
} else if (Json.class.isAssignableFrom(clazz)) {
return data;
} else if (Field.class.isAssignableFrom(clazz)) {
String fieldName = data.get("fieldName").asString();
String parentClass = data.get("parentClass").asString();
try {
Class<?> cl = Class.forName(parentClass);
Field fi = cl.getField(fieldName);
return fi;
} catch (ClassNotFoundException ex) {
throw new SienaException(ex);
} catch (NoSuchFieldException ex) {
throw new SienaException(ex);
}
} else if (clazz.isArray()) {
Class<?> arrClazz = clazz.getComponentType();
Object arr = Array.newInstance(arrClazz, data.size());
int i = 0;
for (Json value : data) {
Array.set(arr, i++, deserialize(arrClazz, value));
}
return arr;
} else if (clazz == Class.class) {
String className = data.get("className").asString();
try {
Class<?> cl = Class.forName(className);
return cl;
} catch (ClassNotFoundException ex) {
throw new SienaException(ex);
}
}
Format format = f.getAnnotation(Format.class);
if (format != null) {
if (f.getType() == Date.class) {
SimpleDateFormat sdf = new SimpleDateFormat(format.value());
try {
return sdf.parse(data.str());
} catch (ParseException e) {
throw new SienaException(e);
}
}
}
JsonDeserializeAs as = f.getAnnotation(JsonDeserializeAs.class);
if (as != null) {
if (as.value() == String.class) {
return data.asString();
} else {
return deserialize(as.value(), data);
}
}
return deserialize(clazz, data);
}
use of siena.Json in project siena by mandubian.
the class JdbcPersistenceManager method addParameters.
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
for (Field field : fields) {
Class<?> type = field.getType();
if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
Object rel = Util.readField(obj, field);
for (Field f : ci.keys) {
if (rel != null) {
Object value = Util.readField(rel, f);
if (value instanceof Json)
value = ((Json) value).toString();
setParameter(ps, i++, value);
} else {
setParameter(ps, i++, null);
}
}
} else {
Object value = Util.readField(obj, field);
if (value != null) {
if (Json.class.isAssignableFrom(type)) {
value = ((Json) value).toString();
} else if (field.getAnnotation(Embedded.class) != null) {
value = JsonSerializer.serialize(value).toString();
} else if (field.getAnnotation(Polymorphic.class) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(value);
out.close();
} catch (IOException e) {
throw new SienaException(e);
}
value = bos.toByteArray();
} else if (Enum.class.isAssignableFrom(type)) {
value = value.toString();
} else if (BigDecimal.class == type) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = (BigDecimal) value;
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
value = ((BigDecimal) value).toPlainString();
break;
case NATIVE:
value = (BigDecimal) value;
break;
}
}
}
}
setParameter(ps, i++, value);
}
}
return i;
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method serialize.
public void serialize(Document document, OutputStream out) throws IOException {
Json json = toJson(document);
json.write(new OutputStreamWriter(out));
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method deserialize.
public Document deserialize(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Json json = Json.load(reader);
return fromJson(json);
}
use of siena.Json in project siena by mandubian.
the class JsonSerializer method main.
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
doc.addElement("root").addAttribute("foo", "bar").addAttribute("foobar", "baz").addElement("child").addAttribute("x", "y").addElement("grandchild").addAttribute("a", "b").setText("foo bar");
System.out.println("Original document");
System.out.println(doc.asXML());
System.out.println();
Json json = toJson(doc);
System.out.println("As Json");
System.out.println(json);
System.out.println();
Document result = fromJson(json);
System.out.println("Back to document");
System.out.println(result.asXML());
System.out.println();
}
Aggregations