Search in sources :

Example 21 with Json

use of siena.Json in project siena by mandubian.

the class JsonTest method testPutAll.

public void testPutAll() {
    Json map = Json.map().put("foo", 1).put("bar", 2);
    Json other = Json.map().put("baz", 3);
    other.putAll(map);
    assertEquals(3, other.size());
    assertEquals(3, other.get("baz").asInt());
    assertEquals(2, other.get("bar").asInt());
    assertEquals(1, other.get("foo").asInt());
}
Also used : Json(siena.Json)

Example 22 with Json

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 23 with Json

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Json(siena.Json) Field(java.lang.reflect.Field) SimpleDateFormat(java.text.SimpleDateFormat) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) SienaException(siena.SienaException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet)

Example 24 with Json

use of siena.Json in project siena by mandubian.

the class GoogleSqlPersistenceManager method addParameters.

@Override
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, f);
                } else {
                    setParameter(ps, i++, null, f);
                }
            }
        } 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, field);
        }
    }
    return i;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 25 with Json

use of siena.Json in project siena by mandubian.

the class JdbcMappingUtils method fromObject.

public static Object fromObject(Field field, Object value) {
    Class<?> type = field.getType();
    // in H2 database, mediumtext is mapped to CLOB
    if (Json.class.isAssignableFrom(type) && value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
        java.sql.Clob clob = (java.sql.Clob) value;
        try {
            return Json.load(new BufferedReader(clob.getCharacterStream()));
        } catch (SQLException e) {
            throw new SienaException(e);
        }
    }
    if (field.getAnnotation(Embedded.class) != null && value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
        java.sql.Clob clob = (java.sql.Clob) value;
        try {
            Json data = Json.load(new BufferedReader(clob.getCharacterStream()));
            return JsonSerializer.deserialize(field, data);
        } catch (SQLException e) {
            throw new SienaException(e);
        }
    }
    // issue https://github.com/mandubian/siena/issues/5
    if (value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
        java.sql.Clob clob = (java.sql.Clob) value;
        try {
            // @see http://osdir.com/ml/h2-database/2011-06/msg00170.html
            return clob.getSubString(1, (int) clob.length());
        } catch (SQLException e) {
            throw new SienaException(e);
        }
    }
    if (field.isAnnotationPresent(Polymorphic.class)) {
        try {
            if (java.sql.Blob.class.isAssignableFrom(value.getClass())) {
                java.sql.Blob blob = (java.sql.Blob) value;
                ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(blob.getBytes(0, (int) blob.length())));
                return in.readObject();
            } else {
                ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream((byte[]) value));
                return in.readObject();
            }
        } catch (IOException e) {
            throw new SienaException(e);
        } catch (ClassNotFoundException e) {
            throw new SienaException(e);
        } catch (SQLException e) {
            throw new SienaException(e);
        }
    }
    if (byte[].class == type && value != null && java.sql.Blob.class.isAssignableFrom(value.getClass())) {
        java.sql.Blob blob = (java.sql.Blob) value;
        try {
            // TODO what to do with a very long blob????
            return blob.getBytes(0, (int) blob.length());
        } catch (SQLException e) {
            throw new SienaException(e);
        }
    }
    if (BigDecimal.class == type) {
        DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
        if (ann == null) {
            return (BigDecimal) value;
        } else {
            switch(ann.storageType()) {
                case DOUBLE:
                    return BigDecimal.valueOf((Double) value);
                case STRING:
                    return new BigDecimal((String) value);
                case NATIVE:
                    return (BigDecimal) value;
            }
        }
    }
    return Util.fromObject(field, value);
}
Also used : SQLException(java.sql.SQLException) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) SienaException(siena.SienaException) Embedded(siena.embed.Embedded) ObjectInputStream(java.io.ObjectInputStream)

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