Search in sources :

Example 1 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-modules-public by infiniteautomation.

the class SerotoninJsonValueSerializer method serialize.

@Override
public void serialize(JsonValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, new SerotoninToJacksonJsonWriter(jgen));
    // TODO Make configurable
    writer.setPrettyIndent(3);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(value);
    } catch (JsonException e) {
        throw new IOException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter)

Example 2 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-modules-public by infiniteautomation.

the class AuditEventInstanceModel method getContext.

@JsonRawValue
public String getContext() {
    // Since the JsonData table can contain JSON within the context, return raw JSON all the time here
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
    writer.setPrettyIndent(3);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(this.data.getContext());
        return stringWriter.toString();
    } catch (JsonException e) {
        throw new ShouldNeverHappenException(e);
    } catch (IOException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonRawValue(com.fasterxml.jackson.annotation.JsonRawValue)

Example 3 with JsonWriter

use of com.serotonin.json.JsonWriter in project ma-modules-public by infiniteautomation.

the class M2MReportImportDwr method generateJson.

@DwrPermission(admin = true)
public ProcessResult generateJson(String driverClassname, String connectionUrl, String username, String password) {
    ProcessResult result = new ProcessResult();
    // Validate the connection
    try {
        DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
        Connection connection = DriverManager.getConnection(connectionUrl, username, password);
        connection.setAutoCommit(false);
        // Test the connection.
        DatabaseMetaData md = connection.getMetaData();
        String productName = md.getDatabaseProductName();
        DatabaseType type = DatabaseType.DERBY;
        if (productName.equalsIgnoreCase("mysql")) {
            type = DatabaseType.MYSQL;
        } else if (productName.equalsIgnoreCase("Apache Derby")) {
            type = DatabaseType.DERBY;
        } else if (productName.contains("Microsoft")) {
            type = DatabaseType.MSSQL;
        } else if (productName.equalsIgnoreCase("h2")) {
            type = DatabaseType.H2;
        } else if (productName.equalsIgnoreCase("postgressql")) {
            type = DatabaseType.MYSQL;
        }
        // Get the reports
        M2MReportDao dao = new M2MReportDao(connection, type);
        List<M2MReportVO> legacyReports = dao.getReports();
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        jsonWriter.setPrettyIndent(3);
        jsonWriter.setPrettyOutput(true);
        int cnt = 0;
        jsonWriter.append("{");
        jsonWriter.indent();
        jsonWriter.quote("reports");
        jsonWriter.append(": [");
        // Convert the reports to our VOs
        for (M2MReportVO legacyReport : legacyReports) {
            legacyReport.jsonWrite(jsonWriter, dao);
            cnt++;
            if (cnt < legacyReports.size())
                jsonWriter.append(",");
        }
        jsonWriter.append(']');
        jsonWriter.append("}");
        jsonWriter.flush();
        result.addData("reports", stringWriter.toString());
    } catch (Exception e) {
        result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
        return result;
    }
    return result;
}
Also used : DatabaseType(com.serotonin.m2m2.db.DatabaseProxy.DatabaseType) StringWriter(java.io.StringWriter) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) JsonWriter(com.serotonin.json.JsonWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with JsonWriter

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

the class StoreLatestProductionTest method testThreeThreeReleaseVersion.

public void testThreeThreeReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "3.3.0");
    jsonModules.put("mangoApi", "3.3.0");
    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.assertNotNull(response.getJsonValue("versionState"));
    Assert.assertEquals("PRODUCTION", response.getJsonValue("versionState").toString());
    // 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() > 0);
    for (JsonValue upgrade : upgrades) {
        Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
    // 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 5 with JsonWriter

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

the class StoreLatestProductionTest method printResponse.

/**
 * Helper to print the json neatly
 * @param response
 * @throws IOException
 * @throws JsonException
 */
protected void printResponse(JsonValue response) throws IOException, JsonException {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(JSON_CONTEXT, stringWriter);
    jsonWriter.setPrettyOutput(true);
    jsonWriter.setPrettyIndent(4);
    jsonWriter.writeObject(response);
    System.out.print(stringWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.serotonin.json.JsonWriter)

Aggregations

JsonWriter (com.serotonin.json.JsonWriter)29 StringWriter (java.io.StringWriter)26 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)12 HttpPost (org.apache.http.client.methods.HttpPost)12 StringEntity (org.apache.http.entity.StringEntity)12 JsonArray (com.serotonin.json.type.JsonArray)10 JsonValue (com.serotonin.json.type.JsonValue)10 IOException (java.io.IOException)7 JsonException (com.serotonin.json.JsonException)6 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)5 HashMap (java.util.HashMap)4 Version (com.github.zafarkhaja.semver.Version)2 JsonContext (com.serotonin.json.JsonContext)2 JsonReader (com.serotonin.json.JsonReader)2 JsonObject (com.serotonin.json.type.JsonObject)2 JsonString (com.serotonin.json.type.JsonString)2 Module (com.serotonin.m2m2.module.Module)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 OutputStreamWriter (java.io.OutputStreamWriter)2 JsonRawValue (com.fasterxml.jackson.annotation.JsonRawValue)1