Search in sources :

Example 31 with JsonParser

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

Example 32 with JsonParser

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;
}
Also used : Movie(org.javaee7.movieplex7.entities.Movie) JsonParser(javax.json.stream.JsonParser)

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