Search in sources :

Example 6 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class PopulateTest method test1.

static void test1() throws JsonException, IOException {
    List<BaseClass> list = new ArrayList<BaseClass>();
    list.add(new Subclass1());
    list.add(new Subclass2());
    String json = JsonWriter.writeToString(context, list);
    System.out.println(json);
    JsonReader reader = new JsonReader(context, json);
    TypeDefinition type = new TypeDefinition(List.class, BaseClass.class);
    list.clear();
    reader.readInto(type, list);
    System.out.println(list);
}
Also used : ArrayList(java.util.ArrayList) JsonReader(com.serotonin.json.JsonReader) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 7 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class ReadTest method main.

public static void main(String[] args) throws Exception {
    read("\"k\"", String.class);
    read("{\"bigi\":1234567890123456,\"bigd\":1234567890123456.7890123456789,}", Object.class);
    read("{\"bigi\":1234567890123456,\"bigd\":1234567890123456.7890123456789,}", new TypeDefinition(Map.class, String.class, BigDecimal.class));
    read("{\"bigi\":1234567890123456,\"bigd\":1234567890123456.7890123456789,}", new TypeDefinition(HashMap.class, String.class, BigDecimal.class));
    read("true", Boolean.TYPE);
    read("true", Boolean.class);
    read("10.1", Float.TYPE);
    read("10.1", Float.class);
    read("10.1", Double.TYPE);
    read("10.1", Double.class);
    read("1234567890123456789012345678901234567890", BigInteger.class);
    read("1234567890123456789012345678901234567890", BigDecimal.class);
    read("1234567890123456789012345678901234567890.1234567890123456789012345678901234567890", BigDecimal.class);
    read("[\"qwer\",\"asdf\",\"zxcv\",]", new TypeDefinition(List.class, String.class));
    read("[\"qwer\",\"asdf\",\"zxcv\",]", new TypeDefinition(ArrayList.class, String.class));
    // read("{\"34\":\"34\",\"list\":[\"qwer\",\"asdf\",\"zxcv\"],\"34\":34}", new TypeDefinition(Map.class, null,
    // String.class));
    read("[\"qwer\",\"asdf\",\"zxcv\"]", String[].class);
    read("\"SECOND\" \"SECOND\"", Enums.class);
    read("{\"bigDecimal\":1.1,\"bigInteger\":2,\"boolean1\":false,\"byte1\":-4,\"double1\":5,\"float1\":6.1,\"int1\":7,\"long1\":-8,\"short1\":9,\"string1\":\"i'm a read string\"}", Primitives.class);
    context.addSerializer(new PrimitiveSerializer(), Primitives.class);
    read("{\"bigi\":1234567890123456,\"bigd\":1234567890123456.7890123456789,}", Primitives.class);
    read("{\"myId\":\"Subclass2\",\"sub2Value\":\"sub2\",\"baseValue\":\"base\"}", Subclass2.class);
    read("{\"value\":[\"qwer\",\"asdf\",\"zxcv\"]}", new TypeDefinition(GenObject.class, new TypeDefinition(List.class, String.class)));
    read("[\"qwer\",\"asdf\",\"zxcv\"]", new ArrayList<String>(), new TypeDefinition(List.class, String.class));
    read("{\"imSerializable\":{\"id\":12,\"value\":\"my value\"},\"list1\":null,\"list2\":null,\"map\":null,\"primitives\":{\"bigi\":1234567890123456,\"bigd\":1234567890123456.7890123456789},\"uuid\":\"ad9557f1-5e88-4b74-ab51-5b64364e6ccf\"}", Compound.class);
    read("[[\"a1\",\"b1\",\"c1\"],[\"a2\",\"b2\",\"c2\"],[\"a3\",\"b3\",\"c3\"]]", new TypeDefinition(List.class, String[].class));
    read("{\"rows\":[[\"a1\",\"b1\",\"c1\"],[\"a2\",\"b2\",\"c2\"],[\"a3\",\"b3\",\"c3\"]]}", ListArray.class);
    read("{\"s1\":\"asdf\",\"s2\":\"zxcv\"}", new Mutable(), Mutable.class);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 8 with TypeDefinition

use of com.serotonin.json.util.TypeDefinition in project ma-core-public by infiniteautomation.

the class GenericTest method main.

public static void main(String[] args) throws Exception {
    Class<?> varClass = GenObject.class;
    Method method = varClass.getDeclaredMethod("getValue");
    ParameterizedType paramType = new TypeDefinition(List.class, String.class);
    ParameterizedType type = new TypeDefinition(varClass, paramType);
    // System.out.println(TypeUtils.determineTypeArguments(varClass,
    // (ParameterizedType) varClass.getGenericSuperclass()));
    System.out.println(TypeUtils.resolveTypeVariable(type, method.getGenericReturnType()));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Method(java.lang.reflect.Method) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Aggregations

TypeDefinition (com.serotonin.json.util.TypeDefinition)8 List (java.util.List)4 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 ArrayList (java.util.ArrayList)3 JsonException (com.serotonin.json.JsonException)2 JsonReader (com.serotonin.json.JsonReader)2 JsonArray (com.serotonin.json.type.JsonArray)2 JsonBoolean (com.serotonin.json.type.JsonBoolean)2 JsonValue (com.serotonin.json.type.JsonValue)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 IntStringPair (com.serotonin.db.pair.IntStringPair)1 JsonContext (com.serotonin.json.JsonContext)1 TypeResolver (com.serotonin.json.spi.TypeResolver)1 JsonNumber (com.serotonin.json.type.JsonNumber)1 JsonObject (com.serotonin.json.type.JsonObject)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 PointFolder (com.serotonin.m2m2.vo.hierarchy.PointFolder)1