Search in sources :

Example 26 with Json

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);
}
Also used : QueryOptionPage(siena.core.options.QueryOptionPage) QueryOption(siena.core.options.QueryOption) PersonLongAutoID(siena.base.test.model.PersonLongAutoID) Json(siena.Json)

Example 27 with Json

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;
}
Also used : Field(java.lang.reflect.Field) Json(siena.Json) SienaException(siena.SienaException)

Example 28 with Json

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);
    }
}
Also used : Json(siena.Json) SienaException(siena.SienaException) ParseException(java.text.ParseException) Field(java.lang.reflect.Field) SienaException(siena.SienaException)

Example 29 with Json

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);
}
Also used : Json(siena.Json) SienaException(siena.SienaException) ParseException(java.text.ParseException) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) Collection(java.util.Collection) SienaException(siena.SienaException) HashMap(java.util.HashMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 30 with Json

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;
}
Also used : Field(java.lang.reflect.Field) Json(siena.Json)

Aggregations

Json (siena.Json)48 SienaException (siena.SienaException)7 Field (java.lang.reflect.Field)6 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 Date (java.util.Date)3 Element (org.dom4j.Element)3 DecimalPrecision (siena.core.DecimalPrecision)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Polymorphic (siena.core.Polymorphic)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1