Search in sources :

Example 1 with JsonNumber

use of javax.json.JsonNumber in project jersey by jersey.

the class JsonProcessingResourceTest method testStoreDocuments.

@Test
public void testStoreDocuments() throws Exception {
    final Response response = target("document/multiple").request(MediaType.APPLICATION_JSON).post(Entity.json(getDocumentJsonArray()));
    assertEquals(200, response.getStatus());
    final List<JsonNumber> ids = response.readEntity(JsonArray.class).getValuesAs(JsonNumber.class);
    assertEquals(documents.size(), ids.size());
    // Remove All.
    target("document").request().delete();
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(javax.json.JsonArray) JsonNumber(javax.json.JsonNumber) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 2 with JsonNumber

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

the class Tools method getNumberValueFromJsonObject.

/**
     * Helper function to get numeric value from a JsonObject
     *
     * @param jsonObject The JsonObject object to get the value from
     * @param key The name of the key
     * @return The numeric value
     */
public static long getNumberValueFromJsonObject(JsonObject jsonObject, String key) {
    long retVal;
    JsonNumber jsonNumber = null;
    // Codes_SRS_SERVICE_SDK_JAVA_TOOLS_12_018: [The function shall return zero if any of the input is null]
    if ((jsonObject == null) || (jsonObject == JsonObject.NULL) || (key == null) || (key.length() == 0)) {
        retVal = 0;
    } else {
        // Codes_SRS_SERVICE_SDK_JAVA_TOOLS_12_019: [The function shall get the JsonValue of the key and return zero if it is null]
        JsonValue jsonValue = jsonObject.get(key);
        if (jsonValue != JsonValue.NULL) {
            // Codes_SRS_SERVICE_SDK_JAVA_TOOLS_12_020: [The function shall get the JsonNumber from the JsonValue and return zero if it is null]
            jsonNumber = jsonObject.getJsonNumber(key);
            if (jsonNumber != null) {
                // Codes_SRS_SERVICE_SDK_JAVA_TOOLS_12_021: [The function shall return the long value from the JsonNumber if the JsonNumber is not null]
                retVal = jsonNumber.longValue();
            } else {
                retVal = 0;
            }
        } else {
            retVal = 0;
        }
    }
    return retVal;
}
Also used : JsonNumber(javax.json.JsonNumber) JsonValue(javax.json.JsonValue)

Example 3 with JsonNumber

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

the class JsonContentParser method convertValue.

private Object convertValue(JsonValue value) {
    switch(value.getValueType()) {
        case STRING:
            String stringValue = ((JsonString) value).getString();
            Calendar calendarValue = helper.tryParseCalendar(stringValue);
            if (calendarValue != null) {
                return calendarValue;
            } else {
                return stringValue;
            }
        case NUMBER:
            JsonNumber numberValue = (JsonNumber) value;
            if (numberValue.isIntegral()) {
                return numberValue.longValue();
            } else {
                return numberValue.bigDecimalValue();
            }
        case TRUE:
            return true;
        case FALSE:
            return false;
        case NULL:
            return null;
        case ARRAY:
            JsonArray arrayValue = (JsonArray) value;
            Object[] values = new Object[arrayValue.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = convertValue(arrayValue.get(i));
            }
            return helper.convertSingleTypeArray(values);
        case OBJECT:
            return (JsonObject) value;
        default:
            throw new ParseException("Unexpected JSON value type: " + value.getValueType());
    }
}
Also used : JsonArray(javax.json.JsonArray) Calendar(java.util.Calendar) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) ParseException(org.apache.sling.jcr.contentparser.ParseException)

Example 4 with JsonNumber

use of javax.json.JsonNumber in project jersey by jersey.

the class JsonProcessingResourceTest method testStoreGetRemoveDocument.

@Test
public void testStoreGetRemoveDocument() throws Exception {
    final JsonObject document = documents.get(0);
    // Store.
    final Response response = target("document").request(MediaType.APPLICATION_JSON).post(Entity.json(document));
    assertEquals(200, response.getStatus());
    final List<JsonNumber> ids = response.readEntity(JsonArray.class).getValuesAs(JsonNumber.class);
    assertEquals(1, ids.size());
    // Get.
    final String id = ids.get(0).toString();
    final WebTarget documentTarget = target("document").path(id);
    final JsonObject storedDocument = documentTarget.request(MediaType.APPLICATION_JSON).get(JsonObject.class);
    assertEquals(document, storedDocument);
    // Remove.
    final JsonObject removedDocument = documentTarget.request(MediaType.APPLICATION_JSON).delete(JsonObject.class);
    assertEquals(document, removedDocument);
    // Get.
    final Response errorResponse = documentTarget.request(MediaType.APPLICATION_JSON).get();
    assertEquals(204, errorResponse.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(javax.json.JsonArray) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

JsonNumber (javax.json.JsonNumber)4 JsonArray (javax.json.JsonArray)3 JsonObject (javax.json.JsonObject)2 Response (javax.ws.rs.core.Response)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 Test (org.junit.Test)2 Calendar (java.util.Calendar)1 JsonString (javax.json.JsonString)1 JsonValue (javax.json.JsonValue)1 WebTarget (javax.ws.rs.client.WebTarget)1 ParseException (org.apache.sling.jcr.contentparser.ParseException)1