use of org.apache.sling.commons.json.JSONObject in project flink by apache.
the class JSONGenerator method visitIteration.
private void visitIteration(JSONArray jsonArray, List<Integer> toVisit, int headId, Map<Integer, Integer> edgeRemapings, JSONArray iterationInEdges) throws JSONException {
Integer vertexID = toVisit.get(0);
StreamNode vertex = streamGraph.getStreamNode(vertexID);
toVisit.remove(vertexID);
// Ignoring head and tail to avoid redundancy
if (!streamGraph.vertexIDtoLoopTimeout.containsKey(vertexID)) {
JSONObject obj = new JSONObject();
jsonArray.put(obj);
decorateNode(vertexID, obj);
JSONArray inEdges = new JSONArray();
obj.put(PREDECESSORS, inEdges);
for (StreamEdge inEdge : vertex.getInEdges()) {
int inputID = inEdge.getSourceId();
if (edgeRemapings.keySet().contains(inputID)) {
decorateEdge(inEdges, inEdge, inputID);
} else if (!streamGraph.vertexIDtoLoopTimeout.containsKey(inputID)) {
decorateEdge(iterationInEdges, inEdge, inputID);
}
}
edgeRemapings.put(vertexID, headId);
visitIteration(jsonArray, toVisit, headId, edgeRemapings, iterationInEdges);
}
}
use of org.apache.sling.commons.json.JSONObject in project flink by apache.
the class JSONGenerator method visit.
private void visit(JSONArray jsonArray, List<Integer> toVisit, Map<Integer, Integer> edgeRemapings) throws JSONException {
Integer vertexID = toVisit.get(0);
StreamNode vertex = streamGraph.getStreamNode(vertexID);
if (streamGraph.getSourceIDs().contains(vertexID) || Collections.disjoint(vertex.getInEdges(), toVisit)) {
JSONObject node = new JSONObject();
decorateNode(vertexID, node);
if (!streamGraph.getSourceIDs().contains(vertexID)) {
JSONArray inputs = new JSONArray();
node.put(PREDECESSORS, inputs);
for (StreamEdge inEdge : vertex.getInEdges()) {
int inputID = inEdge.getSourceId();
Integer mappedID = (edgeRemapings.keySet().contains(inputID)) ? edgeRemapings.get(inputID) : inputID;
decorateEdge(inputs, inEdge, mappedID);
}
}
jsonArray.put(node);
toVisit.remove(vertexID);
} else {
Integer iterationHead = -1;
for (StreamEdge inEdge : vertex.getInEdges()) {
int operator = inEdge.getSourceId();
if (streamGraph.vertexIDtoLoopTimeout.containsKey(operator)) {
iterationHead = operator;
}
}
JSONObject obj = new JSONObject();
JSONArray iterationSteps = new JSONArray();
obj.put(STEPS, iterationSteps);
obj.put(ID, iterationHead);
obj.put(PACT, "IterativeDataStream");
obj.put(PARALLELISM, streamGraph.getStreamNode(iterationHead).getParallelism());
obj.put(CONTENTS, "Stream Iteration");
JSONArray iterationInputs = new JSONArray();
obj.put(PREDECESSORS, iterationInputs);
toVisit.remove(iterationHead);
visitIteration(iterationSteps, toVisit, iterationHead, edgeRemapings, iterationInputs);
jsonArray.put(obj);
}
if (!toVisit.isEmpty()) {
visit(jsonArray, toVisit, edgeRemapings);
}
}
use of org.apache.sling.commons.json.JSONObject in project flink by apache.
the class JSONGenerator method decorateEdge.
private void decorateEdge(JSONArray inputArray, StreamEdge inEdge, int mappedInputID) throws JSONException {
JSONObject input = new JSONObject();
inputArray.put(input);
input.put(ID, mappedInputID);
input.put(SHIP_STRATEGY, inEdge.getPartitioner());
input.put(SIDE, (inputArray.length() == 0) ? "first" : "second");
}
use of org.apache.sling.commons.json.JSONObject in project felix by apache.
the class JsonSerializerTest method testJSONCodec.
@Test
public void testJSONCodec() throws Exception {
Map<Object, Object> m1 = new HashMap<>();
m1.put("x", true);
m1.put("y", null);
Map<Object, Object> m = new HashMap<>();
m.put(1, 11L);
m.put("ab", "cd");
m.put(true, m1);
JsonSerializerImpl jsonCodec = new JsonSerializerImpl();
String json = jsonCodec.serialize(m).toString();
JSONObject jo = new JSONObject(json);
assertEquals(11, jo.getInt("1"));
assertEquals("cd", jo.getString("ab"));
JSONObject jo2 = jo.getJSONObject("true");
assertEquals(true, jo2.getBoolean("x"));
assertTrue(jo2.isNull("y"));
@SuppressWarnings("rawtypes") Map m2 = jsonCodec.deserialize(Map.class).from(json);
// m2 is not exactly equal to m, as the keys are all strings now, this is unavoidable with JSON
assertEquals(m.size(), m2.size());
assertEquals(m.get(1), m2.get("1"));
assertEquals(m.get("ab"), m2.get("ab"));
assertEquals(m.get(true), m2.get("true"));
}
use of org.apache.sling.commons.json.JSONObject in project felix by apache.
the class JsonSerializerTest method testCodecWithAdapter.
@Test
public void testCodecWithAdapter() throws JSONException {
Map<String, Foo> m1 = new HashMap<>();
m1.put("f", new Foo("fofofo"));
Map<String, Map<String, Foo>> m = new HashMap<>();
m.put("submap", m1);
Converter ca = converter.newConverterBuilder().rule(new TypeRule<Foo, String>(Foo.class, String.class, Foo::tsFun)).rule(new TypeRule<String, Foo>(String.class, Foo.class, v -> Foo.fsFun(v))).build();
JsonSerializerImpl jsonCodec = new JsonSerializerImpl();
String json = jsonCodec.serialize(m).convertWith(ca).toString();
JSONObject jo = new JSONObject(json);
assertEquals(1, jo.length());
JSONObject jo1 = jo.getJSONObject("submap");
assertEquals("<fofofo>", jo1.getString("f"));
// And convert back
Map<String, Map<String, Foo>> m2 = jsonCodec.deserialize(new TypeReference<Map<String, Map<String, Foo>>>() {
}).convertWith(ca).from(json);
assertEquals(m, m2);
}
Aggregations