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 wildfly by wildfly.
the class JobExecutionMarshaller method unmarshall.
public static JobExecution unmarshall(final String json) {
final JsonParser parser = Json.createParser(new StringReader(json));
final JobExecutionBuilder builder = JobExecutionBuilder.create();
String key = null;
while (parser.hasNext()) {
final Event event = parser.next();
switch(event) {
case KEY_NAME:
key = parser.getString();
break;
case VALUE_FALSE:
case VALUE_NULL:
case VALUE_NUMBER:
case VALUE_STRING:
case VALUE_TRUE:
final String value = parser.getString();
if (key == null) {
throw new IllegalStateException(String.format("No key for value '%s'. Parsing position: %s%n\t%s", value, parser.getLocation(), json));
}
switch(key) {
case ID:
if (value != null) {
builder.setId(Long.parseLong(value));
}
break;
case NAME:
builder.setName(value);
break;
case STATUS:
if (value != null) {
builder.setStatus(BatchStatus.valueOf(value));
}
break;
case EXIT_STATUS:
builder.setExitStatus(value);
break;
case CREATE_TIME:
if (value != null) {
builder.setCreateTime(Long.parseLong(value));
}
break;
case END_TIME:
if (value != null) {
builder.setEndTime(Long.parseLong(value));
}
break;
case LAST_UPDATE_TIME:
if (value != null) {
builder.setLastUpdatedTime(Long.parseLong(value));
}
break;
case START_TIME:
if (value != null) {
builder.setStartTime(Long.parseLong(value));
}
break;
case PROPERTIES:
String k = null;
while (parser.hasNext()) {
final Event e = parser.next();
switch(e) {
case KEY_NAME:
k = parser.getString();
break;
case VALUE_FALSE:
case VALUE_NULL:
case VALUE_NUMBER:
case VALUE_STRING:
case VALUE_TRUE:
if (k != null) {
builder.addParameter(k, parser.getString());
}
break;
}
}
break;
}
break;
}
}
parser.close();
return builder.build();
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class RestModelListReader method readFrom.
@Override
public List<RestModel> readFrom(Class<List<RestModel>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
Locale locale = CompositeUtil.instance().getLocale(httpHeaders);
List<RestModel> list = new ArrayList<RestModel>();
BufferedReader in = new BufferedReader(new InputStreamReader(entityStream));
JsonParser parser = Json.createParser(in);
JsonArray array;
if (parser.hasNext()) {
parser.next();
array = parser.getArray();
} else {
array = JsonValue.EMPTY_JSON_ARRAY;
}
Class<?> modelType = null;
if (genericType instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) genericType;
modelType = (Class) pt.getActualTypeArguments()[0];
}
for (int i = 0; i < array.size(); i++) {
JsonObject o = array.getJsonObject(i);
RestModel model = (RestModel) CompositeUtil.instance().unmarshallClass(locale, modelType, o);
Set<ConstraintViolation<RestModel>> cv = CompositeUtil.instance().validateRestModel(locale, model);
if (!cv.isEmpty()) {
final Response response = Response.status(Status.BAD_REQUEST).entity(CompositeUtil.instance().getValidationFailureMessages(locale, cv, model)).build();
throw new WebApplicationException(response);
}
list.add(model);
}
return list;
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
use of javax.json.stream.JsonParser in project Payara by payara.
the class CompositeUtilTest method readInJson.
@Test
public void readInJson() throws Exception {
Locale locale = null;
JsonParser parser = Json.createParser(new StringReader(JSON));
parser.next();
JsonObject o = parser.getObject();
BaseModel model = CompositeUtil.instance().unmarshallClass(locale, BaseModel.class, o);
Assert.assertEquals(model.getName(), "testModel");
Assert.assertEquals(model.getRelated().size(), 2);
Assert.assertTrue(model.getRelated().get(0).getDescription().startsWith("description "));
}
Aggregations