Search in sources :

Example 56 with JsonObject

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>&nbsp;<small>(left brace)</small> and ending
     *  with <code>}</code>&nbsp;<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();
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonString(javax.json.JsonString)

Example 57 with JsonObject

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"));
    }
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 58 with JsonObject

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"));
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 59 with JsonObject

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());
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject)

Example 60 with JsonObject

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();
}
Also used : JsonObject(javax.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

JsonObject (javax.json.JsonObject)302 Test (org.junit.Test)110 JsonArray (javax.json.JsonArray)97 HashMap (java.util.HashMap)68 StringReader (java.io.StringReader)66 ArrayList (java.util.ArrayList)58 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 JsonString (javax.json.JsonString)50 JsonReader (javax.json.JsonReader)49 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 HashSet (java.util.HashSet)20 Map (java.util.Map)20 JsonException (javax.json.JsonException)20 JsonObjectBuilder (javax.json.JsonObjectBuilder)20 Response (javax.ws.rs.core.Response)19 LinkedHashMap (java.util.LinkedHashMap)17 JsonValue (javax.json.JsonValue)14 StringWriter (java.io.StringWriter)13