List of usage examples for com.google.gson JsonArray size
public int size()
From source file:at.orz.arangodb.util.JsonUtils.java
License:Apache License
public static double[] toDoubleArray(JsonArray array) { int len = array.size(); double[] darray = new double[len]; for (int i = 0; i < len; i++) { darray[i] = toDouble(array.get(i)); }/*www . ja v a 2s. c o m*/ return darray; }
From source file:augsburg.se.alltagsguide.common.EventCategory.java
License:Open Source License
@NonNull public static List<EventCategory> fromJson(@NonNull JsonArray array) { List<EventCategory> categories = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JsonObject obj = array.get(i).getAsJsonObject(); EventCategory category = fromJson(obj); if (category != null) { categories.add(category);/*from www . j a v a 2 s . c om*/ } } return categories; }
From source file:augsburg.se.alltagsguide.common.EventTag.java
License:Open Source License
@NonNull public static List<EventTag> fromJson(@NonNull JsonArray array) { List<EventTag> tags = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { EventTag tag = fromJson(array.get(i).getAsJsonObject()); tags.add(tag);/*from w w w .jav a2 s . c o m*/ } return tags; }
From source file:augsburg.se.alltagsguide.serialization.EventPageSerializer.java
License:Open Source License
@NonNull private List<EventPage> getPagesByParentId(final EventPage parent, final int parentId, @NonNull final JsonArray jsonPages) { final List<EventPage> result = new ArrayList<>(); for (int i = 0; i < jsonPages.size(); i++) { JsonObject jsonPage = jsonPages.get(i).getAsJsonObject(); if (jsonPage.get("parent").getAsInt() == parentId) { EventPage page = EventPage.fromJson(jsonPage); page.setParent(parent);/*from ww w.j a va 2 s . co m*/ result.add(page); } } return result; }
From source file:augsburg.se.alltagsguide.serialization.LanguageSerializer.java
License:Open Source License
@NonNull private List<Language> parseLanguages(@NonNull final JsonArray jsonPages) { List<Language> Languages = new ArrayList<>(); for (int i = 0; i < jsonPages.size(); i++) { Languages.add(Language.fromJson(jsonPages.get(i).getAsJsonObject())); }/* ww w . j av a 2 s . co m*/ return Languages; }
From source file:augsburg.se.alltagsguide.serialization.LocationSerializer.java
License:Open Source License
@NonNull private List<Location> parseLocations(@NonNull final JsonArray jsonPages) { List<Location> locations = new ArrayList<>(); for (int i = 0; i < jsonPages.size(); i++) { locations.add(Location.fromJson(jsonPages.get(i).getAsJsonObject())); }/*from www. j av a 2 s . c o m*/ return locations; }
From source file:augsburg.se.alltagsguide.serialization.PageSerializer.java
License:Open Source License
private List<Page> parsePages(@NonNull final JsonArray jsonPages) { List<Page> pages = new ArrayList<>(); for (int i = 0; i < jsonPages.size(); i++) { pages.add(Page.fromJson(jsonPages.get(i).getAsJsonObject())); }/*from w w w . ja v a 2 s. c o m*/ return pages; }
From source file:be.iminds.iot.dianne.dataset.DatasetConfigurator.java
License:Open Source License
private void parseDatasetConfiguration(File f) { try {/*from w w w. j a v a2 s .c o m*/ // parse any adapter configurations from JSON and apply config? JsonParser parser = new JsonParser(); JsonObject json = parser.parse(new JsonReader(new FileReader(f))).getAsJsonObject(); String name = json.get("name").getAsString(); if (name == null) return; // should have a name Hashtable<String, Object> props = new Hashtable<>(); String dir = f.getParentFile().getAbsolutePath(); props.put("dir", dir); String pid = null; if (json.has("adapter")) { String adapter = json.get("adapter").getAsString(); pid = adapter.contains(".") ? adapter : "be.iminds.iot.dianne.dataset.adapters." + adapter; // in case of adapter, set Dataset target: the dataset it is adapting String dataset = json.get("dataset").getAsString(); props.put("Dataset.target", "(name=" + dataset + ")"); } else if (json.has("type")) { String type = json.get("type").getAsString(); pid = "be.iminds.iot.dianne.dataset." + type; } else { // some hard coded pids if (name.startsWith("MNIST")) { pid = "be.iminds.iot.dianne.dataset.MNIST"; } else if (name.startsWith("CIFAR-100")) { pid = "be.iminds.iot.dianne.dataset.CIFAR100"; } else if (name.startsWith("CIFAR-10")) { pid = "be.iminds.iot.dianne.dataset.CIFAR10"; } else if (name.startsWith("STL-10")) { pid = "be.iminds.iot.dianne.dataset.STL10"; } else if (name.startsWith("SVHN")) { pid = "be.iminds.iot.dianne.dataset.SVHN"; } else if (name.equalsIgnoreCase("ImageNetValidation")) { pid = "be.iminds.iot.dianne.dataset.ImageNet.validation"; } else if (name.equalsIgnoreCase("ImageNetTraining")) { pid = "be.iminds.iot.dianne.dataset.ImageNet.training"; } else { pid = "be.iminds.iot.dianne.dataset." + name; } } // set an aiolos instance id using the dataset name to treat // equally named datasets as single instance in the network props.put("aiolos.instance.id", name); // combine all offered interfaces (might be SequenceDataset or ExperiencePool) props.put("aiolos.combine", "*"); // TODO use object conversion from JSON here? Configuration config = ca.createFactoryConfiguration(pid, null); json.entrySet().stream().forEach(e -> { if (e.getValue().isJsonArray()) { JsonArray a = e.getValue().getAsJsonArray(); String[] val = new String[a.size()]; for (int i = 0; i < val.length; i++) { val[i] = a.get(i).getAsString(); } props.put(e.getKey(), val); } else { props.put(e.getKey(), e.getValue().getAsString()); } }); config.update(props); } catch (Exception e) { System.err.println("Error parsing Dataset config file: " + f.getAbsolutePath()); e.printStackTrace(); } }
From source file:be.iminds.iot.dianne.jsonrpc.DianneRequestHandler.java
License:Open Source License
@Override public void handleRequest(JsonObject request, JsonWriter writer) throws IOException { String i = "null"; if (request.has("id")) { i = request.get("id").getAsString(); }//from w w w .j a v a 2 s .com final String id = i; if (!request.has("jsonrpc")) { writeError(writer, id, -32600, "Invalid JSONRPC request"); return; } if (!request.get("jsonrpc").getAsString().equals("2.0")) { writeError(writer, id, -32600, "Wrong JSONRPC version: " + request.get("jsonrpc").getAsString()); return; } if (!request.has("method")) { writeError(writer, id, -32600, "No method specified"); return; } String method = request.get("method").getAsString(); // TODO use a more generic approach here? switch (method) { case "deploy": try { String description = null; UUID runtimeId = null; String[] tags = null; NeuralNetworkInstanceDTO nni; JsonArray params = request.get("params").getAsJsonArray(); if (params.size() > 1) { description = params.get(1).getAsString(); } if (params.size() > 2) { runtimeId = UUID.fromString(params.get(2).getAsString()); } if (params.size() > 3) { tags = new String[params.size() - 3]; for (int t = 3; t < params.size(); t++) { tags[t - 3] = params.get(t).getAsString(); } } if (params.get(0).isJsonPrimitive()) { String nnName = params.get(0).getAsString(); nni = platform.deployNeuralNetwork(nnName, description, runtimeId, tags); } else { NeuralNetworkDTO nn = DianneJSONConverter.parseJSON(params.get(0).getAsJsonObject()); nni = platform.deployNeuralNetwork(nn, description, runtimeId, tags); } writeResult(writer, id, nni.id.toString()); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "undeploy": try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { String s = params.get(0).getAsString(); UUID nnId = UUID.fromString(s); platform.undeployNeuralNetwork(nnId); writeResult(writer, id, nnId); } } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "forward": try { JsonArray params = request.get("params").getAsJsonArray(); if (params.size() != 2) { throw new Exception("2 parameters expected"); } if (!params.get(0).isJsonPrimitive()) throw new Exception("first parameter should be neural network instance id"); if (!params.get(1).isJsonArray()) throw new Exception("second parameter should be input data"); String s = params.get(0).getAsString(); UUID nnId = UUID.fromString(s); NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId); if (nni == null) { writeError(writer, id, -32603, "Neural network with id " + nnId + " does not exist."); return; } NeuralNetwork nn = dianne.getNeuralNetwork(nni).getValue(); JsonArray in = params.get(1).getAsJsonArray(); Tensor input = asTensor(in); nn.forward(null, null, input).then(p -> { int argmax = TensorOps.argmax(p.getValue().tensor); writeResult(writer, id, nn.getOutputLabels()[argmax]); return null; }, p -> { writeError(writer, id, -32603, "Error during forward: " + p.getFailure().getMessage()); }); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "learn": case "eval": case "act": String[] nnName = null; NeuralNetworkDTO nn = null; String dataset; Map<String, String> config; try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { nnName = params.get(0).getAsString().split(","); for (int k = 0; k < nnName.length; k++) { nnName[k] = nnName[k].trim(); } } else { nn = DianneJSONConverter.parseJSON(params.get(0).getAsJsonObject()); } dataset = params.get(1).getAsString(); config = params.get(2).getAsJsonObject().entrySet().stream() .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getAsString())); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } // call coordinator if (method.equals("learn")) { // learn Promise<LearnResult> result = null; if (nnName != null) { result = coordinator.learn(dataset, config, nnName); } else { result = coordinator.learn(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, p.getValue()); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } else if (method.equals("eval")) { // eval Promise<EvaluationResult> result = null; if (nnName != null) { result = coordinator.eval(dataset, config, nnName); } else { result = coordinator.eval(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, p.getValue()); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } else if (method.equals("act")) { Promise<AgentResult> result = null; if (nnName != null) { result = coordinator.act(dataset, config, nnName); } else { result = coordinator.act(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, null); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } break; case "learnResult": case "evaluationResult": case "agentResult": case "job": case "stop": UUID jobId = null; try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { String s = params.get(0).getAsString(); jobId = UUID.fromString(s); } } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } if (method.equals("learnResult")) { writeResult(writer, id, coordinator.getLearnResult(jobId)); } else if (method.equals("evaluationResult")) { writeResult(writer, id, coordinator.getEvaluationResult(jobId)); } else if (method.equals("agentResult")) { writeResult(writer, id, coordinator.getAgentResult(jobId)); } else if (method.equals("stop")) { try { coordinator.stop(jobId); } catch (Exception e) { writeError(writer, id, -32603, "Error stopping job: " + e.getMessage()); } } else { writeResult(writer, id, coordinator.getJob(jobId)); } break; case "availableNeuralNetworks": writeResult(writer, id, platform.getAvailableNeuralNetworks()); break; case "availableDatasets": writeResult(writer, id, datasets.getDatasets().stream().map(d -> d.name).collect(Collectors.toList())); break; case "queuedJobs": writeResult(writer, id, coordinator.queuedJobs()); break; case "runningJobs": writeResult(writer, id, coordinator.runningJobs()); break; case "finishedJobs": writeResult(writer, id, coordinator.finishedJobs()); break; case "notifications": writeResult(writer, id, coordinator.getNotifications()); break; case "status": writeResult(writer, id, coordinator.getStatus()); break; case "devices": writeResult(writer, id, coordinator.getDevices()); break; default: writeError(writer, id, -32601, "Method " + method + " not found"); } }
From source file:be.iminds.iot.dianne.jsonrpc.DianneRequestHandler.java
License:Open Source License
private Tensor asTensor(JsonArray array) { // support up to 3 dim input atm int dim0 = 1; int dim1 = 1; int dim2 = 1; int dims = 1; dim0 = array.size(); if (array.get(0).isJsonArray()) { dims = 2;/* ww w . j a va 2s. co m*/ JsonArray a = array.get(0).getAsJsonArray(); dim1 = a.size(); if (a.get(0).isJsonArray()) { dims = 3; dim2 = a.get(0).getAsJsonArray().size(); } } int size = dim0 * dim1 * dim2; float[] data = new float[size]; int k = 0; for (int i = 0; i < dim0; i++) { for (int j = 0; j < dim1; j++) { for (int l = 0; l < dim2; l++) { JsonElement e = array.get(i); if (e.isJsonArray()) { e = e.getAsJsonArray().get(j); if (e.isJsonArray()) { e = e.getAsJsonArray().get(l); } } data[k++] = e.getAsFloat(); } } } int[] d = new int[dims]; d[0] = dim0; if (dims > 1) d[1] = dim1; if (dims > 2) d[2] = dim2; return new Tensor(data, d); }