Search in sources :

Example 16 with JsonString

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

the class ToolsTest method getNumberValueFromJsonObject_input_key_empty.

// 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_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 = "";
    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 17 with JsonString

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

the class ToolsTest method getValueFromJsonString_good_case.

// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_016: [The function shall get the string value from JsonString]
// Tests_SRS_SERVICE_SDK_JAVA_TOOLS_12_017: [The function shall trim the leading and trailing parenthesis from the string and return with it]
@Test
public void getValueFromJsonString_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";
    JsonString jsonStringObject = jsonObject.getJsonString(key);
    String expResult = "111111111111111111";
    // Act
    String result = Tools.getValueFromJsonString(jsonStringObject);
    // Assert
    assertEquals(expResult, result);
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) Test(org.junit.Test)

Example 18 with JsonString

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

the class ToolsTest method getNumberValueFromJsonObject_input_key_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_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;
    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 19 with JsonString

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

the class JsonRenderer method valueToString.

/**
     * Make a JSON text of an Object value. 
     * <p>
     * Warning: This method assumes that the data structure is acyclical.
     * @param value The value to be serialized.
     * @return a printable, displayable, transmittable
     *  representation of the object, beginning
     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
     *  with <code>}</code>&nbsp;<small>(right brace)</small>.
     * @throws JSONException If the value is or contains an invalid number.
     */
public String valueToString(Object value) {
    // TODO call the other valueToString instead
    if (value == null || value.equals(null)) {
        return "null";
    }
    if (value instanceof JsonString) {
        quote(((JsonString) value).getString());
    }
    if (value instanceof Number) {
        return numberToString((Number) value);
    }
    if (value instanceof Boolean) {
        return value.toString();
    }
    if (value instanceof JsonObject || value instanceof JsonArray) {
        StringWriter writer = new StringWriter();
        Json.createGenerator(writer).write((JsonValue) value).close();
        return writer.toString();
    }
    return quote(value.toString());
}
Also used : JsonArray(javax.json.JsonArray) StringWriter(java.io.StringWriter) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 20 with JsonString

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

the class OverrideStringParser method convertJsonValue.

/**
     * Convert single JSON-conformant value object
     * @param jsonValue JSON value
     * @return Object
     * @throws JSONException If JSON-parsing of value failed
     */
private static Object convertJsonValue(String jsonValue) {
    String jsonString = "{\"value\":" + jsonValue + "}";
    JsonObject json = toJson(jsonString);
    return convertJsonValue(json.get("value"));
}
Also used : JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Aggregations

JsonString (javax.json.JsonString)21 JsonObject (javax.json.JsonObject)18 StringReader (java.io.StringReader)14 JsonReader (javax.json.JsonReader)12 Test (org.junit.Test)10 JsonArray (javax.json.JsonArray)5 HashMap (java.util.HashMap)4 JsonException (javax.json.JsonException)4 JsonValue (javax.json.JsonValue)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ParseException (org.apache.sling.jcr.contentparser.ParseException)2 TableRef (com.torodb.core.TableRef)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FilenameFilter (java.io.FilenameFilter)1 StringWriter (java.io.StringWriter)1 Calendar (java.util.Calendar)1