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;
}
}
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());
}
}
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;
}
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"));
}
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();
}
Aggregations