Search in sources :

Example 11 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 12 with JsonReader

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

the class ToolsTest method getValueFromJsonObject_input_key_null.

// 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_null() {
    // 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 = null;
    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 13 with JsonReader

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

the class ToolsTest method getNumberValueFromJsonObject_input_object_null.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_018: [The function shall return zero if any of the input is null]
@Test
public void getNumberValueFromJsonObject_input_object_null() {
    // 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 = "";
    long expResult = 0;
    // Act
    long result = Tools.getNumberValueFromJsonObject(null, 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 14 with JsonReader

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

the class ToolsTest method getNumberValueFromJsonObject_jsonvalue_null.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_019: [The function shall get the JsonValue of the key and return zero if it is null]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_020: [The function shall get the JsonNumber from the JsonValue and return zero if it is null]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_020: [The function shall get the JsonNumber from the JsonValue and return zero if it is null]
@Test
public void getNumberValueFromJsonObject_jsonvalue_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";
    long expResult = 0;
    // Act
    long result = Tools.getNumberValueFromJsonObject(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 15 with JsonReader

use of javax.json.JsonReader in project sling by apache.

the class JsonContentParser method toJsonObjectWithJsonTicks.

private JsonObject toJsonObjectWithJsonTicks(InputStream is) {
    String jsonString;
    try {
        jsonString = IOUtils.toString(is, CharEncoding.UTF_8);
    } catch (IOException ex) {
        throw new ParseException("Error getting JSON string.", ex);
    }
    // convert ticks to double quotes
    jsonString = JsonTicksConverter.tickToDoubleQuote(jsonString);
    try (JsonReader reader = jsonReaderFactory.createReader(new StringReader(jsonString))) {
        return reader.readObject();
    } catch (JsonParsingException ex) {
        throw new ParseException("Error parsing JSON content: " + ex.getMessage(), ex);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString) IOException(java.io.IOException) ParseException(org.apache.sling.jcr.contentparser.ParseException) JsonParsingException(javax.json.stream.JsonParsingException)

Aggregations

JsonReader (javax.json.JsonReader)30 JsonObject (javax.json.JsonObject)25 StringReader (java.io.StringReader)22 Test (org.junit.Test)18 JsonArray (javax.json.JsonArray)14 JsonString (javax.json.JsonString)12 File (java.io.File)3 JsonValue (javax.json.JsonValue)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonException (javax.json.JsonException)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)1 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)1 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)1 AttributeReference (com.torodb.core.language.AttributeReference)1 DefaultNamedToroIndex (com.torodb.core.model.DefaultNamedToroIndex)1 IndexedAttributes (com.torodb.core.model.IndexedAttributes)1