use of javax.json.stream.JsonParser in project javaee7-firstcup by ecabrerar.
the class MovieReader method readFrom.
@Override
public Movie readFrom(Class<Movie> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
Movie movie = new Movie();
JsonParser parser = Json.createParser(in);
while (parser.hasNext()) {
switch(parser.next()) {
case KEY_NAME:
String key = parser.getString();
parser.next();
switch(key) {
case "id":
movie.setId(parser.getInt());
break;
case "name":
movie.setName(parser.getString());
break;
case "actors":
movie.setActors(parser.getString());
break;
default:
break;
}
break;
default:
break;
}
}
return movie;
}
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());
}
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 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 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());
}
Aggregations