use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.
the class ScriptContextVariable method jsonWriteVarContext.
public static void jsonWriteVarContext(ObjectWriter writer, List<ScriptContextVariable> context) throws IOException, JsonException {
DataPointDao dataPointDao = DataPointDao.instance;
JsonArray pointList = new JsonArray();
for (ScriptContextVariable p : context) {
String xid = dataPointDao.getXidById(p.getDataPointId());
JsonObject point = new JsonObject();
pointList.add(point);
point.put("varName", new JsonString(p.getVariableName()));
point.put("dataPointXid", new JsonString(xid));
point.put("updateContext", new JsonBoolean(p.isContextUpdate()));
}
writer.writeEntry("context", pointList);
}
use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.
the class ScriptContextVariable method jsonReadVarContext.
/**
* Read in context,
* @param json
* @param context
* @return if my XID is in the context, return the name it has to map into the VO otherwise return null
* @throws JsonException
*/
public static String jsonReadVarContext(JsonObject json, List<ScriptContextVariable> context, boolean isContextUpdate) throws JsonException {
JsonArray jsonContext = json.getJsonArray("context");
if (jsonContext != null) {
context.clear();
DataPointDao dataPointDao = DataPointDao.instance;
for (JsonValue jv : jsonContext) {
JsonObject jo = jv.toJsonObject();
String xid = jo.getString("dataPointXid");
if (xid == null)
throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
Integer dpid = dataPointDao.getIdByXid(xid);
if (dpid == null) {
// This can also happen if the point is in its own context (Bug from legacy systems).
throw new TranslatableJsonException("emport.error.missingPoint", xid);
}
// For compatibility with varName and variableName json types
String var = jo.getString("varName");
if (var == null) {
var = jo.getString("variableName");
if (var == null)
throw new TranslatableJsonException("emport.error.context.missing", "varName");
}
// Default for legacy systems
if (jo.containsKey("updateContext"))
isContextUpdate = jo.getBoolean("updateContext");
context.add(new ScriptContextVariable(dpid, var, isContextUpdate));
}
}
return json.getString("variableName");
}
use of com.serotonin.json.type.JsonObject 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;
}
use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.
the class MapConverter method jsonRead.
@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
JsonObject jsonObject = (JsonObject) jsonValue;
@SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) obj;
Type valueType = TypeUtils.getActualTypeArgument(type, 1);
for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) map.put(entry.getKey(), reader.read(valueType, entry.getValue()));
}
Aggregations