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