use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.
the class JsonParserFromReaderTest method testArray.
@Test
public void testArray() throws JSONException {
String json = "[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]";
JsonParser parser = Json.createParser(new StringReader(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-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;
}
Aggregations