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