Search in sources :

Example 26 with JsonParser

use of javax.json.stream.JsonParser in project tutorials by eugenp.

the class JsonUnitTest method whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned.

@Test
public void whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned() throws IOException, ParseException {
    JsonParser jsonParser = Json.createParser(new StringReader(petshopJson));
    int count = 0;
    String result = null;
    while (jsonParser.hasNext()) {
        Event e = jsonParser.next();
        if (e == Event.KEY_NAME) {
            if (jsonParser.getString().equals("name")) {
                jsonParser.next();
                if (++count == 3) {
                    result = jsonParser.getString();
                    break;
                }
            }
        }
    }
    assertEquals("The query should return the 'name' property of the third pet from the list", "Jake", result);
}
Also used : StringReader(java.io.StringReader) Event(javax.json.stream.JsonParser.Event) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 27 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromStreamTest method testSimpleObject.

@Test
public void testSimpleObject() throws JSONException {
    JsonParser parser = Json.createParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("/2.json"));
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
}
Also used : JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 28 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromStreamTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    JsonParser parser = Json.createParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("/4.json"));
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_NUMBER, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.START_ARRAY, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.END_ARRAY, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
}
Also used : JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 29 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromStreamTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    JsonParser parser = Json.createParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("/1.json"));
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
}
Also used : JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 30 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromStreamTest method testArray.

@Test
public void testArray() throws JSONException {
    JsonParser parser = Json.createParser(Thread.currentThread().getContextClassLoader().getResourceAsStream("/3.json"));
    assertEquals(JsonParser.Event.START_ARRAY, parser.next());
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.KEY_NAME, parser.next());
    assertEquals(JsonParser.Event.VALUE_STRING, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
    assertEquals(JsonParser.Event.END_ARRAY, parser.next());
}
Also used : JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Aggregations

JsonParser (javax.json.stream.JsonParser)32 StringReader (java.io.StringReader)14 JsonObject (javax.json.JsonObject)10 Test (org.junit.Test)10 Event (javax.json.stream.JsonParser.Event)8 JsonException (javax.json.JsonException)6 Locale (java.util.Locale)4 JsonString (javax.json.JsonString)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 JsonArray (javax.json.JsonArray)3 JsonValue (javax.json.JsonValue)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 JsonParserFactory (javax.json.stream.JsonParserFactory)2 ConstraintViolation (javax.validation.ConstraintViolation)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 BaseModel (org.glassfish.admin.rest.model.BaseModel)2 Test (org.testng.annotations.Test)2