use of javax.json.JsonObjectBuilder in project visualee by Thomas-S-B.
the class GraphCreatorTest method testBuildJSONNode.
@Test
public void testBuildJSONNode() {
JavaSourceContainer.getInstance().clear();
DependencyContainer.getInstance().clear();
String name = "MyTestClass";
String sourcecode = "test source code - Matt Pavolka Group - Something People Can Use";
JavaSource javaSource = JavaSourceFactory.getInstance().newJavaSource("MyTestClass");
javaSource.setGroup(2);
javaSource.setPackagePath("de.test.test2");
javaSource.setSourceCode(sourcecode);
JsonObjectBuilder job = GraphCreator.buildJSONNode(javaSource);
JsonObject node = job.build();
assertEquals(name, node.getString("name"));
assertEquals(2, node.getInt("group"));
assertNotNull(node.getString("description"));
assertEquals(sourcecode, node.getString("sourcecode"));
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class AdapterWebConsolePlugin method getJson.
private void getJson(final HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
try {
Map<String, Map<String, List<String>>> values = new HashMap<>();
for (final AdaptableDescription desc : allAdaptables) {
final Map<String, List<String>> adaptableObj;
if (values.containsKey(desc.adaptable)) {
adaptableObj = values.get(desc.adaptable);
} else {
adaptableObj = new HashMap<>();
values.put(desc.adaptable, adaptableObj);
}
for (final String adapter : desc.adapters) {
List<String> conditions = adaptableObj.get(desc.condition == null ? "" : desc.condition);
if (conditions == null) {
conditions = new ArrayList<>();
adaptableObj.put(desc.condition == null ? "" : desc.condition, conditions);
}
conditions.add(adapter);
}
}
final JsonObjectBuilder obj = Json.createObjectBuilder();
for (Map.Entry<String, Map<String, List<String>>> entry : values.entrySet()) {
JsonObjectBuilder adaptable = Json.createObjectBuilder();
for (Map.Entry<String, List<String>> subEnty : entry.getValue().entrySet()) {
if (subEnty.getValue().size() > 1) {
JsonArrayBuilder array = Json.createArrayBuilder();
for (String condition : subEnty.getValue()) {
array.add(condition);
}
adaptable.add(subEnty.getKey(), array);
} else {
adaptable.add(subEnty.getKey(), subEnty.getValue().get(0));
}
}
obj.add(entry.getKey(), adaptable);
}
Json.createGenerator(resp.getWriter()).write(obj.build()).flush();
} catch (final JsonException e) {
throw new ServletException("Unable to produce JSON", e);
}
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class Announcement method asJSON.
/** convert a clusterview into json **/
private static JsonObject asJSON(final ClusterView clusterView) {
JsonObjectBuilder obj = Json.createObjectBuilder();
obj.add("id", clusterView.getId());
JsonArrayBuilder instancesObj = Json.createArrayBuilder();
List<InstanceDescription> instances = clusterView.getInstances();
for (Iterator<InstanceDescription> it = instances.iterator(); it.hasNext(); ) {
InstanceDescription instanceDescription = it.next();
instancesObj.add(asJSON(instanceDescription));
}
obj.add("instances", instancesObj);
return obj.build();
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class TopologyRequestValidator method encodeMessage.
/**
* Encodes a request returning the encoded body
*
* @param body
* @return the encoded body.
* @throws IOException
*/
public String encodeMessage(String body) throws IOException {
checkActive();
if (encryptionEnabled) {
try {
JsonObjectBuilder json = Json.createObjectBuilder();
JsonArrayBuilder array = Json.createArrayBuilder();
for (String value : encrypt(body)) {
array.add(value);
}
json.add("payload", array);
StringWriter writer = new StringWriter();
Json.createGenerator(writer).write(json.build()).close();
return writer.toString();
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (BadPaddingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (UnsupportedEncodingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (NoSuchPaddingException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (JsonException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (InvalidKeySpecException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
} catch (InvalidParameterSpecException e) {
throw new IOException("Unable to Encrypt Message " + e.getMessage());
}
}
return body;
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class DiscoveryLiteDescriptorBuilder method asJson.
public String asJson() {
JsonObjectBuilder json = Json.createObjectBuilder();
if (id != null) {
json.add("id", id);
}
json.add("final", isFinal);
json.add("me", me);
json.add("seq", seqNum);
json.add("active", toArray(Arrays.asList(activeIds)));
json.add("inactive", toArray(Arrays.asList(inactiveIds)));
json.add("deactivating", toArray(Arrays.asList(deactivating)));
StringWriter writer = new StringWriter();
Json.createGenerator(writer).write(json.build()).close();
return writer.toString();
}
Aggregations