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));
}
}
}
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));
}
}
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));
}
}
}
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);
}
}
}
}
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();
}
Aggregations