Search in sources :

Example 16 with JsonTypeReader

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

the class StoreLatestSnapshotTest method testThreeTwoReleaseVersion.

// @Test
public void testThreeTwoReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "3.2.1");
    jsonModules.put("mangoApi", "3.2.1");
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    printResponse(response);
    Assert.assertNull(response.getJsonValue("versionState"));
    // Assert newInstalls-oldCore should be empty
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() == 0);
    // Assert update-versionState
    Assert.assertNull(response.getJsonValue("update-versionState"));
    // Assert updates for this core iff major upgrade available
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() == 0);
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() == 1);
    Assert.assertEquals(true, upgrades.get(0).getJsonValue("name").toString().equals("mangoApi"));
    Assert.assertEquals(true, upgrades.get(0).getJsonValue("fullVersion").toString().equals("3.2.2"));
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 17 with JsonTypeReader

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

the class StreamTest method main.

public static void main(String[] args) throws Exception {
    StringWriter out = new StringWriter();
    JsonStreamWriter w = new JsonStreamWriter(out);
    w.startObject();
    {
        w.writeObjectString("command", "echo");
        w.startObjectArray("parameters");
        {
            w.writeArrayNull();
            w.writeArrayNumber(123);
            w.writeArrayString("str");
            w.writeArrayBoolean(true);
            w.startArrayObject();
            {
                w.writeObjectString("nullStr", null);
                w.writeObjectString("quote", "\"'\t");
            }
            w.endObject();
        }
        w.endArray();
    }
    w.endObject();
    System.out.println(out);
    String s = "{\"id\":\"1234\",\"result\":{\"node\":{\"path\":\"/DGBox/Data Sources/Anders\",\"name\":\"Anders\",\"hasChildren\":true,\"icon\":\"../../../modules/modbus/web/modbusIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/bnip\",\"name\":\"bnip\",\"hasChildren\":true,\"icon\":\"../../../modules/BACnet/web/bacnetIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/bnmstp\",\"name\":\"bnmstp\",\"hasChildren\":false,\"icon\":\"../../../modules/BACnet/web/bacnetIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Calculations\",\"name\":\"Calculations\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Cylon test\",\"name\":\"Cylon test\",\"hasChildren\":true,\"icon\":\"../../../modules/BACnet/web/bacnetIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Discover test\",\"name\":\"Discover test\",\"hasChildren\":true,\"icon\":\"../../../modules/modbus/web/modbusIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/EnOcean 315\",\"name\":\"EnOcean 315\",\"hasChildren\":true,\"icon\":\"../../../modules/enocean/web/enoceanIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/EnOcean 902\",\"name\":\"EnOcean 902\",\"hasChildren\":true,\"icon\":\"../../../modules/enocean/web/enoceanIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/House lights\",\"name\":\"House lights\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/http\",\"name\":\"http\",\"hasChildren\":true,\"icon\":\"../../../modules/http/web/httpIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Insteon\",\"name\":\"Insteon\",\"hasChildren\":true,\"icon\":\"../../../modules/insteon/web/insteon_5.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Internal\",\"name\":\"Internal\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/KNX\",\"name\":\"KNX\",\"hasChildren\":true,\"icon\":\"../../../modules/knx/web/knx.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/MatthewWS\",\"name\":\"MatthewWS\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Maverick\",\"name\":\"Maverick\",\"hasChildren\":true,\"icon\":\"../../../modules/mamac/web/mamacIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/mbus test\",\"name\":\"mbus test\",\"hasChildren\":true,\"icon\":\"../../../modules/mbus/web/mbusIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Modbus Serial\",\"name\":\"Modbus Serial\",\"hasChildren\":true,\"icon\":\"../../../modules/modbus/web/modbusIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/OPC\",\"name\":\"OPC\",\"hasChildren\":true,\"icon\":\"../../../modules/opcda/web/opcIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Set test\",\"name\":\"Set test\",\"hasChildren\":true,\"icon\":\"../../../modules/BACnet/web/bacnetIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/SNMP\",\"name\":\"SNMP\",\"hasChildren\":true,\"icon\":\"../../../modules/snmp/web/snmpIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/SQL\",\"name\":\"SQL\",\"hasChildren\":true,\"icon\":\"../../../modules/sqlds/web/sqlIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/test\",\"name\":\"test\",\"hasChildren\":true,\"icon\":\"../../../modules/mamac/web/mamacIcon.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/virt test\",\"name\":\"virt test\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"},\"node\":{\"path\":\"/DGBox/Data Sources/Virtual\",\"name\":\"Virtual\",\"hasChildren\":true,\"icon\":\"../../../images/icon_ds.png\"}}}";
    System.out.println(new JsonTypeReader(s).read());
}
Also used : StringWriter(java.io.StringWriter) JsonStreamWriter(com.serotonin.json.JsonStreamWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 18 with JsonTypeReader

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

the class LengthTest method main.

public static void main(String[] args) throws Exception {
    String data = "{\"bigDecimal\":1.1,\"bigInteger\":2,\"boolean1\":false,\"byte1\":-4,\"double1\":5,\"float1\":6.1,\"int1\":7,\"long1\":-8,\"short1\":9,\"string1\":\"i'm a read string\"}";
    System.out.println(data.length());
    new JsonTypeReader(new StringReader(data), -1).read();
    new JsonTypeReader(new StringReader(data), 150).read();
    new JsonTypeReader(new StringReader(data), 149).read();
    new JsonTypeReader(new StringReader(data), 148).read();
    new JsonTypeReader(new StringReader(data), 147).read();
    new JsonTypeReader(new StringReader(data), 146).read();
    new JsonTypeReader(new StringReader(data), 100).read();
}
Also used : StringReader(java.io.StringReader) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 19 with JsonTypeReader

use of com.serotonin.json.type.JsonTypeReader 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;
}
Also used : JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonReader(com.serotonin.json.JsonReader) IOException(java.io.IOException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Aggregations

JsonTypeReader (com.serotonin.json.type.JsonTypeReader)19 JsonValue (com.serotonin.json.type.JsonValue)15 StringWriter (java.io.StringWriter)13 JsonWriter (com.serotonin.json.JsonWriter)12 HttpPost (org.apache.http.client.methods.HttpPost)12 StringEntity (org.apache.http.entity.StringEntity)12 JsonArray (com.serotonin.json.type.JsonArray)10 JsonObject (com.serotonin.json.type.JsonObject)7 JsonException (com.serotonin.json.JsonException)4 IOException (java.io.IOException)4 Version (com.github.zafarkhaja.semver.Version)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 JsonReader (com.serotonin.json.JsonReader)2 JsonString (com.serotonin.json.type.JsonString)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 HashMap (java.util.HashMap)2 StringStringPair (com.serotonin.db.pair.StringStringPair)1 JsonStreamWriter (com.serotonin.json.JsonStreamWriter)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)1