use of javax.json.JsonArray in project javaee7-samples by javaee-samples.
the class JsonReaderFromReaderTest method testNestedStructure.
@Test
public void testNestedStructure() throws JSONException {
JsonReader jsonReader = Json.createReader(new StringReader("{" + " \"title\":\"The Matrix\"," + " \"year\":1999," + " \"cast\":[" + " \"Keanu Reaves\"," + " \"Laurence Fishburne\"," + " \"Carrie-Anne Moss\"" + " ]" + "}"));
JsonObject json = jsonReader.readObject();
assertNotNull(json);
assertFalse(json.isEmpty());
assertTrue(json.containsKey("title"));
assertEquals("The Matrix", json.getString("title"));
assertTrue(json.containsKey("year"));
assertEquals(1999, json.getInt("year"));
assertTrue(json.containsKey("cast"));
JsonArray jsonArr = json.getJsonArray("cast");
assertNotNull(jsonArr);
assertEquals(3, jsonArr.size());
JSONAssert.assertEquals("[" + " \"Keanu Reaves\"," + " \"Laurence Fishburne\"," + " \"Carrie-Anne Moss\"" + " ]", jsonArr.toString(), JSONCompareMode.STRICT);
}
use of javax.json.JsonArray in project javaee7-samples by javaee-samples.
the class JsonReaderFromStreamTest method testNestedStructure.
@Test
public void testNestedStructure() throws JSONException {
JsonReader jsonReader = Json.createReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("/4.json"));
JsonObject json = jsonReader.readObject();
assertNotNull(json);
assertFalse(json.isEmpty());
assertTrue(json.containsKey("title"));
assertEquals("The Matrix", json.getString("title"));
assertTrue(json.containsKey("year"));
assertEquals(1999, json.getInt("year"));
assertTrue(json.containsKey("cast"));
JsonArray jsonArr = json.getJsonArray("cast");
assertNotNull(jsonArr);
assertEquals(3, jsonArr.size());
JSONAssert.assertEquals("[" + " \"Keanu Reaves\"," + " \"Laurence Fishburne\"," + " \"Carrie-Anne Moss\"" + " ]", jsonArr.toString(), JSONCompareMode.STRICT);
}
use of javax.json.JsonArray in project jersey by jersey.
the class JsonProcessingTest method testJsonArrayAsStringPlus.
@Test
public void testJsonArrayAsStringPlus() throws Exception {
final Response response = target("jsonArray").request("application/foo+json").post(Entity.json(JSON_ARRAY_STR));
assertEquals(JSON_ARRAY, response.readEntity(JsonArray.class));
}
use of javax.json.JsonArray in project jersey by jersey.
the class JsonProcessingTest method testJsonArrayPlus.
@Test
public void testJsonArrayPlus() throws Exception {
final Response response = target("jsonArray").request("application/foo+json").post(Entity.json(JSON_ARRAY));
assertEquals(JSON_ARRAY, response.readEntity(JsonArray.class));
}
use of javax.json.JsonArray in project jersey by jersey.
the class HttpPatchTest method testPatch.
/**
* This test verifies that the patching of the resource state works.
* <p>
* The patch is created using the new standard JSON Processing API for Java and
* is then sent to the server. {@code PATCH} response as well as the new resource
* state obtained via subsequent {@code GET} method is verified against the expected
* state.
* </p>
*/
@Test
public void testPatch() {
final WebTarget target = target(App.ROOT_PATH);
// initial precondition check
final State expected = new State();
assertEquals(expected, target.request("application/json").get(State.class));
// apply first patch
expected.setMessage("patchedMessage");
expected.setTitle("patchedTitle");
expected.getList().add("one");
expected.getList().add("two");
JsonArray patch_1 = Json.createArrayBuilder().add(Json.createObjectBuilder().add("op", "replace").add("path", "/message").add("value", expected.getMessage()).build()).add(Json.createObjectBuilder().add("op", "replace").add("path", "/title").add("value", expected.getTitle()).build()).add(Json.createObjectBuilder().add("op", "replace").add("path", "/list").add("value", Json.createArrayBuilder().add(expected.getList().get(0)).add(expected.getList().get(1)).build()).build()).build();
assertEquals(expected, target.request("application/json").method("PATCH", Entity.entity(patch_1, PatchingInterceptor.PATCH_MEDIA_TYPE), State.class));
assertEquals(expected, target.request("application/json").get(State.class));
// apply second patch
expected.getList().add("three");
JsonArray patch_2 = Json.createArrayBuilder().add(Json.createObjectBuilder().add("op", "add").add("path", "/list/-").add("value", expected.getList().get(2)).build()).build();
assertEquals(expected, target.request("application/json").method("PATCH", Entity.entity(patch_2, PatchingInterceptor.PATCH_MEDIA_TYPE), State.class));
assertEquals(expected, target.request("application/json").get(State.class));
}
Aggregations