Search in sources :

Example 1 with JsonException

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

the class SerotoninJsonValueSerializer method serialize.

@Override
public void serialize(JsonValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, new SerotoninToJacksonJsonWriter(jgen));
    // TODO Make configurable
    writer.setPrettyIndent(3);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(value);
    } catch (JsonException e) {
        throw new IOException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter)

Example 2 with JsonException

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

the class AuditEventInstanceModel method getContext.

@JsonRawValue
public String getContext() {
    // Since the JsonData table can contain JSON within the context, return raw JSON all the time here
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
    writer.setPrettyIndent(3);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(this.data.getContext());
        return stringWriter.toString();
    } catch (JsonException e) {
        throw new ShouldNeverHappenException(e);
    } catch (IOException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonRawValue(com.fasterxml.jackson.annotation.JsonRawValue)

Example 3 with JsonException

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

the class PointLinkEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject pointLink = jsonValue.toJsonObject();
    PointLinkDao pointLinkDao = PointLinkDao.instance;
    String xid = pointLink.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = pointLinkDao.generateUniqueXid();
    PointLinkVO vo = pointLinkDao.getPointLink(xid);
    if (vo == null) {
        vo = new PointLinkVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, pointLink);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        vo.validate(voResponse);
        if (voResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(voResponse, "emport.pointLink.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = vo.isNew();
            RTMDefinition.instance.savePointLink(vo);
            importContext.addSuccessMessage(isnew, "emport.pointLink.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.pointLink.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.pointLink.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
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) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 4 with JsonException

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

the class WatchlistEmportDefinitionNoCase method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject watchListJson = jsonValue.toJsonObject();
    String xid = watchListJson.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = WatchListDao.instance.generateUniqueXid();
    WatchListVO watchList = WatchListDao.instance.getWatchList(xid);
    if (watchList == null) {
        watchList = new WatchListVO();
        watchList.setXid(xid);
    }
    try {
        importContext.getReader().readInto(watchList, watchListJson);
        // Now validate it. Use a new response object so we can distinguish errors in this user from other
        // errors.
        ProcessResult watchListResponse = new ProcessResult();
        watchList.validate(watchListResponse);
        if (watchListResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(watchListResponse, "emport.watchList.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = watchList.getId() == Common.NEW_ID;
            WatchListDao.instance.saveWatchList(watchList);
            importContext.addSuccessMessage(isnew, "emport.watchList.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.watchList.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
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) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 5 with JsonException

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

the class MaintenanceEventEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    MaintenanceEventDao maintenanceEventDao = new MaintenanceEventDao();
    JsonObject maintenanceEvent = jsonValue.toJsonObject();
    String xid = maintenanceEvent.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = maintenanceEventDao.generateUniqueXid();
    MaintenanceEventVO vo = maintenanceEventDao.getMaintenanceEvent(xid);
    if (vo == null) {
        vo = new MaintenanceEventVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, maintenanceEvent);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        vo.validate(voResponse);
        if (voResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(voResponse, "emport.maintenanceEvent.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = vo.isNew();
            RTMDefinition.instance.saveMaintenanceEvent(vo);
            importContext.addSuccessMessage(isnew, "emport.maintenanceEvent.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
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) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

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