Search in sources :

Example 11 with JsonReader

use of com.serotonin.json.JsonReader 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 12 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, Object obj, Type type) throws Exception {
    JsonReader reader = new JsonReader(context, data);
    while (!reader.isDone()) {
        reader.readInto(type, obj);
        System.out.println(obj);
    }
}
Also used : JsonReader(com.serotonin.json.JsonReader)

Example 13 with JsonReader

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

the class EventDetectorRowMapper method mapRow.

/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	 */
@Override
public AbstractEventDetectorVO<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
    EventDetectorDefinition<?> definition = ModuleRegistry.getEventDetectorDefinition(rs.getString(this.firstColumn + 3));
    if (definition == null)
        throw new ShouldNeverHappenException("Event Detector defintion of type: " + rs.getString(this.firstColumn + 3) + " not found.");
    AbstractEventDetectorVO<?> vo = definition.baseCreateEventDetectorVO();
    vo.setId(rs.getInt(this.firstColumn));
    vo.setXid(rs.getString(this.firstColumn + 1));
    vo.setDefinition(definition);
    // Extract the source id
    int sourceIdColumnIndex;
    if (this.sourceIdColumnOffset < 0)
        sourceIdColumnIndex = this.firstColumn + 5 + EventDetectorDao.instance.getSourceIdIndex(definition.getSourceTypeName());
    else
        sourceIdColumnIndex = this.firstColumn + this.sourceIdColumnOffset;
    vo.setSourceId(rs.getInt(sourceIdColumnIndex));
    // Read Into Detector
    JsonTypeReader typeReader = new JsonTypeReader(rs.getString(this.firstColumn + 4));
    try {
        JsonValue value = typeReader.read();
        JsonObject root = value.toJsonObject();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        root.remove("handlers");
        reader.readInto(vo);
    } catch (ClassCastException | IOException | JsonException e) {
        LOG.error(e.getMessage(), e);
    }
    return vo;
}
Also used : JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonReader(com.serotonin.json.JsonReader) IOException(java.io.IOException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

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