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();
}
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());
}
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());
}
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());
}
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;
}
Aggregations