List of usage examples for com.google.gson JsonObject getAsJsonObject
public JsonObject getAsJsonObject(String memberName)
From source file:com.sheepdog.mashmesh.Itinerary.java
License:Apache License
public static Itinerary fetch(String fromLocation, String toLocation, String viaLocation, DateTime arrivalTime) throws URISyntaxException, IOException { URIBuilder uriBuilder = new URIBuilder(DIRECTIONS_ENDPOINT_URL); uriBuilder.addParameter("origin", fromLocation); uriBuilder.addParameter("destination", toLocation); uriBuilder.addParameter("mode", "driving"); uriBuilder.addParameter("language", "en_US"); // TODO: Configurable? uriBuilder.addParameter("region", "us"); uriBuilder.addParameter("waypoints", viaLocation); uriBuilder.addParameter("sensor", "false"); URL url = uriBuilder.build().toURL(); BufferedReader responseReader = new BufferedReader(new InputStreamReader(url.openStream())); JsonParser parser = new JsonParser(); JsonObject responseObject = parser.parse(responseReader).getAsJsonObject(); JsonObject route = responseObject.getAsJsonArray("routes").get(0).getAsJsonObject(); JsonArray legs = route.getAsJsonArray("legs"); JsonObject startLeg = legs.get(0).getAsJsonObject(); JsonObject endLeg = legs.get(legs.size() - 1).getAsJsonObject(); DateTime departureTime = arrivalTime; List<Leg> directionLegs = new ArrayList<Leg>(); Preconditions.checkState(legs.size() == 2, "Expected two direction legs in response"); // Process the legs in reverse order so that we can compute departure and arrival // times by working backwards from the desired arrival time. for (int i = legs.size() - 1; i >= 0; i--) { JsonObject leg = legs.get(i).getAsJsonObject(); List<Step> directionSteps = new ArrayList<Step>(); DateTime legArrivalTime = departureTime; for (JsonElement stepElement : leg.getAsJsonArray("steps")) { JsonObject stepObject = stepElement.getAsJsonObject(); int duration = stepObject.getAsJsonObject("duration").get("value").getAsInt(); String htmlInstructions = stepObject.get("html_instructions").getAsString(); String distance = stepObject.getAsJsonObject("distance").get("text").getAsString(); departureTime = departureTime.minusSeconds(duration); directionSteps.add(new Step(htmlInstructions, distance)); }//from w w w .jav a 2 s . c o m Leg directionLeg = new Leg(); directionLeg.departureTime = departureTime; directionLeg.startLatLng = getLatLng(leg.getAsJsonObject("start_location")); directionLeg.arrivalTime = legArrivalTime; directionLeg.endLatLng = getLatLng(leg.getAsJsonObject("end_location")); directionLeg.distanceMeters = leg.getAsJsonObject("distance").get("value").getAsInt(); directionLeg.steps = Collections.unmodifiableList(directionSteps); directionLegs.add(directionLeg); } Collections.reverse(directionLegs); Itinerary itinerary = new Itinerary(); itinerary.startAddress = startLeg.get("start_address").getAsString(); itinerary.endAddress = endLeg.get("end_address").getAsString(); itinerary.legs = Collections.unmodifiableList(directionLegs); itinerary.overviewPolyline = route.getAsJsonObject("overview_polyline").get("points").getAsString(); return itinerary; }
From source file:com.sill.kafka.connect.Mapping.java
License:Apache License
/** * Get the JSON mapping for given index and type. Returns {@code null} if it does not exist. *///from ww w.j ava2 s .c o m public static JsonObject getMapping(JestClient client, String index, String type) throws IOException { final JestResult result = client.execute(new GetMapping.Builder().addIndex(index).addType(type).build()); final JsonObject indexRoot = result.getJsonObject().getAsJsonObject(index); if (indexRoot == null) { return null; } final JsonObject mappingsJson = indexRoot.getAsJsonObject("mappings"); if (mappingsJson == null) { return null; } return mappingsJson.getAsJsonObject(type); }
From source file:com.simiacryptus.mindseye.layers.java.ImgTileSubnetLayer.java
License:Apache License
/** * Instantiates a new Rescaled subnet key. * * @param json the json// w ww. j a v a2s. com * @param rs the rs */ protected ImgTileSubnetLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json, rs); height = json.getAsJsonPrimitive("height").getAsInt(); width = json.getAsJsonPrimitive("width").getAsInt(); strideX = json.getAsJsonPrimitive("strideX").getAsInt(); strideY = json.getAsJsonPrimitive("strideY").getAsInt(); JsonObject subnetwork = json.getAsJsonObject("subnetwork"); }
From source file:com.simiacryptus.mindseye.layers.java.MonitoringSynapse.java
License:Apache License
/** * From json monitoring synapse.//from www.j a v a2 s.c om * * @param json the json * @param rs the rs * @return the monitoring synapse */ @Nonnull public static MonitoringSynapse fromJson(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { @Nonnull final MonitoringSynapse obj = new MonitoringSynapse(json); obj.totalBatches = json.get("totalBatches").getAsInt(); obj.totalItems = json.get("totalItems").getAsInt(); obj.backpropStatistics.readJson(json.getAsJsonObject("backpropStatistics")); obj.forwardStatistics.readJson(json.getAsJsonObject("forwardStatistics")); return obj; }
From source file:com.simiacryptus.mindseye.layers.java.MonitoringWrapperLayer.java
License:Apache License
/** * Instantiates a new Monitoring wrapper key. * * @param json the json// ww w .j av a2 s. co m * @param rs the rs */ protected MonitoringWrapperLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json, rs); if (json.has("forwardPerf")) { forwardPerformance.readJson(json.getAsJsonObject("forwardPerf")); } if (json.has("backwardPerf")) { backwardPerformance.readJson(json.getAsJsonObject("backwardPerf")); } if (json.has("backpropStatistics")) { backwardSignal.readJson(json.getAsJsonObject("backpropStatistics")); } if (json.has("outputStatistics")) { forwardSignal.readJson(json.getAsJsonObject("outputStatistics")); } recordSignalMetrics = json.get("recordSignalMetrics").getAsBoolean(); totalBatches = json.get("totalBatches").getAsInt(); totalItems = json.get("totalItems").getAsInt(); }
From source file:com.simiacryptus.mindseye.layers.java.RescaledSubnetLayer.java
License:Apache License
/** * Instantiates a new Rescaled subnet key. * * @param json the json//from w w w. j a va 2 s .c o m * @param rs the rs */ protected RescaledSubnetLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json); scale = json.getAsJsonPrimitive("scale").getAsInt(); JsonObject subnetwork = json.getAsJsonObject("subnetwork"); this.subnetwork = subnetwork == null ? null : Layer.fromJson(subnetwork, rs); }
From source file:com.simiacryptus.mindseye.layers.java.StochasticSamplingSubnetLayer.java
License:Apache License
/** * Instantiates a new Rescaled subnet key. * * @param json the json//from ww w. ja va 2s. c o m * @param rs the rs */ protected StochasticSamplingSubnetLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json); samples = json.getAsJsonPrimitive("samples").getAsInt(); seed = json.getAsJsonPrimitive("seed").getAsInt(); layerSeed = json.getAsJsonPrimitive("layerSeed").getAsInt(); JsonObject subnetwork = json.getAsJsonObject("subnetwork"); this.subnetwork = subnetwork == null ? null : Layer.fromJson(subnetwork, rs); }
From source file:com.simiacryptus.mindseye.layers.java.WrapperLayer.java
License:Apache License
/** * Instantiates a new Wrapper key.//from w ww .j a va2 s .c o m * * @param json the json * @param rs the rs */ public WrapperLayer(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json); this.inner = Layer.fromJson(json.getAsJsonObject("inner"), rs); }
From source file:com.simiacryptus.mindseye.network.DAGNetwork.java
License:Apache License
/** * Instantiates a new Dag network./*from ww w . ja v a 2 s. c om*/ * * @param json the json * @param rs the rs */ protected DAGNetwork(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { super(json); for (@Nonnull final JsonElement item : json.getAsJsonArray("inputs")) { @Nonnull final UUID key = UUID.fromString(item.getAsString()); inputHandles.add(key); InputNode replaced = inputNodes.put(key, new InputNode(this, key)); if (null != replaced) replaced.freeRef(); } final JsonObject jsonNodes = json.getAsJsonObject("nodes"); final JsonObject jsonLayers = json.getAsJsonObject("layers"); final JsonObject jsonLinks = json.getAsJsonObject("links"); final JsonObject jsonLabels = json.getAsJsonObject("labels"); @Nonnull final Map<UUID, Layer> source_layersByNodeId = new HashMap<>(); @Nonnull final Map<UUID, Layer> source_layersByLayerId = new HashMap<>(); for (@Nonnull final Entry<String, JsonElement> e : jsonLayers.entrySet()) { @Nonnull Layer value = Layer.fromJson(e.getValue().getAsJsonObject(), rs); source_layersByLayerId.put(UUID.fromString(e.getKey()), value); } for (@Nonnull final Entry<String, JsonElement> e : jsonNodes.entrySet()) { @Nonnull final UUID nodeId = UUID.fromString(e.getKey()); @Nonnull final UUID layerId = UUID.fromString(e.getValue().getAsString()); final Layer layer = source_layersByLayerId.get(layerId); assert null != layer; source_layersByNodeId.put(nodeId, layer); } @Nonnull final LinkedHashMap<CharSequence, UUID> labels = new LinkedHashMap<>(); for (@Nonnull final Entry<String, JsonElement> e : jsonLabels.entrySet()) { labels.put(e.getKey(), UUID.fromString(e.getValue().getAsString())); } @Nonnull final Map<UUID, List<UUID>> deserializedLinks = new HashMap<>(); for (@Nonnull final Entry<String, JsonElement> e : jsonLinks.entrySet()) { @Nonnull final ArrayList<UUID> linkList = new ArrayList<>(); for (@Nonnull final JsonElement linkItem : e.getValue().getAsJsonArray()) { linkList.add(UUID.fromString(linkItem.getAsString())); } deserializedLinks.put(UUID.fromString(e.getKey()), linkList); } for (final UUID key : labels.values()) { initLinks(deserializedLinks, source_layersByNodeId, key); } for (final UUID key : source_layersByNodeId.keySet()) { initLinks(deserializedLinks, source_layersByNodeId, key); } @Nonnull final UUID head = UUID.fromString(json.getAsJsonPrimitive("head").getAsString()); initLinks(deserializedLinks, source_layersByNodeId, head); source_layersByLayerId.values().forEach(x -> x.freeRef()); this.labels.putAll(labels); assertConsistent(); }
From source file:com.sixt.service.framework.servicetest.eventing.EventUtils.java
License:Apache License
public static String getEventName(JsonObject event) { String eventName = null;/* ww w .j ava 2 s.co m*/ if (event.has(META)) { eventName = event.getAsJsonObject(META).get(NAME).getAsString(); } else { JsonElement eventType = event.get(EVENT_TYPE); if (eventType != null) { eventName = eventType.getAsString(); } } return eventName; }