use of javax.json.JsonObjectBuilder in project visualee by Thomas-S-B.
the class GraphCreator method generateGraph.
public static Graph generateGraph(String fileName, String title, DependencyFilter filter, File outputdirectory) {
id = 0;
Graph graph = new Graph();
graph.setName(fileName);
File jsonFile = new File(outputdirectory.toString() + File.separatorChar + fileName + ".json");
graph.setJsonFile(jsonFile);
File htmlFile = new File(outputdirectory.toString() + File.separatorChar + fileName + ".html");
graph.setHtmlFile(htmlFile);
graph.setTitle(title);
GraphConfigurator.configGraph(graph);
JsonObjectBuilder builder = Json.createObjectBuilder();
// Nodes
JsonArrayBuilder nodesArray = buildJSONNodes(filter);
builder.add("nodes", nodesArray);
// Links
JsonArrayBuilder linksArray = buildJSONLinks(filter);
builder.add("links", linksArray);
JsonObject json = builder.build();
try (PrintStream ps = new PrintStream(graph.getJsonFile())) {
ps.println(json.toString());
} catch (FileNotFoundException ex) {
LogProvider.getInstance().error("Didn't found file " + graph.getJsonFile().getName(), ex);
}
return graph;
}
use of javax.json.JsonObjectBuilder in project visualee by Thomas-S-B.
the class GraphCreator method buildJSONLinks.
static JsonArrayBuilder buildJSONLinks(DependencyFilter filter) {
JsonArrayBuilder linksArray = Json.createArrayBuilder();
int value = 1;
Set<JavaSource> relevantClasses = DependencyContainer.getInstance().getFilteredJavaSources(filter);
for (JavaSource javaSource : relevantClasses) {
for (Dependency d : DependencyContainer.getInstance().getDependencies(javaSource)) {
DependencyType type = d.getDependencyType();
if (filter == null || (relevantClasses.contains(d.getJavaSourceTo()) && filter.contains(type))) {
int source = d.getJavaSourceFrom().getId();
int target = d.getJavaSourceTo().getId();
JsonObjectBuilder linksBuilder = Json.createObjectBuilder();
if (DependencyType.isInverseDirection(type)) {
linksBuilder.add("source", source);
linksBuilder.add("target", target);
} else {
linksBuilder.add("source", target);
linksBuilder.add("target", source);
}
linksBuilder.add("value", value);
linksBuilder.add("type", type.toString());
linksArray.add(linksBuilder);
}
}
}
return linksArray;
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class AbstractGetAclServlet method addTo.
private JsonObjectBuilder addTo(JsonObjectBuilder builder, String key, Object value) {
if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) {
builder.add(key, ((Number) value).longValue());
} else if (value instanceof Float || value instanceof Double) {
builder.add(key, ((Number) value).doubleValue());
} else if (value instanceof Privilege) {
JsonObjectBuilder privilegeBuilder = Json.createObjectBuilder();
privilegeBuilder.add("name", ((Privilege) value).getName());
builder.add(key, privilegeBuilder);
} else if (value instanceof String) {
builder.add(key, (String) value);
} else {
builder.add(key, value.toString());
}
return builder;
}
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();
}
Aggregations