use of javax.json.stream.JsonParser.Event in project jvm-serializers by eishay.
the class JavaxJsonStream method readImages.
private List<Image> readImages(JsonParser parser) throws IOException {
Event current;
if ((current = parser.next()) != Event.START_ARRAY) {
reportIllegal(parser, current, Event.START_ARRAY);
}
List<Image> images = new ArrayList<Image>();
while ((current = parser.next()) == Event.START_OBJECT) {
images.add(readImage(parser));
}
verifyCurrent(parser, current, Event.END_ARRAY);
return images;
}
use of javax.json.stream.JsonParser.Event in project jvm-serializers by eishay.
the class JavaxJsonStream method readMedia.
private Media readMedia(JsonParser parser) throws IOException {
Event current;
if ((current = parser.next()) != Event.START_OBJECT) {
reportIllegal(parser, current, Event.START_OBJECT);
}
Media media = new Media();
boolean haveWidth = false;
boolean haveHeight = false;
boolean haveDuration = false;
boolean haveSize = false;
while ((current = parser.next()) == Event.KEY_NAME) {
String field = parser.getString();
Integer I = fullFieldToIndex.get(field);
if (I != null) {
switch(I) {
case FIELD_IX_PLAYER:
current = parser.next();
media.player = Media.Player.find(parser.getString());
continue;
case FIELD_IX_URI:
current = parser.next();
media.uri = parser.getString();
continue;
case FIELD_IX_TITLE:
current = parser.next();
media.title = parser.getString();
continue;
case FIELD_IX_WIDTH:
current = parser.next();
media.width = parser.getInt();
haveWidth = true;
continue;
case FIELD_IX_HEIGHT:
current = parser.next();
media.height = parser.getInt();
haveHeight = true;
continue;
case FIELD_IX_FORMAT:
current = parser.next();
media.format = parser.getString();
continue;
case FIELD_IX_DURATION:
current = parser.next();
media.duration = parser.getInt();
haveDuration = true;
continue;
case FIELD_IX_SIZE:
current = parser.next();
media.size = parser.getInt();
haveSize = true;
continue;
case FIELD_IX_BITRATE:
current = parser.next();
media.bitrate = parser.getInt();
media.hasBitrate = true;
continue;
case FIELD_IX_PERSONS:
media.persons = readPersons(parser);
continue;
case FIELD_IX_COPYRIGHT:
current = parser.next();
media.copyright = parser.getString();
continue;
}
}
throw new IllegalStateException("Unexpected field '" + field + "'");
}
verifyCurrent(parser, current, Event.END_OBJECT);
if (media.uri == null)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_URI);
if (!haveWidth)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_WIDTH);
if (!haveHeight)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_HEIGHT);
if (media.format == null)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_FORMAT);
if (!haveDuration)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_DURATION);
if (!haveSize)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_SIZE);
if (media.persons == null)
media.persons = new ArrayList<String>();
if (media.player == null)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_PLAYER);
return media;
}
use of javax.json.stream.JsonParser.Event in project wildfly by wildfly.
the class StepExecutionMarshaller method unmarshall.
public static StepExecution unmarshall(final String json) throws IOException, ClassNotFoundException {
final JsonParser parser = Json.createParser(new StringReader(json));
final StepExecutionBuilder builder = StepExecutionBuilder.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 END_TIME:
if (value != null) {
builder.setEndTime(Long.parseLong(value));
}
break;
case START_TIME:
if (value != null) {
builder.setStartTime(Long.parseLong(value));
}
break;
case PERSISTENT_USER_DATA:
builder.setPersistentUserData(deserialize(value));
case METRICS:
String k = null;
String metricType = null;
String metricValue = 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) {
throw new IllegalStateException(String.format("No key for value '%s'. Parsing position: %s%n\t%s", value, parser.getLocation(), json));
}
switch(k) {
case METRIC_TYPE:
metricType = parser.getString();
break;
case METRIC_VALUE:
metricValue = parser.getString();
break;
}
if (metricType != null && metricValue != null) {
final MetricType type = MetricType.valueOf(metricType);
final long v = Long.parseLong(parser.getString());
final Metric m = new Metric() {
@Override
public MetricType getType() {
return type;
}
@Override
public long getValue() {
return v;
}
};
builder.addMetric(m);
}
break;
}
}
break;
}
break;
}
}
parser.close();
return builder.build();
}
use of javax.json.stream.JsonParser.Event in project wildfly by wildfly.
the class JsonTestCase method testJsonServlet.
@Test
public void testJsonServlet() throws Exception {
final String result = HttpRequest.get(url + "json", 10, TimeUnit.SECONDS);
final JsonParser parser = Json.createParser(new StringReader(result));
String key = null;
String value = null;
while (parser.hasNext()) {
final Event event = parser.next();
switch(event) {
case KEY_NAME:
key = parser.getString();
break;
case VALUE_STRING:
value = parser.getString();
break;
}
}
parser.close();
Assert.assertEquals("Key should be \"name\"", "name", key);
Assert.assertEquals("Value should be \"value\"", "value", value);
}
use of javax.json.stream.JsonParser.Event in project jvm-serializers by eishay.
the class JavaxJsonStream method readMediaContent.
//////////////////////////////////////////////////
// Deserialization
//////////////////////////////////////////////////
protected MediaContent readMediaContent(JsonParser parser) throws IOException {
MediaContent mc = new MediaContent();
Event current;
if ((current = parser.next()) != Event.START_OBJECT) {
reportIllegal(parser, current, Event.START_OBJECT);
}
while ((current = parser.next()) == Event.KEY_NAME) {
String field = parser.getString();
Integer I = fullFieldToIndex.get(field);
if (I != null) {
switch(I) {
case FIELD_IX_MEDIA:
mc.media = readMedia(parser);
continue;
case FIELD_IX_IMAGES:
mc.images = readImages(parser);
continue;
}
}
throw new IllegalStateException("Unexpected field '" + field + "'");
}
verifyCurrent(parser, current, Event.END_OBJECT);
if (mc.media == null)
throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_MEDIA);
if (mc.images == null)
mc.images = new ArrayList<Image>();
return mc;
}
Aggregations