Search in sources :

Example 1 with JSONObject

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);
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray)

Example 2 with JSONObject

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);
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray)

Example 3 with JSONObject

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");
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 4 with JSONObject

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"));
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with JSONObject

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);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) Converter(org.osgi.util.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) TypeRule(org.osgi.util.converter.TypeRule) Test(org.junit.Test)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2