Search in sources :

Example 71 with JsonArray

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);
}
Also used : JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 72 with JsonArray

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);
}
Also used : JsonArray(javax.json.JsonArray) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 73 with JsonArray

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));
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(javax.json.JsonArray) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 74 with JsonArray

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));
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(javax.json.JsonArray) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 75 with JsonArray

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));
}
Also used : JsonArray(javax.json.JsonArray) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

JsonArray (javax.json.JsonArray)128 JsonObject (javax.json.JsonObject)97 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)32 JsonReader (javax.json.JsonReader)31 HashMap (java.util.HashMap)29 JsonString (javax.json.JsonString)28 HashSet (java.util.HashSet)21 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 StringReader (java.io.StringReader)18 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 LinkedHashMap (java.util.LinkedHashMap)14 JsonValue (javax.json.JsonValue)11 Map (java.util.Map)10 JsonException (javax.json.JsonException)9 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7