Search in sources :

Example 11 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class JsonParameterMapProvider method readFrom.

@Override
public ParameterMap readFrom(Class<ParameterMap> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream in) throws IOException {
    JsonObject obj;
    try {
        JsonParser parser = Json.createParser(in);
        if (parser.hasNext()) {
            parser.next();
            obj = parser.getObject();
        } else {
            obj = JsonValue.EMPTY_JSON_OBJECT;
        }
        ParameterMap map = new ParameterMap();
        for (String k : obj.keySet()) {
            Object value = obj.get(k);
            if (value instanceof JsonArray) {
                JsonArray array = (JsonArray) value;
                for (int i = 0; i < array.size(); i++) {
                    map.add(k, "" + array.get(i));
                }
            } else {
                map.add(k, "" + value);
            }
        }
        return map;
    } catch (Exception ex) {
        Logger.getLogger("org.glassfish.admin.rest").log(Level.SEVERE, null, ex);
        ParameterMap map = new ParameterMap();
        map.add("error", "Entity Parsing Error: " + ex.getMessage());
        return map;
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) ParameterMap(org.glassfish.api.admin.ParameterMap) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) JsonParser(javax.json.stream.JsonParser)

Example 12 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class RestModelReader method readFrom.

@Override
public T readFrom(Class<T> type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String, String> mm, InputStream entityStream) throws WebApplicationException, IOException {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(entityStream));
        JsonParser parser = Json.createParser(in);
        final Locale locale = CompositeUtil.instance().getLocale(mm);
        JsonObject o;
        if (parser.hasNext()) {
            parser.next();
            o = parser.getObject();
        } else {
            o = JsonValue.EMPTY_JSON_OBJECT;
        }
        T model = CompositeUtil.instance().unmarshallClass(locale, type, o);
        Set<ConstraintViolation<T>> 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);
        }
        return (T) model;
    } catch (JsonException ex) {
        throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(ex.getLocalizedMessage()).build());
    }
}
Also used : Locale(java.util.Locale) Response(javax.ws.rs.core.Response) JsonException(javax.json.JsonException) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) ConstraintViolation(javax.validation.ConstraintViolation) BufferedReader(java.io.BufferedReader) JsonObject(javax.json.JsonObject) JsonParser(javax.json.stream.JsonParser)

Example 13 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class Util method processJsonMap.

/**
 * Converts a String of JSON data into a map
 * If there is an error then an empty Map will be returned.
 * @param json
 * @return A {@link Map} of {@link String} || {@link Number}
 */
public static Map processJsonMap(String json) {
    Map<String, Object> map;
    try {
        JsonParser parser = Json.createParser(new StringReader(json));
        if (parser.hasNext()) {
            parser.next();
            map = processJsonObject(parser.getObject());
        } else {
            map = new HashMap<String, Object>();
        }
    } catch (JsonException e) {
        map = new HashMap();
    }
    return map;
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonParser(javax.json.stream.JsonParser)

Example 14 with JsonParser

use of javax.json.stream.JsonParser in project Payara by payara.

the class CompositeUtilTest method testDirtyFieldDetection.

@Test
public void testDirtyFieldDetection() throws JsonException {
    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);
    RestModel rmi = (RestModel) model;
    Assert.assertTrue(rmi.isSet("name"));
    Assert.assertTrue(rmi.isSet("count"));
    Assert.assertTrue(rmi.isSet("related"));
    Assert.assertFalse(rmi.isSet("size"));
}
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)

Example 15 with JsonParser

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

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