use of javax.json.JsonObject in project sling by apache.
the class OverrideStringParser method toMap.
/**
* Convert JSON object to map.
* @param json JSON object
* @return Map (keys/values are not validated)
*/
private static Map<String, Object> toMap(JsonObject json) {
Map<String, Object> props = new HashMap<>();
Iterator<String> keys = json.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
props.put(key, convertJsonValue(json.get(key)));
}
return props;
}
use of javax.json.JsonObject in project sling by apache.
the class Announcement method asClusterView.
/** create a clusterview from json **/
private static ClusterView asClusterView(final String localClusterViewJSON) {
JsonObject obj = jsonReaderFactory.createReader(new StringReader(localClusterViewJSON)).readObject();
DefaultClusterView clusterView = new DefaultClusterView(obj.getString("id"));
JsonArray instancesObj = obj.getJsonArray("instances");
for (int i = 0; i < instancesObj.size(); i++) {
JsonObject anInstance = instancesObj.getJsonObject(i);
clusterView.addInstanceDescription(asInstance(anInstance));
}
return clusterView;
}
use of javax.json.JsonObject in project sling by apache.
the class Announcement method fromJSON.
/** Create an announcement form json **/
public static Announcement fromJSON(final String topologyAnnouncementJSON) {
JsonObject announcement = jsonReaderFactory.createReader(new StringReader(topologyAnnouncementJSON)).readObject();
final String ownerId = announcement.getString("ownerId");
final int protocolVersion;
if (!announcement.containsKey("protocolVersion")) {
protocolVersion = -1;
} else {
protocolVersion = announcement.getInt("protocolVersion");
}
final Announcement result = new Announcement(ownerId, protocolVersion);
if (announcement.containsKey("created")) {
result.originallyCreatedAt = announcement.getJsonNumber("created").longValue();
}
if (announcement.containsKey("backoffInterval")) {
long backoffInterval = announcement.getJsonNumber("backoffInterval").longValue();
result.backoffInterval = backoffInterval;
}
if (announcement.containsKey("resetBackoff")) {
boolean resetBackoff = announcement.getBoolean("resetBackoff");
result.resetBackoff = resetBackoff;
}
if (announcement.containsKey("loop") && announcement.getBoolean("loop")) {
result.setLoop(true);
return result;
}
if (announcement.containsKey("localClusterView")) {
final String localClusterViewJSON = asJSON(announcement.getJsonObject("localClusterView"));
final ClusterView localClusterView = asClusterView(localClusterViewJSON);
result.setLocalCluster(localClusterView);
}
if (announcement.containsKey("inherited")) {
final Boolean inherited = announcement.getBoolean("inherited");
result.inherited = inherited;
}
if (announcement.containsKey("serverInfo")) {
String serverInfo = announcement.getString("serverInfo");
result.serverInfo = serverInfo;
}
final JsonArray subAnnouncements = announcement.getJsonArray("topologyAnnouncements");
for (int i = 0; i < subAnnouncements.size(); i++) {
String subAnnouncementJSON = subAnnouncements.getString(i);
result.addIncomingTopologyAnnouncement(fromJSON(subAnnouncementJSON));
}
return result;
}
use of javax.json.JsonObject in project sling by apache.
the class Announcement method correspondsTo.
/**
* Compare this Announcement with another one, ignoring the 'created'
* property - which gets added to the JSON object automatically due
* to SLING-3389 wire-backwards-compatibility - and backoffInterval
* introduced as part of SLING-3382
*/
public boolean correspondsTo(Announcement announcement) {
final JsonObject myJson = asJSONObject(true);
final JsonObject otherJson = announcement.asJSONObject(true);
return asJSON(myJson).equals(asJSON(otherJson));
}
use of javax.json.JsonObject in project sling by apache.
the class DiscoveryLiteDescriptor method getDescriptorFrom.
/**
* {"seq":8,"final":true,"id":"aae34e9a-b08d-409e-be10-9ff4106e5387","me":4,"active":[4],"deactivating":[],"inactive":[1,2,3]}
*/
public static DiscoveryLiteDescriptor getDescriptorFrom(ResourceResolver resourceResolver) throws Exception {
Session session = resourceResolver.adaptTo(Session.class);
if (session == null) {
throw new Exception("Could not adapt resourceResolver to session: " + resourceResolver);
}
String descriptorStr = session.getRepository().getDescriptor(DiscoveryLiteDescriptor.OAK_DISCOVERYLITE_CLUSTERVIEW);
if (descriptorStr == null) {
throw new Exception("No value available for descriptor " + OAK_DISCOVERYLITE_CLUSTERVIEW);
}
JsonObject descriptor = jsonReaderFactory.createReader(new StringReader(descriptorStr)).readObject();
return new DiscoveryLiteDescriptor(descriptor);
}
Aggregations