Search in sources :

Example 36 with JsonObject

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

the class VarNames method jsonWriteVarContext.

public static void jsonWriteVarContext(ObjectWriter writer, List<IntStringPair> context) throws IOException, JsonException {
    DataPointDao dataPointDao = DataPointDao.instance;
    JsonArray pointList = new JsonArray();
    for (IntStringPair p : context) {
        JsonObject point = new JsonObject();
        pointList.add(point);
        point.put("varName", new JsonString(p.getValue()));
        point.put("dataPointXid", new JsonString(dataPointDao.getXidById(p.getKey())));
    }
    writer.writeEntry("context", pointList);
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString)

Example 37 with JsonObject

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

the class VarNames method jsonReadVarContext.

public static void jsonReadVarContext(JsonObject json, List<IntStringPair> context) 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.meta.missing", "dataPointXid");
            Integer dpid = dataPointDao.getIdByXid(xid);
            if (dpid == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            String var = jo.getString("varName");
            if (var == null)
                throw new TranslatableJsonException("emport.error.meta.missing", "varName");
            context.add(new IntStringPair(dpid, var));
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonString(com.serotonin.json.type.JsonString)

Example 38 with JsonObject

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

the class JsonObjectConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonObject jsonObject = (JsonObject) obj;
    jsonObject.clear();
    jsonObject.putAll(jsonValue.toJsonObject());
}
Also used : JsonObject(com.serotonin.json.type.JsonObject)

Example 39 with JsonObject

use of com.serotonin.json.type.JsonObject 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 40 with JsonObject

use of com.serotonin.json.type.JsonObject 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)

Aggregations

JsonObject (com.serotonin.json.type.JsonObject)44 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)24 JsonValue (com.serotonin.json.type.JsonValue)20 JsonArray (com.serotonin.json.type.JsonArray)17 JsonException (com.serotonin.json.JsonException)15 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)11 JsonString (com.serotonin.json.type.JsonString)7 IOException (java.io.IOException)7 IntStringPair (com.serotonin.db.pair.IntStringPair)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6 JsonReader (com.serotonin.json.JsonReader)5 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)5 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 ArrayList (java.util.ArrayList)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 User (com.serotonin.m2m2.vo.User)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2