Search in sources :

Example 1 with JsonContext

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

the class EscapeTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.setEscapeForwardSlash(true);
    StringWriter out = new StringWriter();
    JsonWriter writer = new JsonWriter(context, out);
    writer.writeObject("stream://asdf");
    String json = out.toString();
    System.out.println(json);
    JsonReader reader = new JsonReader(context, json);
    String s = reader.read(String.class);
    System.out.println(s);
}
Also used : JsonContext(com.serotonin.json.JsonContext) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonWriter(com.serotonin.json.JsonWriter)

Example 2 with JsonContext

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

the class ExceptionTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.addConverter(new ThrowableSerializingConverter(), Throwable.class);
    StringWriter out = new StringWriter();
    JsonWriter writer = new JsonWriter(context, out);
    writer.setPrettyOutput(true);
    writer.writeObject(createException1());
    System.out.println(out);
    JsonReader reader = new JsonReader(context, out.toString());
    Throwable t = reader.read(Throwable.class);
    System.out.println("Great success!");
    System.out.println(t.getMessage());
    t.printStackTrace();
}
Also used : JsonContext(com.serotonin.json.JsonContext) StringWriter(java.io.StringWriter) ThrowableSerializingConverter(com.serotonin.json.convert.ThrowableSerializingConverter) JsonReader(com.serotonin.json.JsonReader) JsonWriter(com.serotonin.json.JsonWriter)

Example 3 with JsonContext

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

the class PopulateTest method main.

public static void main(String[] args) throws Exception {
    context = new JsonContext();
    context.addFactory(new ObjectFactory() {

        @Override
        public Object create(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return new Subclass1();
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return new Subclass2();
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    test1();
    test2();
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) ObjectFactory(com.serotonin.json.spi.ObjectFactory) JsonValue(com.serotonin.json.type.JsonValue)

Example 4 with JsonContext

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

the class SubclassTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.addResolver(new TypeResolver() {

        @Override
        public Class<?> resolve(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return Subclass1.class;
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return Subclass2.class;
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    // context.addFactory(new ObjectFactory() {
    // @Override
    // public Object create(JsonValue jsonValue) throws JsonException {
    // if (jsonValue.toJsonObject().hasProperty("sub1Value"))
    // return new Subclass1();
    // if (jsonValue.toJsonObject().hasProperty("sub2Value"))
    // return new Subclass2();
    // throw new JsonException("Unknown BaseClass: " + jsonValue);
    // }
    // }, BaseClass.class);
    // List<BaseClass> list = new ArrayList<BaseClass>();
    // list.add(new Subclass1());
    // list.add(new Subclass2());
    // 
    // String json = JsonWriter.writeToString(context, list);
    // 
    // System.out.println(json);
    String json = "[{\"id\":\"Subclass1\",\"sub1Value\":\"a\",\"baseValue\":\"b\"},{\"myId\":\"Subclass2\",\"sub2Value\":\"c\",\"baseValue\":\"d\"}]";
    JsonReader reader = new JsonReader(context, json);
    TypeDefinition type = new TypeDefinition(List.class, BaseClass.class);
    Object read = reader.read(type);
    System.out.println(read);
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) TypeResolver(com.serotonin.json.spi.TypeResolver) JsonValue(com.serotonin.json.type.JsonValue) JsonReader(com.serotonin.json.JsonReader) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Aggregations

JsonContext (com.serotonin.json.JsonContext)4 JsonReader (com.serotonin.json.JsonReader)3 JsonException (com.serotonin.json.JsonException)2 JsonWriter (com.serotonin.json.JsonWriter)2 JsonValue (com.serotonin.json.type.JsonValue)2 StringWriter (java.io.StringWriter)2 ThrowableSerializingConverter (com.serotonin.json.convert.ThrowableSerializingConverter)1 ObjectFactory (com.serotonin.json.spi.ObjectFactory)1 TypeResolver (com.serotonin.json.spi.TypeResolver)1 TypeDefinition (com.serotonin.json.util.TypeDefinition)1