Search in sources :

Example 16 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)

Example 17 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)

Example 18 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 19 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 20 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)

Aggregations

JsonParser (javax.json.stream.JsonParser)24 StringReader (java.io.StringReader)12 JsonObject (javax.json.JsonObject)10 Test (org.junit.Test)9 JsonException (javax.json.JsonException)6 Locale (java.util.Locale)4 Event (javax.json.stream.JsonParser.Event)4 IOException (java.io.IOException)3 JsonArray (javax.json.JsonArray)3 JsonString (javax.json.JsonString)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 JsonValue (javax.json.JsonValue)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 CachedCommandModel (com.sun.enterprise.admin.util.CachedCommandModel)1