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