Search in sources :

Example 1 with JsonParser

use of javax.json.stream.JsonParser in project quickstart by wildfly.

the class JsonController method parseJsonStream.

public void parseJsonStream() {
    StringBuilder sb = new StringBuilder();
    String json = getJsonString();
    try {
        JsonParser parser = Json.createParser(new StringReader(json));
        while (parser.hasNext()) {
            Event event = parser.next();
            if (event.equals(Event.KEY_NAME)) {
                sb.append(" - - - -  >  Key: " + parser.getString() + "  < - - - - - \n");
            }
            if (event.equals(Event.VALUE_STRING)) {
                sb.append("Value as String: " + parser.getString() + "\n");
            }
            if (event.equals(Event.VALUE_NUMBER)) {
                sb.append("Value as Number: " + parser.getInt() + "\n");
            }
            if (event.equals(Event.VALUE_TRUE)) {
                sb.append("Value as Boolean: true\n");
            }
            if (event.equals(Event.VALUE_FALSE)) {
                sb.append("Value as Boolean: false \n");
            }
        }
    } catch (JsonException e) {
        FacesContext.getCurrentInstance().addMessage("form:parsed", new FacesMessage(e.getMessage()));
    }
    parsedResult = sb.toString();
}
Also used : JsonException(javax.json.JsonException) StringReader(java.io.StringReader) Event(javax.json.stream.JsonParser.Event) FacesMessage(javax.faces.application.FacesMessage) JsonParser(javax.json.stream.JsonParser)

Example 2 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromReaderTest method testSimpleObject.

@Test
public void testSimpleObject() throws JSONException {
    String json = "{" + "  \"apple\":\"red\"," + "  \"banana\":\"yellow\"" + "}";
    JsonParser parser = Json.createParser(new StringReader(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 : StringReader(java.io.StringReader) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 3 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromReaderTest method testEmptyObject.

@Test
public void testEmptyObject() throws JSONException {
    String json = "{}";
    JsonParser parser = Json.createParser(new StringReader(json));
    assertEquals(JsonParser.Event.START_OBJECT, parser.next());
    assertEquals(JsonParser.Event.END_OBJECT, parser.next());
}
Also used : StringReader(java.io.StringReader) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 4 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class JsonParserFromReaderTest method testNestedStructure.

@Test
public void testNestedStructure() throws JSONException {
    String json = "{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}";
    JsonParser parser = Json.createParser(new StringReader(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 : StringReader(java.io.StringReader) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 5 with JsonParser

use of javax.json.stream.JsonParser in project javaee7-samples by javaee-samples.

the class MyReader method readFrom.

@Override
public MyObject readFrom(Class<MyObject> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream in) throws IOException, WebApplicationException {
    MyObject mo = new MyObject();
    JsonParser parser = Json.createParser(in);
    while (parser.hasNext()) {
        switch(parser.next()) {
            case KEY_NAME:
                String key = parser.getString();
                parser.next();
                switch(key) {
                    case "name":
                        mo.setName(parser.getString());
                        break;
                    case "age":
                        mo.setAge(parser.getInt());
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }
    return mo;
}
Also used : JsonParser(javax.json.stream.JsonParser)

Aggregations

JsonParser (javax.json.stream.JsonParser)30 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 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 JOSEException (com.nimbusds.jose.JOSEException)1