use of javax.json.JsonObject in project sling by apache.
the class JsonRenderer method prettyPrint.
/**
* Make a prettyprinted JSON text of this JSONObject.
* <p>
* Warning: This method assumes that the data structure is acyclical.
* @return a printable, displayable, transmittable
* representation of the object, beginning
* with <code>{</code> <small>(left brace)</small> and ending
* with <code>}</code> <small>(right brace)</small>.
* @throws IllegalArgumentException If the object contains an invalid number.
*/
public String prettyPrint(JsonObject jo, Options opt) {
int n = jo.size();
if (n == 0) {
return "{}";
}
final JsonArrayBuilder children = Json.createArrayBuilder();
Iterator<String> keys = jo.keySet().iterator();
StringBuilder sb = new StringBuilder("{");
int newindent = opt.initialIndent + opt.indent;
String o;
if (n == 1) {
o = keys.next();
final Object v = jo.get(o);
if (!skipChildObject(children, opt, o, v)) {
sb.append(quote(o));
sb.append(": ");
sb.append(valueToString(v, opt));
}
} else {
while (keys.hasNext()) {
o = keys.next();
final Object v = jo.get(o);
if (skipChildObject(children, opt, o, v)) {
continue;
}
if (sb.length() > 1) {
sb.append(",\n");
} else {
sb.append('\n');
}
indent(sb, newindent);
sb.append(quote(o.toString()));
sb.append(": ");
sb.append(valueToString(v, options().withIndent(opt.indent).withInitialIndent(newindent)));
}
if (sb.length() > 1) {
sb.append('\n');
indent(sb, newindent);
}
}
/** Render children if any were skipped (in "children in arrays" mode) */
JsonArray childrenArray = children.build();
if (childrenArray.size() > 0) {
if (sb.length() > 1) {
sb.append(",\n");
} else {
sb.append('\n');
}
final Options childOpt = new Options(opt);
childOpt.withInitialIndent(childOpt.initialIndent + newindent);
indent(sb, childOpt.initialIndent);
sb.append(quote(opt.childrenKey)).append(":");
sb.append(prettyPrint(childrenArray, childOpt));
}
sb.append('}');
return sb.toString();
}
use of javax.json.JsonObject in project sling by apache.
the class JsonResponseTest method testSend_3xx.
public void testSend_3xx() throws Exception {
final String location = "http://example.com/test_location";
res.onChange("modified", "argument1");
for (int status = 300; status < 308; status++) {
res.setStatus(status, "3xx Status");
res.setLocation(location);
MockResponseWithHeader response = new MockResponseWithHeader();
res.send(response, true);
JsonObject result = Json.createReader(new StringReader(response.getOutput().toString())).readObject();
assertProperty(result, HtmlResponse.PN_STATUS_CODE, Integer.toString(status));
assertEquals(location, response.getHeader("Location"));
}
}
use of javax.json.JsonObject in project sling by apache.
the class JsonResponseTest method testSend_201.
public void testSend_201() throws Exception {
final String location = "http://example.com/test_location";
res.onChange("modified", "argument1");
res.setStatus(HttpServletResponse.SC_CREATED, "Created");
res.setLocation(location);
MockResponseWithHeader response = new MockResponseWithHeader();
res.send(response, true);
JsonObject result = Json.createReader(new StringReader(response.getOutput().toString())).readObject();
assertProperty(result, HtmlResponse.PN_STATUS_CODE, Integer.toString(HttpServletResponse.SC_CREATED));
assertEquals(location, response.getHeader("Location"));
}
use of javax.json.JsonObject in project sling by apache.
the class JsonResponseTest method testOnChange.
public void testOnChange() throws Exception {
res.onChange("modified", "argument1", "argument2");
Object prop = res.getProperty("changes");
JsonArray changes = assertInstanceOf(prop, JsonArray.class);
assertEquals(1, changes.size());
Object obj = changes.getJsonObject(0);
JsonObject change = assertInstanceOf(obj, JsonObject.class);
assertProperty(change, JSONResponse.PROP_TYPE, "modified");
JsonArray arguments = change.getJsonArray(JSONResponse.PROP_ARGUMENT);
assertEquals(2, arguments.size());
}
use of javax.json.JsonObject in project sling by apache.
the class JSONResponse method getJson.
JsonObject getJson() {
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
for (Map.Entry<String, Object> entry : json.entrySet()) {
if (entry.getValue() != null) {
jsonBuilder.add(entry.getKey(), entry.getValue().toString());
} else {
jsonBuilder.addNull(entry.getKey());
}
}
if (this.error != null) {
jsonBuilder.add("error", Json.createObjectBuilder().add("class", error.getClass().getName()).add("message", error.getMessage()));
}
JsonArrayBuilder changesBuilder = Json.createArrayBuilder();
for (Map<String, Object> entry : changes) {
JsonObjectBuilder entryBuilder = Json.createObjectBuilder();
entryBuilder.add(PROP_TYPE, (String) entry.get(PROP_TYPE));
Object arguments = entry.get(PROP_ARGUMENT);
if (arguments != null) {
if (arguments instanceof List) {
JsonArrayBuilder argumentsBuilder = Json.createArrayBuilder();
for (String argument : ((List<String>) arguments)) {
argumentsBuilder.add(argument);
}
entryBuilder.add(PROP_ARGUMENT, argumentsBuilder);
} else {
entryBuilder.add(PROP_ARGUMENT, (String) arguments);
}
}
changesBuilder.add(entryBuilder);
}
jsonBuilder.add(PROP_CHANGES, changesBuilder);
return jsonBuilder.build();
}
Aggregations