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;
}
use of net.minidev.json.JSONArray in project scheduler by btrplace.
the class ModelConverter method fromJSON.
@Override
public Model fromJSON(JSONObject o) throws JSONConverterException {
checkKeys(o, MAPPING_LABEL, ATTRS_LABEL, VIEWS_LABEL);
Model i = new DefaultModel();
fillMapping(i, (JSONObject) o.get(MAPPING_LABEL));
i.setAttributes(AttributesConverter.fromJSON(i, (JSONObject) o.get(ATTRS_LABEL)));
for (Object view : (JSONArray) o.get(VIEWS_LABEL)) {
i.attach(viewsConverter.fromJSON(i, (JSONObject) view));
}
return i;
}
use of net.minidev.json.JSONArray in project scheduler by btrplace.
the class ModelConverter method newVMs.
/**
* Build VMs from a key.
*
* @param mo the model to build
* @param o the object that contains the vm
* @param key the key associated to the VMs
* @return the resulting set of VMs
* @throws JSONConverterException if at least one of the parsed VM already exists
*/
private static Set<VM> newVMs(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<VM> s = new HashSet<>(((JSONArray) x).size());
for (Object i : (JSONArray) x) {
int id = (Integer) i;
VM vm = mo.newVM(id);
if (vm == null) {
throw JSONConverterException.vmAlreadyDeclared(id);
}
s.add(vm);
}
return s;
}
use of net.minidev.json.JSONArray in project scheduler by btrplace.
the class ModelConverter method toJSON.
@Override
public JSONObject toJSON(Model i) throws JSONConverterException {
JSONArray rcs = new JSONArray();
for (ModelView v : i.getViews()) {
rcs.add(viewsConverter.toJSON(v));
}
JSONObject o = new JSONObject();
o.put(MAPPING_LABEL, toJSON(i.getMapping()));
o.put(ATTRS_LABEL, AttributesConverter.toJSON(i.getAttributes()));
o.put(VIEWS_LABEL, rcs);
return o;
}
Aggregations