Search in sources :

Example 6 with Event

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;
}
Also used : Event(javax.json.stream.JsonParser.Event)

Example 7 with Event

use of javax.json.stream.JsonParser.Event in project jvm-serializers by eishay.

the class JavaxJsonStream method readPersons.

private List<String> readPersons(JsonParser parser) throws IOException {
    Event current;
    if ((current = parser.next()) != Event.START_ARRAY) {
        reportIllegal(parser, current, Event.START_ARRAY);
    }
    List<String> persons = new ArrayList<String>();
    while ((current = parser.next()) == Event.VALUE_STRING) {
        persons.add(parser.getString());
    }
    verifyCurrent(parser, current, Event.END_ARRAY);
    return persons;
}
Also used : Event(javax.json.stream.JsonParser.Event)

Example 8 with Event

use of javax.json.stream.JsonParser.Event in project jvm-serializers by eishay.

the class JavaxJsonStream method readImage.

private Image readImage(JsonParser parser) throws IOException {
    Event current;
    boolean haveWidth = false;
    boolean haveHeight = false;
    Image image = new Image();
    while ((current = parser.next()) == Event.KEY_NAME) {
        String field = parser.getString();
        // read value token (or START_ARRAY)
        current = parser.next();
        Integer I = fullFieldToIndex.get(field);
        if (I != null) {
            switch(I) {
                case FIELD_IX_URI:
                    image.uri = parser.getString();
                    continue;
                case FIELD_IX_TITLE:
                    image.title = parser.getString();
                    continue;
                case FIELD_IX_WIDTH:
                    image.width = parser.getInt();
                    haveWidth = true;
                    continue;
                case FIELD_IX_HEIGHT:
                    image.height = parser.getInt();
                    haveHeight = true;
                    continue;
                case FIELD_IX_SIZE:
                    image.size = Image.Size.valueOf(parser.getString());
                    continue;
            }
        }
        throw new IllegalStateException("Unexpected field '" + field + "'");
    }
    if (image.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 (image.size == null)
        throw new IllegalStateException("Missing field: " + FULL_FIELD_NAME_SIZE);
    verifyCurrent(parser, current, Event.END_OBJECT);
    return image;
}
Also used : Event(javax.json.stream.JsonParser.Event)

Example 9 with Event

use of javax.json.stream.JsonParser.Event 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();
}
Also used : StringReader(java.io.StringReader) Event(javax.json.stream.JsonParser.Event) JsonParser(javax.json.stream.JsonParser)

Aggregations

Event (javax.json.stream.JsonParser.Event)9 StringReader (java.io.StringReader)4 JsonParser (javax.json.stream.JsonParser)4 Metric (javax.batch.runtime.Metric)1 MetricType (javax.batch.runtime.Metric.MetricType)1 FacesMessage (javax.faces.application.FacesMessage)1 JsonException (javax.json.JsonException)1 Test (org.junit.Test)1