Search in sources :

Example 36 with JsonArray

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

the class CollectionConverter method jsonWrite.

@Override
public JsonValue jsonWrite(JsonTypeWriter writer, Object value) throws JsonException {
    JsonArray jsonArray = new JsonArray();
    Iterator<?> iterator = ((Collection<?>) value).iterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        jsonArray.add(writer.writeObject(o));
    }
    return jsonArray;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) Collection(java.util.Collection)

Example 37 with JsonArray

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

the class JsonEmportScriptUtility method doExclusions.

private void doExclusions(JsonObject jo) {
    for (JsonImportExclusion exclusion : importExclusions) {
        if (jo.containsKey(exclusion.getImporterType())) {
            JsonArray ja = jo.getJsonArray(exclusion.getImporterType());
            int size = ja.size();
            for (int k = 0; k < size; k += 1) {
                JsonObject obj = ja.getJsonObject(k);
                if (obj.containsKey(exclusion.getKey()) && obj.getString(exclusion.getKey()).equals(exclusion.getValue())) {
                    ja.remove(k);
                    k -= 1;
                    size -= 1;
                }
            }
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonObject(com.serotonin.json.type.JsonObject)

Example 38 with JsonArray

use of com.serotonin.json.type.JsonArray 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);
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonObject(com.serotonin.json.type.JsonObject) JsonBoolean(com.serotonin.json.type.JsonBoolean) JsonString(com.serotonin.json.type.JsonString) JsonString(com.serotonin.json.type.JsonString)

Example 39 with JsonArray

use of com.serotonin.json.type.JsonArray 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");
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonString(com.serotonin.json.type.JsonString)

Aggregations

JsonArray (com.serotonin.json.type.JsonArray)39 JsonValue (com.serotonin.json.type.JsonValue)26 JsonObject (com.serotonin.json.type.JsonObject)17 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)14 JsonWriter (com.serotonin.json.JsonWriter)10 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6 JsonString (com.serotonin.json.type.JsonString)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ArrayList (java.util.ArrayList)5 JsonException (com.serotonin.json.JsonException)4 User (com.serotonin.m2m2.vo.User)4 JsonBoolean (com.serotonin.json.type.JsonBoolean)3 IOException (java.io.IOException)3 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)2