Search in sources :

Example 61 with JsonReader

use of javax.json.JsonReader 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 62 with JsonReader

use of javax.json.JsonReader 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 63 with JsonReader

use of javax.json.JsonReader 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 64 with JsonReader

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

the class TeleporterHttpClient method verifyCorrectBundleState.

void verifyCorrectBundleState(String bundleSymbolicName, int timeoutInSeconds) throws IOException {
    final String url = baseUrl + "/system/console/bundles/" + bundleSymbolicName + ".json";
    final long end = System.currentTimeMillis() + timeoutInSeconds * 1000;
    final ExponentialBackoffDelay d = new ExponentialBackoffDelay(50, 250);
    while (System.currentTimeMillis() < end) {
        String jsonBody = waitForStatus(url, 200, timeoutInSeconds * 1000);
        // deserialize json (https://issues.apache.org/jira/browse/SLING-6536)
        try (JsonReader jsonReader = Json.createReader(new StringReader(jsonBody))) {
            // extract state
            JsonArray jsonArray = jsonReader.readObject().getJsonArray("data");
            if (jsonArray == null) {
                throw new JsonException("Could not find 'data' array");
            }
            JsonObject bundleObject = jsonArray.getJsonObject(0);
            String state = bundleObject.getString("state");
            if ("Active".equals(state)) {
                return;
            }
            // otherwise evaluate the import section
            JsonArray propsArray = bundleObject.getJsonArray("props");
            if (propsArray == null) {
                throw new JsonException("Could not find 'props' object");
            }
            // iterate through all of them until key="Imported Packages" is found
            for (JsonValue propValue : propsArray) {
                if (propValue.getValueType().equals(ValueType.OBJECT)) {
                    JsonObject propObject = (JsonObject) propValue;
                    if ("Imported Packages".equals(propObject.getString("key"))) {
                        JsonArray importedPackagesArray = propObject.getJsonArray("value");
                        String reason = null;
                        for (JsonValue importedPackageValue : importedPackagesArray) {
                            if (importedPackageValue.getValueType().equals(ValueType.STRING)) {
                                String importedPackage = ((JsonString) importedPackageValue).getString();
                                if (importedPackage.startsWith("ERROR:")) {
                                    reason = importedPackage;
                                }
                            }
                        }
                        // only if ERROR is found there is no more need to wait for the bundle to become active, otherwise it might just be started in the background
                        if (reason != null) {
                            throw new IllegalStateException("The test bundle '" + bundleSymbolicName + "' is in state '" + state + "'. This is due to unresolved import-packages: " + reason);
                        }
                    }
                }
            }
        } catch (JsonException | IndexOutOfBoundsException e) {
            throw new IllegalArgumentException("Test bundle '" + bundleSymbolicName + "' not correctly installed. Could not parse JSON response though to expose further information: " + jsonBody, e);
        }
        d.waitNextDelay();
    }
    throw new IOException("Bundle '" + bundleSymbolicName + "' was not started after " + timeoutInSeconds + " seconds. The check at " + url + " was not successfull. Probably some dependent bundle was not started.");
}
Also used : JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) IOException(java.io.IOException) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonString(javax.json.JsonString)

Example 65 with JsonReader

use of javax.json.JsonReader in project quickstart by wildfly.

the class MessageDecoder method decode.

// create a Message object from JSON
@Override
public Message decode(String msg) throws DecodeException {
    logger.info("Decoding: " + msg);
    // It uses the JSON-P API to parse JSON content
    JsonReader reader = Json.createReader(new StringReader(msg));
    JsonObject jsonObject = reader.readObject();
    String command = jsonObject.getString("command");
    Integer bidValue = null;
    if (jsonObject.containsKey("bidValue")) {
        bidValue = jsonObject.getInt("bidValue");
    }
    return new Message(command, bidValue);
}
Also used : StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

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