Search in sources :

Example 16 with JsonException

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

the class JsonSerializableUtility method gatherJsonPropertyNames.

private Map<String, JsonProperty> gatherJsonPropertyNames(Class<?> clazz) throws JsonException {
    // Ignore Object.
    if (clazz == Object.class)
        return null;
    Map<String, JsonProperty> jsonProperties = new HashMap<>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        JsonProperty anno = field.getAnnotation(JsonProperty.class);
        if (anno != null)
            jsonProperties.put(field.getName(), anno);
    }
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        JsonProperty anno = method.getAnnotation(JsonProperty.class);
        if (anno == null)
            continue;
        // Convert the method name to a property name using the JavaBean rules.
        String name = method.getName();
        if (method.getReturnType() == Boolean.TYPE) {
            if (!name.startsWith("is"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(2)) + name.substring(3);
        } else {
            if (!name.startsWith("get") && !name.startsWith("set"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
        }
        jsonProperties.put(name, anno);
    }
    return jsonProperties;
}
Also used : JsonException(com.serotonin.json.JsonException) Field(java.lang.reflect.Field) JsonProperty(com.serotonin.json.spi.JsonProperty) HashMap(java.util.HashMap) Method(java.lang.reflect.Method)

Example 17 with JsonException

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

the class AuditEventType method raiseDeletedEvent.

public static void raiseDeletedEvent(String auditEventType, AbstractVO<?> o) {
    Map<String, Object> context = new HashMap<String, Object>();
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findValues(o);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_DELETE, auditEventType, o, "event.audit.extended.deleted", 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 18 with JsonException

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

the class AuditEventType method raiseAddedEvent.

public static void raiseAddedEvent(String auditEventType, AbstractVO<?> o) {
    Map<String, Object> context = new HashMap<String, Object>();
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findValues(o);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_CREATE, auditEventType, o, "event.audit.extended.added", 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 19 with JsonException

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

the class GraphicalViewEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject viewJson = jsonValue.toJsonObject();
    String xid = viewJson.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = graphicalViewDao.generateUniqueXid();
    ensureDao();
    GraphicalView view = graphicalViewDao.getViewByXid(xid);
    if (view == null) {
        view = new GraphicalView();
        view.setXid(xid);
    }
    try {
        importContext.getReader().readInto(view, viewJson);
        // Now validate it. Use a new response object so we can distinguish errors in this view from other
        // errors.
        ProcessResult viewResponse = new ProcessResult();
        view.validate(viewResponse);
        if (viewResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(viewResponse, "emport.view.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = view.isNew();
            graphicalViewDao.saveView(view);
            importContext.addSuccessMessage(isnew, "emport.view.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.view.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.view.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 20 with JsonException

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

the class ReportEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext) throws JsonException {
    JsonObject reportJson = jsonValue.toJsonObject();
    String xid = reportJson.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ReportDao.instance.generateUniqueXid();
    ReportVO report = null;
    try {
        report = ReportDao.instance.getReport(xid);
    } catch (IncorrectResultSizeDataAccessException e) {
        importContext.getResult().addGenericMessage("reports.emport.duplicateXids", xid);
        return;
    }
    if (report == null) {
        report = new ReportVO();
        report.setXid(xid);
    }
    try {
        importContext.getReader().readInto(report, reportJson);
        // Now validate it. Use a new response object so we can distinguish errors in this user from other
        // errors.
        ProcessResult reportResponse = new ProcessResult();
        report.validate(reportResponse);
        if (reportResponse.getHasMessages())
            // Too bad. Copy the errors into the actual response.
            importContext.copyValidationMessages(reportResponse, "emport.report.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = report.getId() == Common.NEW_ID;
            ReportDao.instance.saveReport(report);
            importContext.addSuccessMessage(isnew, "emport.report.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.report.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.report.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO)

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