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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations