Search in sources :

Example 41 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)

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

Example 43 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 : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 44 with JSONArray

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

the class AmongConverter method toJSON.

@Override
public JSONObject toJSON(Among o) {
    JSONObject c = new JSONObject();
    c.put("id", getJSONId());
    c.put("vms", vmsToJSON(o.getInvolvedVMs()));
    JSONArray a = new JSONArray();
    for (Collection<Node> grp : o.getGroupsOfNodes()) {
        a.add(nodesToJSON(grp));
    }
    c.put("parts", a);
    c.put("continuous", o.isContinuous());
    return c;
}
Also used : JSONObject(net.minidev.json.JSONObject) Node(org.btrplace.model.Node) JSONArray(net.minidev.json.JSONArray)

Example 45 with JSONArray

use of net.minidev.json.JSONArray in project jmeter-plugins by undera.

the class JSONPathExtractor method process.

@Override
public void process() {
    JMeterContext context = getThreadContext();
    JMeterVariables vars = context.getVariables();
    SampleResult previousResult = context.getPreviousResult();
    String responseData;
    if (getSubject().equals(SUBJECT_VARIABLE)) {
        responseData = vars.get(getSrcVariableName());
    } else {
        responseData = previousResult.getResponseDataAsString();
    }
    try {
        Object jsonPathResult = JsonPath.read(responseData, getJsonPath());
        if (jsonPathResult instanceof JSONArray) {
            Object[] arr = ((JSONArray) jsonPathResult).toArray();
            if (arr.length == 0) {
                throw new PathNotFoundException("Extracted array is empty");
            }
            vars.put(this.getVar(), objectToString(jsonPathResult));
            vars.put(this.getVar() + "_matchNr", objectToString(arr.length));
            int k = 1;
            while (vars.get(this.getVar() + "_" + k) != null) {
                vars.remove(this.getVar() + "_" + k);
                k++;
            }
            for (int n = 0; n < arr.length; n++) {
                vars.put(this.getVar() + "_" + (n + 1), objectToString(arr[n]));
            }
        } else {
            vars.put(this.getVar(), objectToString(jsonPathResult));
        }
    } catch (Exception e) {
        log.debug("Extract failed", e);
        vars.put(this.getVar(), getDefaultValue());
        vars.put(this.getVar() + "_matchNr", "0");
        int k = 1;
        while (vars.get(this.getVar() + "_" + k) != null) {
            vars.remove(this.getVar() + "_" + k);
            k++;
        }
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JSONArray(net.minidev.json.JSONArray) SampleResult(org.apache.jmeter.samplers.SampleResult) JSONObject(net.minidev.json.JSONObject) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

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