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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations