Search in sources :

Example 31 with JSONArray

use of net.minidev.json.JSONArray in project ddf by codice.

the class GeoJsonQueryResponseTransformer method transform.

@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    if (upstreamResponse == null) {
        throw new CatalogTransformerException("Cannot transform null " + SourceResponse.class.getName());
    }
    JSONObject rootObject = new JSONObject();
    addNonNullObject(rootObject, "hits", upstreamResponse.getHits());
    JSONArray resultsList = new JSONArray();
    if (upstreamResponse.getResults() != null) {
        for (Result result : upstreamResponse.getResults()) {
            if (result == null) {
                throw new CatalogTransformerException("Cannot transform null " + Result.class.getName());
            }
            JSONObject jsonObj = convertToJSON(result);
            if (jsonObj != null) {
                resultsList.add(jsonObj);
            }
        }
    }
    addNonNullObject(rootObject, "results", resultsList);
    String jsonText = JSONValue.toJSONString(rootObject);
    return new BinaryContentImpl(new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8)), DEFAULT_MIME_TYPE);
}
Also used : JSONObject(net.minidev.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONArray(net.minidev.json.JSONArray) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Result(ddf.catalog.data.Result)

Example 32 with JSONArray

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

the class TestCase method fromJSON.

public static TestCase fromJSON(List<Constraint> cstrs, String c) throws ParseException, JSONConverterException {
    JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
    JSONObject o = (JSONObject) p.parse(new StringReader(c));
    String cId = o.getAsString("constraint");
    Optional<Constraint> opt = cstrs.stream().filter(x -> x.id().equals(cId)).findFirst();
    if (!opt.isPresent()) {
        throw new IllegalArgumentException("Unknown constraint '" + cId + "'");
    }
    Constraint cstr = opt.get();
    InstanceConverter ic = new InstanceConverter();
    ic.getConstraintsConverter().register(new ScheduleConverter());
    ReconfigurationPlanConverter rc = new ReconfigurationPlanConverter();
    Instance i = ic.fromJSON(o.getAsString("instance"));
    ReconfigurationPlan plan = rc.fromJSON(o.getAsString("plan"));
    TestCase tc = new TestCase(i, plan, cstr);
    List<Constant> l = new ArrayList<>();
    for (Object x : (JSONArray) o.get("args")) {
        l.add(Constant.fromJSON((JSONObject) x));
    }
    tc.args(l);
    if (cstr.isSatConstraint()) {
        tc.impl(cstr.instantiate(l.stream().map(x -> x.eval(null)).collect(Collectors.toList())));
    }
    if (tc.impl() != null) {
        tc.impl().setContinuous((Boolean) o.get("continuous"));
    }
    return tc;
}
Also used : Constant(org.btrplace.safeplace.spec.term.Constant) ModelView(org.btrplace.model.view.ModelView) JSONConverterException(org.btrplace.json.JSONConverterException) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) Collectors(java.util.stream.Collectors) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) Objects(java.util.Objects) Constraint(org.btrplace.safeplace.spec.Constraint) List(java.util.List) StringReader(java.io.StringReader) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) ParseException(net.minidev.json.parser.ParseException) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Optional(java.util.Optional) Instance(org.btrplace.model.Instance) Collections(java.util.Collections) SatConstraint(org.btrplace.model.constraint.SatConstraint) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Constraint(org.btrplace.safeplace.spec.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) Instance(org.btrplace.model.Instance) Constant(org.btrplace.safeplace.spec.term.Constant) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) StringReader(java.io.StringReader) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject)

Example 33 with JSONArray

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

the class SplitAmongConverter method toJSON.

@Override
public JSONObject toJSON(SplitAmong o) {
    JSONObject c = new JSONObject();
    c.put("id", getJSONId());
    JSONArray vGroups = new JSONArray();
    for (Collection<VM> grp : o.getGroupsOfVMs()) {
        vGroups.add(vmsToJSON(grp));
    }
    JSONArray pGroups = new JSONArray();
    for (Collection<Node> grp : o.getGroupsOfNodes()) {
        pGroups.add(nodesToJSON(grp));
    }
    c.put("vParts", vGroups);
    c.put("pParts", pGroups);
    c.put("continuous", o.isContinuous());
    return c;
}
Also used : JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) Node(org.btrplace.model.Node) JSONArray(net.minidev.json.JSONArray)

Example 34 with JSONArray

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

the class SplitConverter method toJSON.

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

Example 35 with JSONArray

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

the class StaticRoutingConverter method toJSON.

/**
 * Convert a Routing implementation into a JSON object
 *
 * @param routing the routing implementation to convert
 * @return the JSON formatted routing object
 */
@Override
public JSONObject toJSON(StaticRouting routing) {
    JSONObject o = new JSONObject();
    o.put("type", getJSONId());
    JSONArray a = new JSONArray();
    Map<StaticRouting.NodesMap, Map<Link, Boolean>> routes = routing.getStaticRoutes();
    for (Map.Entry<StaticRouting.NodesMap, Map<Link, Boolean>> e : routes.entrySet()) {
        StaticRouting.NodesMap nm = e.getKey();
        JSONObject ao = new JSONObject();
        ao.put("nodes_map", nodesMapToJSON(nm));
        JSONArray links = new JSONArray();
        Map<Link, Boolean> v = e.getValue();
        for (Link l : v.keySet()) {
            JSONObject lo = new JSONObject();
            lo.put("link", l.id());
            lo.put("direction", routes.get(nm).get(l).toString());
            links.add(lo);
        }
        ao.put("links", links);
        a.add(ao);
    }
    o.put(ROUTES_LABEL, a);
    return o;
}
Also used : JSONObject(net.minidev.json.JSONObject) StaticRouting(org.btrplace.model.view.network.StaticRouting) JSONArray(net.minidev.json.JSONArray) LinkedHashMap(java.util.LinkedHashMap) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) TIntObjectMap(gnu.trove.map.TIntObjectMap) Map(java.util.Map) Link(org.btrplace.model.view.network.Link)

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