Search in sources :

Example 51 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 52 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 : Node(org.btrplace.model.Node) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException) HashSet(java.util.HashSet)

Example 53 with JSONArray

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;
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) JSONObject(net.minidev.json.JSONObject) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 54 with JSONArray

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;
}
Also used : VM(org.btrplace.model.VM) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException) HashSet(java.util.HashSet)

Example 55 with JSONArray

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;
}
Also used : JSONObject(net.minidev.json.JSONObject) ModelView(org.btrplace.model.view.ModelView) JSONArray(net.minidev.json.JSONArray)

Aggregations

JSONArray (net.minidev.json.JSONArray)71 JSONObject (net.minidev.json.JSONObject)52 Test (org.junit.Test)13 HashMap (java.util.HashMap)10 JSONParser (net.minidev.json.parser.JSONParser)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 HashSet (java.util.HashSet)6 List (java.util.List)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 JSONConverterException (org.btrplace.json.JSONConverterException)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 JsonPath (com.jayway.jsonpath.JsonPath)3 Option (com.jayway.jsonpath.Option)3 ContentType (ddf.catalog.data.ContentType)3 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)3 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)3 SourceDescriptor (ddf.catalog.source.SourceDescriptor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3