Search in sources :

Example 36 with JsonException

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

the class ArrayConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object array, Type type) throws JsonException {
    JsonArray jsonArray = (JsonArray) jsonValue;
    Class<?> clazz = array.getClass().getComponentType();
    for (int i = 0; i < jsonArray.size(); i++) {
        try {
            Array.set(array, i, reader.read(clazz, jsonArray.get(i)));
        } catch (Exception e) {
            throw new JsonException("JsonException reading array element " + i, e);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonException(com.serotonin.json.JsonException) JsonException(com.serotonin.json.JsonException) IOException(java.io.IOException)

Example 37 with JsonException

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

the class AuditEventType method raiseChangedEvent.

public static void raiseChangedEvent(String auditEventType, AbstractVO<?> from, AbstractVO<?> to) {
    Map<String, Object> context = new HashMap<String, Object>();
    // Find the annotated properties
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findChanges(from, to);
        if (context.size() == 0)
            // If the object wasn't in fact changed, don't raise an event.
            return;
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_MODIFY, auditEventType, to, "event.audit.extended.changed", context);
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 38 with JsonException

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

the class EventDao method saveEvent.

public void saveEvent(EventInstance event) {
    if (event.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT)) {
        AuditEventInstanceVO vo = new AuditEventInstanceVO();
        AuditEventType type = (AuditEventType) event.getEventType();
        vo.setTypeName(type.getEventSubtype());
        vo.setAlarmLevel(event.getAlarmLevel());
        if (type.getRaisingUser() != null)
            vo.setUserId(type.getRaisingUser().getId());
        else
            vo.setUserId(Common.NEW_ID);
        vo.setChangeType(type.getChangeType());
        vo.setObjectId(type.getReferenceId1());
        vo.setTimestamp(event.getActiveTimestamp());
        try {
            vo.setContext(JsonSerializableUtility.convertMapToJsonObject(event.getContext()));
        } catch (JsonException e) {
            LOG.error(e.getMessage(), e);
        }
        vo.setMessage(event.getMessage());
        AuditEventDao.instance.save(vo);
        // Save for use in the cache
        type.setReferenceId2(vo.getId());
    } else {
        if (event.getId() == Common.NEW_ID)
            insertEvent(event);
        else
            updateEvent(event);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) AuditEventInstanceVO(com.serotonin.m2m2.vo.event.audit.AuditEventInstanceVO)

Example 39 with JsonException

use of com.serotonin.json.JsonException 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)

Example 40 with JsonException

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

the class ThrowableSerializingConverter method jsonRead.

@Override
public Object jsonRead(JsonReader reader, JsonValue jsonValue, Type type) throws JsonException {
    String hex = jsonValue.toString();
    byte[] bs = StreamUtils.fromHex(hex);
    ByteArrayInputStream bais = new ByteArrayInputStream(bs);
    try {
        ObjectInputStream ois = new ObjectInputStream(bais);
        return ois.readObject();
    } catch (Exception e) {
        throw new JsonException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonString(com.serotonin.json.type.JsonString) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

JsonException (com.serotonin.json.JsonException)40 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)22 JsonObject (com.serotonin.json.type.JsonObject)18 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)17 IOException (java.io.IOException)17 JsonValue (com.serotonin.json.type.JsonValue)6 JsonWriter (com.serotonin.json.JsonWriter)5 JsonReader (com.serotonin.json.JsonReader)4 HashMap (java.util.HashMap)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 JsonArray (com.serotonin.json.type.JsonArray)3 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)3 SerializableProperty (com.serotonin.json.util.SerializableProperty)3 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 JsonSerializableUtility (com.serotonin.m2m2.util.JsonSerializableUtility)3 StringWriter (java.io.StringWriter)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JsonContext (com.serotonin.json.JsonContext)2 TypeDefinition (com.serotonin.json.util.TypeDefinition)2