use of javax.json.JsonArray in project Payara by payara.
the class PayaraMicroDeployableContainer method processDeploymentAsJson.
private void processDeploymentAsJson(String deploymentInformation, HTTPContext httpContext) {
Matcher jsonMatcher = jsonPattern.matcher(deploymentInformation);
if (jsonMatcher.find()) {
// Convert the deployment information into a JsonArray
String jsonString = jsonMatcher.group("jsonArray");
try (JsonReader reader = Json.createReader(new StringReader(jsonString))) {
JsonArray array = reader.readArray();
// For each deployed application
for (JsonObject app : array.getValuesAs(JsonObject.class)) {
// Print the application details
printApplicationFound(app.getString("Name"));
// Store all application servlet mappings
Map<String, JsonObject> allMappings = new HashMap<>();
// If there's only one module, the mappings and context root are directly under the application
JsonObject appMappings = app.getJsonObject("Mappings");
if (appMappings != null) {
// Get the context root and store the relevant servlets
String contextRoot = app.getString("Context Root");
allMappings.put(contextRoot, app.getJsonObject("Mappings"));
} else {
JsonArray modules = app.getJsonArray("Modules");
modules.forEach(moduleValue -> {
// Get the context root and store the relevant servlets
JsonObject module = (JsonObject) moduleValue;
String contextRoot = module.getString("Context Root");
printModuleFound(module.getString("Name"), contextRoot);
allMappings.put(contextRoot, module);
});
}
// Handle each set of servlet mappings
for (String contextRoot : allMappings.keySet()) {
JsonObject mappings = allMappings.get(contextRoot);
mappings.values().forEach(name -> {
String servletName = name.toString().replaceAll("\"", "");
printServletFound(servletName);
httpContext.add(new Servlet(servletName, contextRoot));
});
}
}
}
}
}
use of javax.json.JsonArray 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.JsonArray in project Payara by payara.
the class JsonUtilTest method testArrayEncoding.
@Test
public void testArrayEncoding() throws JsonException {
Locale locale = null;
BaseModel model = CompositeUtil.instance().getModel(BaseModel.class);
model.setStringArray(new String[] { "one", "two" });
JsonObject json = (JsonObject) JsonUtil.getJsonValue(model);
Assert.assertNotNull(json);
Object o = json.get("stringArray");
Assert.assertTrue(o instanceof JsonArray);
JsonArray array = (JsonArray) o;
Assert.assertEquals(array.size(), 2);
Assert.assertTrue(contains(array, "one"));
Assert.assertTrue(contains(array, "two"));
BaseModel model2 = CompositeUtil.instance().unmarshallClass(locale, BaseModel.class, json);
Assert.assertNotNull(model2);
Assert.assertNotNull(model2.getStringArray());
Assert.assertEquals(2, model2.getStringArray().length);
}
use of javax.json.JsonArray in project Payara by payara.
the class Common method toValue.
private static Value toValue(Object j) throws Exception {
if (j instanceof String) {
return stringVal().value((String) j);
}
if (j instanceof Long) {
return longVal().value((Long) j);
}
if (j instanceof Integer) {
return intVal().value((Integer) j);
}
if (j instanceof Double) {
return doubleVal().value((Double) j);
}
if (j instanceof Boolean) {
return booleanVal().value((Boolean) j);
}
if (JsonObject.NULL == j) {
return nilVal();
}
if (j instanceof JsonObject) {
JsonObject jo = (JsonObject) j;
ObjectValue ov = objectVal();
for (String key : jo.keySet()) {
ov.put(key, toValue(jo.get(key)));
}
return ov;
}
if (j instanceof JsonArray) {
JsonArray ja = (JsonArray) j;
ArrayValue av = arrayVal();
for (int i = 0; i < ja.size(); i++) {
av.add(toValue(ja.get(i)));
}
return av;
}
throw new IllegalArgumentException("Cannot convert " + j + " to a Value");
}
use of javax.json.JsonArray in project javaee7-samples by javaee-samples.
the class DOMGeneratorTest method testArray.
@Test
public void testArray() throws JSONException {
JsonArray jsonArray = Json.createArrayBuilder().add(Json.createObjectBuilder().add("apple", "red")).add(Json.createObjectBuilder().add("banana", "yellow")).build();
StringWriter w = new StringWriter();
try (JsonWriter writer = Json.createWriter(w)) {
writer.write(jsonArray);
}
JSONAssert.assertEquals("[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]", w.toString(), JSONCompareMode.STRICT);
}
Aggregations