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