use of com.serotonin.json.JsonReader in project ma-core-public by infiniteautomation.
the class PopulateTest method test2.
static void test2() throws JsonException, IOException {
Subclass2 subclass2 = new Subclass2();
String json = JsonWriter.writeToString(context, subclass2);
System.out.println(json);
JsonReader reader = new JsonReader(context, json);
reader.readInto(subclass2);
System.out.println(subclass2);
}
use of com.serotonin.json.JsonReader in project ma-core-public by infiniteautomation.
the class ReadTest method read.
static void read(String data, Type type) throws Exception {
JsonReader reader = new JsonReader(context, data);
while (!reader.isDone()) {
Object p = reader.read(type);
System.out.println(p);
}
}
use of com.serotonin.json.JsonReader in project ma-core-public by infiniteautomation.
the class Restorer method restore.
/**
* Restore the object based on the Audit Trail
* @return
*/
public T restore() {
T vo = null;
try {
// Follow the trail
for (AuditEventInstanceVO audit : trail) {
JsonObject context = audit.getContext();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, context);
if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_CREATE) {
vo = this.build(audit.getObjectId(), context, reader);
} else if (audit.getChangeType() == AuditEventInstanceVO.CHANGE_TYPE_MODIFY) {
if (vo == null)
vo = getExisting(audit.getObjectId());
vo = this.build(vo, context, reader);
}
}
ProcessResult voResponse = new ProcessResult();
vo.validate(voResponse);
if (voResponse.getHasMessages())
copyValidationMessages(voResponse, "restore.prefix", vo.getXid());
else {
addSuccessMessage(vo.isNew(), "restore.prefix", vo.getXid());
}
} catch (TranslatableJsonException e) {
addFailureMessage("restore.prefix", "need-to-fill-in", e.getMsg());
} catch (JsonException e) {
addFailureMessage("restoring.prefix", "need-to-fill-in", getJsonExceptionMessage(e));
} catch (Exception e) {
addFailureMessage("restoring.prefix", "need-to-fill-in", e.getMessage());
}
return vo;
}
use of com.serotonin.json.JsonReader in project ma-core-public by infiniteautomation.
the class SerotoninJsonMessageConverter method readInternal.
/* (non-Javadoc)
* @see org.springframework.http.converter.AbstractHttpMessageConverter#readInternal(java.lang.Class, org.springframework.http.HttpInputMessage)
*/
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
InputStreamReader isReader = new InputStreamReader(inputMessage.getBody());
JsonTypeReader typeReader = new JsonTypeReader(isReader);
try {
JsonValue value = typeReader.read();
if (clazz.equals(JsonValue.class))
return value;
// First get the definition for the model so we can create a real object
ModelDefinition def = findModelDefinition(clazz);
AbstractRestModel<?> model = def.createModel();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, value);
if (value instanceof JsonObject) {
// TODO Should do some pre-validation or something to ensure we are
// importing the right thing?
JsonObject root = value.toJsonObject();
if (model != null) {
Object data = model.getData();
reader.readInto(data, root);
return model;
} else {
// Catchall
return root.toNative();
}
} else {
throw new IOException("Huh?");
}
} catch (JsonException e) {
throw new IOException(e);
}
}
use of com.serotonin.json.JsonReader 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