Search in sources :

Example 6 with JsonReader

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);
}
Also used : JsonReader(com.serotonin.json.JsonReader)

Example 7 with JsonReader

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);
    }
}
Also used : JsonReader(com.serotonin.json.JsonReader)

Example 8 with JsonReader

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;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) JsonReader(com.serotonin.json.JsonReader) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AuditEventInstanceVO(com.serotonin.m2m2.vo.event.audit.AuditEventInstanceVO)

Example 9 with JsonReader

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);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) InputStreamReader(java.io.InputStreamReader) JsonValue(com.serotonin.json.type.JsonValue) ModelDefinition(com.serotonin.m2m2.module.ModelDefinition) JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 10 with JsonReader

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

JsonReader (com.serotonin.json.JsonReader)13 JsonObject (com.serotonin.json.type.JsonObject)5 JsonException (com.serotonin.json.JsonException)4 JsonContext (com.serotonin.json.JsonContext)3 JsonValue (com.serotonin.json.type.JsonValue)3 JsonWriter (com.serotonin.json.JsonWriter)2 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)2 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 StringWriter (java.io.StringWriter)2 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 ThrowableSerializingConverter (com.serotonin.json.convert.ThrowableSerializingConverter)1 TypeResolver (com.serotonin.json.spi.TypeResolver)1 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ModelDefinition (com.serotonin.m2m2.module.ModelDefinition)1