Search in sources :

Example 36 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class StaticRoutingConverter method fromJSON.

@Override
public StaticRouting fromJSON(Model mo, JSONObject o) throws JSONConverterException {
    Network v = Network.get(mo);
    TIntObjectMap<Link> idToLink = new TIntObjectHashMap<>();
    for (Link l : v.getLinks()) {
        idToLink.put(l.id(), l);
    }
    StaticRouting r = new StaticRouting();
    checkKeys(o, ROUTES_LABEL);
    JSONArray a = (JSONArray) o.get(ROUTES_LABEL);
    for (Object ao : a) {
        StaticRouting.NodesMap nm = nodesMapFromJSON(mo, (JSONObject) ((JSONObject) ao).get("nodes_map"));
        Map<Link, Boolean> links = new LinkedHashMap<>();
        JSONArray aoa = (JSONArray) ((JSONObject) ao).get("links");
        for (Object aoao : aoa) {
            links.put(idToLink.get(requiredInt((JSONObject) aoao, "link")), Boolean.valueOf(requiredString((JSONObject) aoao, "direction")));
        }
        r.setStaticRoute(nm, links);
    }
    return r;
}
Also used : JSONObject(net.minidev.json.JSONObject) StaticRouting(org.btrplace.model.view.network.StaticRouting) Network(org.btrplace.model.view.network.Network) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Link(org.btrplace.model.view.network.Link) LinkedHashMap(java.util.LinkedHashMap)

Example 37 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class ActionConverter method makeActionSkeleton.

/**
 * Just create the JSONObject and set the consume and the end attribute.
 *
 * @param a the action to convert
 * @return a skeleton JSONObject
 */
private JSONObject makeActionSkeleton(Action a) {
    JSONObject o = new JSONObject();
    o.put(START_LABEL, a.getStart());
    o.put(END_LABEL, a.getEnd());
    JSONObject hooks = new JSONObject();
    for (Action.Hook k : Action.Hook.values()) {
        JSONArray arr = new JSONArray();
        for (Event e : a.getEvents(k)) {
            arr.add(toJSON(e));
        }
        hooks.put(k.toString(), arr);
    }
    o.put(HOOK_LABEL, hooks);
    return o;
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray)

Example 38 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class ReconfigurationPlanConverter method toJSON.

@Override
public JSONObject toJSON(ReconfigurationPlan plan) throws JSONConverterException {
    JSONObject ob = new JSONObject();
    Model src = plan.getOrigin();
    ActionConverter ac = new ActionConverter(src);
    ob.put(ORIGIN_LABEL, mc.toJSON(src));
    JSONArray actions = new JSONArray();
    for (Action a : plan.getActions()) {
        actions.add(ac.toJSON(a));
    }
    ob.put(ACTIONS_LABEL, actions);
    return ob;
}
Also used : Action(org.btrplace.plan.event.Action) JSONObject(net.minidev.json.JSONObject) Model(org.btrplace.model.Model) JSONArray(net.minidev.json.JSONArray)

Example 39 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class JSONs method requiredNodes.

/**
 * Read an expected list of nodes.
 *
 * @param mo the associated model to browse
 * @param o  the object to parse
 * @param id the key in the map that points to the list
 * @return the parsed list
 * @throws JSONConverterException if the key does not point to a list of nodes identifiers
 */
public static List<Node> requiredNodes(Model mo, JSONObject o, String id) throws JSONConverterException {
    checkKeys(o, id);
    Object x = o.get(id);
    if (!(x instanceof JSONArray)) {
        throw new JSONConverterException("integers expected at key '" + id + "'");
    }
    return nodesFromJSON(mo, (JSONArray) x);
}
Also used : JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 40 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class ModelConverter method newNodes.

/**
 * Build nodes from a key.
 *
 * @param mo  the model to build
 * @param o   the object that contains the node
 * @param key the key associated to the nodes
 * @return the resulting set of nodes
 * @throws JSONConverterException if at least one of the parsed node already exists
 */
private static Set<Node> newNodes(Model mo, JSONObject o, String key) throws JSONConverterException {
    checkKeys(o, key);
    Object x = o.get(key);
    if (!(x instanceof JSONArray)) {
        throw new JSONConverterException("array expected at key '" + key + "'");
    }
    Set<Node> s = new HashSet<>(((JSONArray) x).size());
    for (Object i : (JSONArray) x) {
        int id = (Integer) i;
        Node n = mo.newNode(id);
        if (n == null) {
            throw JSONConverterException.nodeAlreadyDeclared(id);
        }
        s.add(n);
    }
    return s;
}
Also used : JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException) HashSet(java.util.HashSet)

Aggregations

JSONArray (net.minidev.json.JSONArray)55 JSONObject (net.minidev.json.JSONObject)41 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 JSONParser (net.minidev.json.parser.JSONParser)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONConverterException (org.btrplace.json.JSONConverterException)3 VM (org.btrplace.model.VM)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 ContentType (ddf.catalog.data.ContentType)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)2 SourceDescriptor (ddf.catalog.source.SourceDescriptor)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2