Search in sources :

Example 56 with JsonReader

use of javax.json.JsonReader in project azure-iot-sdk-java by Azure.

the class ToolsTest method getValueFromJsonObject_value_null.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_014: [The function shall return empty string if the JsonValue is null]
@Test
public void getValueFromJsonObject_value_null() {
    // Arrange
    String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":null,\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"Disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
    StringReader stringReader = new StringReader(jsonString);
    JsonReader jsonReader = Json.createReader(stringReader);
    JsonObject jsonObject = jsonReader.readObject();
    String key = "generationId";
    String expResult = "";
    // Act
    String result = Tools.getValueFromJsonObject(jsonObject, key);
    // Assert
    assertEquals(expResult, result);
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 57 with JsonReader

use of javax.json.JsonReader in project mysql_perf_analyzer by yahoo.

the class AlertDefinition method createFromJson.

public static AlertDefinition createFromJson(java.io.InputStream in) {
    JsonReader jsonReader = null;
    AlertDefinition alert = null;
    try {
        jsonReader = javax.json.Json.createReader(in);
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();
        alert = new AlertDefinition(jsonObject.getString("name"));
        alert.setSource(jsonObject.getString("source"));
        alert.setMetricName(jsonObject.getString("metricName", null));
        alert.setMetricValueType(jsonObject.getString("metricValueType", null));
        alert.setMetricComparison(jsonObject.getString("metricComparison", null));
        try {
            alert.setDefaultThreshold(Float.parseFloat(jsonObject.getString("defaultThreshold", null)));
        } catch (Exception ex) {
        }
        alert.setSqlId(jsonObject.getString("sqlId", null));
        alert.setSqlText(jsonObject.getString("sqlText", null));
        JsonArray params = jsonObject.getJsonArray("params");
        if (params != null) {
            int mlen = params.size();
            for (int i = 0; i < mlen; i++) {
                JsonObject mobj = params.getJsonObject(i);
                alert.addParam(mobj.getString("name"), mobj.getString("defaultValue"));
            }
        }
        return alert;
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error to parse UDM", ex);
    }
    return null;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) UserDefinedMetricsException(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics.UserDefinedMetricsException)

Example 58 with JsonReader

use of javax.json.JsonReader in project mysql_perf_analyzer by yahoo.

the class SNMPSettings method readConfig.

private synchronized boolean readConfig() {
    File cfgFile = new File(this.myperfSnmpConfigPath);
    if (!cfgFile.exists()) {
        logger.info("There is no customized snmp configuration file");
        return true;
    }
    FileInputStream in = null;
    try {
        in = new FileInputStream(cfgFile);
        JsonReader jr = javax.json.Json.createReader(in);
        JsonObject jsonObject = jr.readObject();
        jr.close();
        this.setSiteSetting(readFromJson(jsonObject.getJsonObject("site")));
        JsonArray groups = jsonObject.getJsonArray("group");
        if (groups != null) {
            int len = groups.size();
            for (int i = 0; i < len; i++) {
                JsonObject grp = groups.getJsonObject(i);
                SNMPSetting grpSetting = readFromJson(grp);
                String grpName = grp.getString("dbgroup", null);
                if (grpName != null && grpSetting != null)
                    this.groupSettings.put(grpName, grpSetting);
            }
        }
        JsonArray hosts = jsonObject.getJsonArray("host");
        if (hosts != null) {
            int len = hosts.size();
            for (int i = 0; i < len; i++) {
                JsonObject host = hosts.getJsonObject(i);
                SNMPSetting hostSetting = readFromJson(host);
                String hostName = host.getString("dbhost", null);
                if (hostName != null && hostSetting != null)
                    this.hostSettings.put(hostName, hostSetting);
            }
        }
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to read SNMP configuration file " + cfgFile.getAbsolutePath(), ex);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception fex) {
            }
            ;
        }
    }
    return false;
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 59 with JsonReader

use of javax.json.JsonReader in project azure-iot-sdk-java by Azure.

the class ToolsTest method getValueFromJsonObject_input_key_empty.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_010: [The function shall return empty string if any of the input is null]
@Test
public void getValueFromJsonObject_input_key_empty() {
    // Arrange
    String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":\"111111111111111111\",\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
    StringReader stringReader = new StringReader(jsonString);
    JsonReader jsonReader = Json.createReader(stringReader);
    JsonObject jsonObject = jsonReader.readObject();
    String key = "";
    String expResult = "";
    // Act
    String result = Tools.getValueFromJsonObject(jsonObject, key);
    // Assert
    assertEquals(expResult, result);
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 60 with JsonReader

use of javax.json.JsonReader in project azure-iot-sdk-java by Azure.

the class ToolsTest method getValueFromJsonObject_good_case.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_011: [The function shall get the JsonValue of the key]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_012: [The function shall get the JsonString from the JsonValue if the JsonValue is not null]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_013: [The function shall return the string value from the JsonString calling the getValueFromJsonString function]
@Test
public void getValueFromJsonObject_good_case() {
    // Arrange
    String jsonString = "{\"deviceId\":\"xxx-device\",\"generationId\":\"111111111111111111\",\"etag\":\"MA==\",\"connectionState\":\"Disconnected\",\"status\":\"disabled\",\"statusReason\":null,\"connectionStateUpdatedTime\":\"0001-01-01T00:00:00\",\"statusUpdatedTime\":\"0001-01-01T00:00:00\",\"lastActivityTime\":\"0001-01-01T00:00:00\",\"cloudToDeviceMessageCount\":0,\"authentication\":{\"symmetricKey\":{\"primaryKey\":\"AAABBBCCC111222333444000\",\"secondaryKey\":\"111222333444555AAABBBCCC\"}}}";
    StringReader stringReader = new StringReader(jsonString);
    JsonReader jsonReader = Json.createReader(stringReader);
    JsonObject jsonObject = jsonReader.readObject();
    String key = "generationId";
    String expResult = "111111111111111111";
    // Act
    String result = Tools.getValueFromJsonObject(jsonObject, key);
    // Assert
    assertEquals(expResult, result);
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) Test(org.junit.Test)

Aggregations

JsonReader (javax.json.JsonReader)130 JsonObject (javax.json.JsonObject)110 StringReader (java.io.StringReader)78 Test (org.junit.Test)47 JsonArray (javax.json.JsonArray)44 JsonString (javax.json.JsonString)42 HashMap (java.util.HashMap)21 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)13 File (java.io.File)10 LinkedHashMap (java.util.LinkedHashMap)10 JsonParser (edu.harvard.iq.dataverse.util.json.JsonParser)9 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)8 InputStream (java.io.InputStream)7 Gson (com.google.gson.Gson)6 AsyncCompletionHandler (com.ning.http.client.AsyncCompletionHandler)6 Response (com.ning.http.client.Response)6 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)5