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);
}
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();
}
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();
}
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);
}
Aggregations