Search in sources :

Example 26 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project sling by apache.

the class Announcement method asJSONObject.

/** Convert this announcement into a json object **/
private JsonObject asJSONObject(boolean filterTimes) {
    JsonObjectBuilder announcement = Json.createObjectBuilder();
    announcement.add("ownerId", ownerId);
    announcement.add("protocolVersion", protocolVersion);
    // for backwards compatibility!
    if (!filterTimes) {
        announcement.add("created", System.currentTimeMillis());
    }
    announcement.add("inherited", inherited);
    if (loop) {
        announcement.add("loop", loop);
    }
    if (serverInfo != null) {
        announcement.add("serverInfo", serverInfo);
    }
    if (localCluster != null) {
        announcement.add("localClusterView", asJSON(localCluster));
    }
    if (!filterTimes && backoffInterval > 0) {
        announcement.add("backoffInterval", backoffInterval);
    }
    if (resetBackoff) {
        announcement.add("resetBackoff", resetBackoff);
    }
    JsonArrayBuilder incomingAnnouncements = Json.createArrayBuilder();
    for (Iterator<Announcement> it = incomings.iterator(); it.hasNext(); ) {
        Announcement incoming = it.next();
        incomingAnnouncements.add(incoming.asJSONObject(filterTimes));
    }
    announcement.add("topologyAnnouncements", incomingAnnouncements);
    return announcement.build();
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 27 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project sling by apache.

the class Announcement method asJSON.

/** convert an instance description into a json object **/
private static JsonObject asJSON(final InstanceDescription instanceDescription) {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    obj.add("slingId", instanceDescription.getSlingId());
    obj.add("isLeader", instanceDescription.isLeader());
    ClusterView cluster = instanceDescription.getClusterView();
    if (cluster != null) {
        obj.add("cluster", cluster.getId());
    }
    JsonObjectBuilder propertiesObj = Json.createObjectBuilder();
    Map<String, String> propertiesMap = instanceDescription.getProperties();
    for (Iterator<Entry<String, String>> it = propertiesMap.entrySet().iterator(); it.hasNext(); ) {
        Entry<String, String> entry = it.next();
        propertiesObj.add(entry.getKey(), entry.getValue());
    }
    obj.add("properties", propertiesObj);
    return obj.build();
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Entry(java.util.Map.Entry) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 28 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project sling by apache.

the class VersionInfoServlet method getJsonObject.

private JsonObject getJsonObject(Resource resource) throws RepositoryException {
    final JsonObjectBuilder result = Json.createObjectBuilder();
    final Node node = resource.adaptTo(Node.class);
    if (node == null || !node.isNodeType(JcrConstants.MIX_VERSIONABLE)) {
        return result.build();
    }
    final VersionHistory history = node.getVersionHistory();
    final Version baseVersion = node.getBaseVersion();
    for (final VersionIterator it = history.getAllVersions(); it.hasNext(); ) {
        final Version v = it.nextVersion();
        final JsonObjectBuilder obj = Json.createObjectBuilder();
        obj.add("created", createdDate(v));
        obj.add("successors", getArrayBuilder(getNames(v.getSuccessors())));
        obj.add("predecessors", getArrayBuilder(getNames(v.getPredecessors())));
        obj.add("labels", getArrayBuilder(history.getVersionLabels(v)));
        obj.add("baseVersion", baseVersion.isSame(v));
        result.add(v.getName(), obj);
    }
    return Json.createObjectBuilder().add("versions", result).build();
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionIterator(javax.jcr.version.VersionIterator) JsonObjectBuilder(javax.json.JsonObjectBuilder) VersionHistory(javax.jcr.version.VersionHistory)

Example 29 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project sling by apache.

the class JsonObjectCreator method create.

/** Dump given resource in JSON, optionally recursing into its objects */
private static JsonObjectBuilder create(final Resource resource, final int currentRecursionLevel, final int maxRecursionLevels) {
    final ValueMap valueMap = resource.adaptTo(ValueMap.class);
    final Map propertyMap = (valueMap != null) ? valueMap : resource.adaptTo(Map.class);
    final JsonObjectBuilder obj = Json.createObjectBuilder();
    if (propertyMap == null) {
        // no map available, try string
        final String value = resource.adaptTo(String.class);
        if (value != null) {
            // single value property or just plain String resource or...
            obj.add(ResourceUtil.getName(resource), value.toString());
        } else {
            // Try multi-value "property"
            final String[] values = resource.adaptTo(String[].class);
            if (values != null) {
                JsonArrayBuilder builder = Json.createArrayBuilder();
                for (String v : values) {
                    builder.add(v);
                }
                obj.add(ResourceUtil.getName(resource), builder);
            }
        }
    } else {
        @SuppressWarnings("unchecked") final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
        // the node's actual properties
        while (props.hasNext()) {
            final Map.Entry prop = props.next();
            if (prop.getValue() != null) {
                createProperty(obj, valueMap, prop.getKey().toString(), prop.getValue());
            }
        }
    }
    // the child nodes
    if (recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
        final Iterator<Resource> children = ResourceUtil.listChildren(resource);
        while (children.hasNext()) {
            final Resource n = children.next();
            createSingleResource(n, obj, currentRecursionLevel, maxRecursionLevels);
        }
    }
    return obj;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map)

Example 30 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project sling by apache.

the class JsonRenderer method skipChildObject.

/** Decide whether o must be skipped and added to a, when rendering a JSONObject */
private boolean skipChildObject(JsonArrayBuilder a, Options opt, String key, Object value) {
    if (opt.arraysForChildren && (value instanceof JsonObject)) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
        builder.add(opt.childNameKey, key);
        for (Map.Entry<String, JsonValue> entry : ((JsonObject) value).entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        a.add(builder);
        return true;
    }
    return false;
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)34 JsonArrayBuilder (javax.json.JsonArrayBuilder)21 Map (java.util.Map)11 JsonObject (javax.json.JsonObject)10 JsonException (javax.json.JsonException)7 HashMap (java.util.HashMap)6 StringWriter (java.io.StringWriter)5 ArrayList (java.util.ArrayList)5 List (java.util.List)3 Entry (java.util.Map.Entry)3 JsonValue (javax.json.JsonValue)3 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 Privilege (javax.jcr.security.Privilege)2 JsonArray (javax.json.JsonArray)2 JsonString (javax.json.JsonString)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 AttributeReference (com.torodb.core.language.AttributeReference)1 IndexType (com.torodb.core.model.IndexedAttributes.IndexType)1