Search in sources :

Example 31 with JsonException

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

the class JsonDataImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    boolean isNew = false;
    if (StringUtils.isBlank(xid)) {
        xid = JsonDataDao.instance.generateUniqueXid();
        isNew = true;
    }
    JsonDataVO vo = JsonDataDao.instance.getByXid(xid);
    if (vo == null) {
        isNew = true;
        vo = new JsonDataVO();
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // 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())
                setValidationMessages(voResponse, "emport.jsondata.prefix", xid);
            else {
                // Sweet. Save it.
                JsonDataDao.instance.save(vo);
                addSuccessMessage(isNew, "emport.jsondata.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.jsondata.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) JsonDataVO(com.serotonin.m2m2.vo.json.JsonDataVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 32 with JsonException

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

the class MailingListImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getMailingListDao().generateUniqueXid();
    MailingList vo = ctx.getMailingListDao().getMailingList(xid);
    if (vo == null) {
        vo = new MailingList();
        vo.setXid(xid);
    }
    try {
        ctx.getReader().readInto(vo, json);
        // 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())
            setValidationMessages(voResponse, "emport.mailingList.prefix", xid);
        else {
            // Sweet. Save it.
            boolean isnew = vo.getId() == Common.NEW_ID;
            ctx.getMailingListDao().saveMailingList(vo);
            addSuccessMessage(isnew, "emport.mailingList.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.mailingList.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.mailingList.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 33 with JsonException

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

the class PublisherImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getPublisherDao().generateUniqueXid();
    PublisherVO<?> vo = ctx.getPublisherDao().getPublisher(xid);
    if (vo == null) {
        String typeStr = json.getString("type");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.publisher.missingType", xid, ModuleRegistry.getPublisherDefinitionTypes());
        else {
            PublisherDefinition def = ModuleRegistry.getPublisherDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.publisher.invalidType", xid, typeStr, ModuleRegistry.getPublisherDefinitionTypes());
            else {
                vo = def.baseCreatePublisherVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // 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())
                setValidationMessages(voResponse, "emport.publisher.prefix", xid);
            else {
                // Sweet. Save it.
                boolean isnew = vo.isNew();
                if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
                    Common.runtimeManager.savePublisher(vo);
                    addSuccessMessage(isnew, "emport.publisher.prefix", xid);
                } else {
                    addFailureMessage(new ProcessMessage("Runtime manager not running publisher with xid : " + vo.getXid() + " not saved."));
                }
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publisher.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publisher.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : PublisherDefinition(com.serotonin.m2m2.module.PublisherDefinition) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 34 with JsonException

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

the class JsonPropertyConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonObject jsonObject = (JsonObject) jsonValue;
    if (jsonSerializable)
        ((JsonSerializable) obj).jsonRead(reader, jsonObject);
    if (properties != null) {
        for (SerializableProperty prop : properties) {
            // Check whether the property should be included
            if (!prop.include(reader.getIncludeHint()))
                continue;
            Method writeMethod = prop.getWriteMethod();
            if (writeMethod == null)
                continue;
            String name = prop.getNameToUse();
            JsonValue propJsonValue = jsonObject.get(name);
            if (propJsonValue == null)
                continue;
            Type propType = writeMethod.getGenericParameterTypes()[0];
            propType = TypeUtils.resolveTypeVariable(type, propType);
            Class<?> propClass = TypeUtils.getRawClass(propType);
            try {
                Object propValue = reader.read(propType, propJsonValue);
                if (propClass.isPrimitive() && propValue == null) {
                    if (propClass == Boolean.TYPE)
                        propValue = false;
                    else
                        propValue = 0;
                }
                prop.getWriteMethod().invoke(obj, propValue);
            } catch (Exception e) {
                throw new JsonException("JsonException writing property '" + prop.getName() + "' of class " + propClass.getName(), e);
            }
        }
    }
}
Also used : JsonException(com.serotonin.json.JsonException) Type(java.lang.reflect.Type) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonObject(com.serotonin.json.type.JsonObject) Method(java.lang.reflect.Method) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException)

Example 35 with JsonException

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

the class JsonPropertyConverter method jsonWrite.

public void jsonWrite(String includeHint, Object value, ObjectWriter objectWriter) throws IOException, JsonException {
    if (jsonSerializable)
        ((JsonSerializable) value).jsonWrite(objectWriter);
    if (properties != null) {
        for (SerializableProperty prop : properties) {
            // Check whether the property should be included
            if (!prop.include(includeHint))
                continue;
            Method readMethod = prop.getReadMethod();
            if (readMethod == null)
                continue;
            String name = prop.getNameToUse();
            Object propertyValue;
            try {
                propertyValue = readMethod.invoke(value);
            } catch (Exception e) {
                throw new JsonException("Error reading '" + prop.getName() + "' from value " + value + " of class " + value.getClass(), e);
            }
            // Check if the value should be ignored.
            boolean ignore = false;
            if (prop.isSuppressDefaultValue()) {
                if (propertyValue == null)
                    ignore = true;
                else {
                    Class<?> propertyClass = readMethod.getReturnType();
                    // Check if this value is the properties default value.
                    if (propertyClass == Boolean.TYPE)
                        ignore = ((Boolean) propertyValue) == false;
                    else if (propertyClass == Double.TYPE)
                        ignore = (Double) propertyValue == 0;
                    else if (propertyClass == Long.TYPE)
                        ignore = (Long) propertyValue == 0;
                    else if (propertyClass == Float.TYPE)
                        ignore = (Float) propertyValue == 0;
                    else if (propertyClass == Integer.TYPE)
                        ignore = (Integer) propertyValue == 0;
                    else if (propertyClass == Short.TYPE)
                        ignore = (Short) propertyValue == 0;
                    else if (propertyClass == Byte.TYPE)
                        ignore = (Byte) propertyValue == 0;
                    else if (propertyClass == Character.TYPE)
                        ignore = (Character) propertyValue == 0;
                }
            }
            if (!ignore)
                objectWriter.writeEntry(name, propertyValue);
        }
    }
    objectWriter.finish();
}
Also used : JsonException(com.serotonin.json.JsonException) SerializableProperty(com.serotonin.json.util.SerializableProperty) Method(java.lang.reflect.Method) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) JsonObject(com.serotonin.json.type.JsonObject)

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