Search in sources :

Example 21 with JsonParser

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());
}
Also used : StringReader(java.io.StringReader) JsonParser(javax.json.stream.JsonParser) Test(org.junit.Test)

Example 22 with JsonParser

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

Example 23 with JsonParser

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);
    }
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) RestModel(org.glassfish.admin.rest.composite.RestModel) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) JsonArray(javax.json.JsonArray) ParameterizedType(java.lang.reflect.ParameterizedType) Response(javax.ws.rs.core.Response) ConstraintViolation(javax.validation.ConstraintViolation) BufferedReader(java.io.BufferedReader) JsonParser(javax.json.stream.JsonParser)

Example 24 with JsonParser

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 "));
}
Also used : Locale(java.util.Locale) BaseModel(org.glassfish.admin.rest.model.BaseModel) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonParser(javax.json.stream.JsonParser) Test(org.testng.annotations.Test)

Aggregations

JsonParser (javax.json.stream.JsonParser)24 StringReader (java.io.StringReader)12 JsonObject (javax.json.JsonObject)10 Test (org.junit.Test)9 JsonException (javax.json.JsonException)6 Locale (java.util.Locale)4 Event (javax.json.stream.JsonParser.Event)4 IOException (java.io.IOException)3 JsonArray (javax.json.JsonArray)3 JsonString (javax.json.JsonString)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 JsonValue (javax.json.JsonValue)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 CachedCommandModel (com.sun.enterprise.admin.util.CachedCommandModel)1