List of usage examples for com.fasterxml.jackson.databind JsonNode size
public int size()
From source file:org.n52.io.geojson.GeoJSONDecoder.java
protected MultiLineString decodeMultiLineString(JsonNode node, GeometryFactory fac) throws GeoJSONException { JsonNode coordinates = requireCoordinates(node); LineString[] lineStrings = new LineString[coordinates.size()]; for (int i = 0; i < coordinates.size(); ++i) { JsonNode coords = coordinates.get(i); lineStrings[i] = fac.createLineString(decodeCoordinates(coords)); }/*from w w w .j a va2 s . c o m*/ return fac.createMultiLineString(lineStrings); }
From source file:net.sf.taverna.t2.activities.apiconsumer.ApiConsumerActivityFactory.java
@Override public Set<ActivityInputPort> getInputPorts(JsonNode json) { Set<ActivityInputPort> inputPorts = new HashSet<>(); // All non-static methods need the object to invoke the method on and it is // passed through the input port called 'object'. Non-static methods and constructors // also return the same object through the output port called 'object' if (!json.get("isMethodStatic").asBoolean() && !json.get("isMethodConstructor").asBoolean()) { inputPorts.add(edits.createActivityInputPort("object", 0, false, null, null)); }/*from www . j av a 2s . com*/ // Add input ports for method's parameters JsonNode parameterNames = json.get("parameterNames"); JsonNode parameterTypes = json.get("parameterTypes"); JsonNode parameterDimensions = json.get("parameterDimensions"); for (int i = 0; i < parameterNames.size(); i++) { // Create input ports... if (ApiConsumerActivity.canRegisterAsString(parameterTypes.get(i).textValue())) { if (parameterTypes.get(i).textValue().equals("char")) { // char, char[] are treated as a string (where char is a string with only one character), // so the port depth for char[] is 0 rather than 1 as is expected for an array if (parameterDimensions.get(i).intValue() == 0 || parameterDimensions.get(i).intValue() == 1) { inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), 0, true, null, String.class)); } // char[][], etc. else { inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), parameterDimensions.get(i).intValue() - 1, true, null, String.class)); } } else { inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), parameterDimensions.get(i).intValue(), true, null, String.class)); } } else if (parameterTypes.get(i).textValue().equals("byte")) { // byte, byte[] are treated as a stream of depth 0 rather that array of bytes of depth 1. // byte is treated as byte[] with only one element. // Note that the port depth is set to 0 rather than 1 as is expected for an array if (parameterDimensions.get(i).intValue() == 0 || parameterDimensions.get(i).intValue() == 1) { inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), 0, true, null, byte[].class)); } // byte[][], etc. else { inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), parameterDimensions.get(i).intValue() - 1, true, null, byte[].class)); } } else { //POJO inputPorts.add(edits.createActivityInputPort(parameterNames.get(i).textValue(), parameterDimensions.get(i).intValue(), false, null, null)); } } return inputPorts; }
From source file:org.aperte.lp.movies.OpeningParser.java
/** * Returns a list of opening movies given a JSON input stream conforming * to the Rotten Tomatoes opening movie model. * * @param in the input stream//from w w w . j a v a 2s.c o m * @return the opening movies * @throws ParseException if there's an error parsing the input stream * @throws IOException if there's an error reading the input stream */ List<OpeningMovie> parseMovies(InputStream in) throws ParseException, IOException { ObjectMapper mapper = new ObjectMapper(); JsonNode root = null; try { root = mapper.readTree(in); } catch (JsonProcessingException e) { throw new ParseException("Unable to parse JSON", e); } if (root != null) { JsonNode movies = root.get("movies"); if (movies == null) { return Collections.<OpeningMovie>emptyList(); } List<OpeningMovie> result = new ArrayList<OpeningMovie>(movies.size()); for (JsonNode movie : movies) { OpeningMovie o = new OpeningMovie(); JsonNode title = movie.get("title"); if (title != null) { o.setTitle(title.textValue()); } JsonNode ratings = movie.get("ratings"); if (ratings != null) { JsonNode criticsRating = ratings.get("critics_rating"); if (criticsRating != null) { o.setCriticsRating(criticsRating.textValue()); } JsonNode criticsScore = ratings.get("critics_score"); if (criticsScore != null) { o.setCriticsScore(criticsScore.intValue()); } JsonNode audienceRating = ratings.get("audience_rating"); if (audienceRating != null) { o.setAudienceRating(audienceRating.textValue()); } JsonNode audienceScore = ratings.get("audience_score"); if (audienceScore != null) { o.setAudienceScore(audienceScore.intValue()); } } JsonNode posters = movie.get("posters"); if (posters != null) { JsonNode thumbnail = posters.get("thumbnail"); if (thumbnail != null) { o.setPosterThumbnailUrl(thumbnail.textValue()); } JsonNode profile = posters.get("profile"); if (profile != null) { o.setPosterProfileUrl(profile.textValue()); } } JsonNode cast = movie.get("abridged_cast"); if (cast != null) { List<String> actorNames = new ArrayList<String>(cast.size()); for (JsonNode actor : cast) { JsonNode name = actor.get("name"); if (name != null) { actorNames.add(name.textValue()); } } o.setCast(actorNames); } result.add(o); } return result; } return Collections.<OpeningMovie>emptyList(); }
From source file:com.github.fge.jsonschema.keyword.digest.helpers.DraftV3TypeKeywordDigester.java
@Override public JsonNode digest(final JsonNode schema) { final ObjectNode ret = FACTORY.objectNode(); final ArrayNode simpleTypes = FACTORY.arrayNode(); ret.put(keyword, simpleTypes);//from w w w .j a va2s .c om final ArrayNode schemas = FACTORY.arrayNode(); ret.put("schemas", schemas); final JsonNode node = schema.get(keyword); final EnumSet<NodeType> set = EnumSet.noneOf(NodeType.class); if (node.isTextual()) // Single type putType(set, node.textValue()); else { // More than one type, and possibly schemas final int size = node.size(); JsonNode element; for (int index = 0; index < size; index++) { element = node.get(index); if (element.isTextual()) putType(set, element.textValue()); else schemas.add(index); } } /* * If all types are there, no need to collect schemas */ if (EnumSet.complementOf(set).isEmpty()) schemas.removeAll(); /* * Note that as this is an enumset, order is guaranteed */ for (final NodeType type : set) simpleTypes.add(type.toString()); return ret; }
From source file:br.com.ingenieux.mojo.simpledb.cmd.PutAttributesCommand.java
private Collection<ReplaceableAttribute> getAttributesFrom(ArrayNode attributesNode, boolean replaceP) { List<ReplaceableAttribute> attributeList = new ArrayList<ReplaceableAttribute>(); for (int i = 0; i < attributesNode.size(); i++) { ObjectNode objectNode = (ObjectNode) attributesNode.get(i); Iterator<String> itFieldName = objectNode.fieldNames(); while (itFieldName.hasNext()) { String key = itFieldName.next(); JsonNode valueNode = objectNode.get(key); if (valueNode.isValueNode()) { attributeList.add(new ReplaceableAttribute(key, valueNode.asText(), replaceP)); } else if (valueNode.isArray()) { for (int j = 0; j < valueNode.size(); j++) { JsonNode scalarValueNode = valueNode.get(j); attributeList.add(new ReplaceableAttribute(key, scalarValueNode.asText(), replaceP)); }/*from w w w . jav a2s. c om*/ } } } return attributeList; }
From source file:com.github.mrstampy.gameboot.otp.netty.client.ClientHandler.java
private void unencrypted(ChannelHandlerContext ctx, byte[] msg) throws Exception { Response r = getResponse(msg); lastResponse = r;//from w w w . j av a2 s. co m boolean c = ctx.pipeline().get(SslHandler.class) != null; log.info("Unencrypted: on {} channel\n{}", (c ? "secured" : "unsecured"), mapper.writeValueAsString(r)); if (!ok(r.getResponseCode())) return; if (ResponseCode.INFO == r.getResponseCode()) { Object[] payload = r.getPayload(); if (payload == null || payload.length == 0 || !(payload[0] instanceof Map<?, ?>)) { throw new IllegalStateException("Expecting map of systemId:[value]"); } systemId = (Long) ((Map<?, ?>) payload[0]).get("systemId"); log.info("Setting system id {}", systemId); clearChannel = ctx.channel(); return; } JsonNode node = mapper.readTree(msg); JsonNode response = node.get("payload"); boolean hasKey = response != null && response.isArray() && response.size() == 1; if (hasKey) { log.info("Setting key"); otpKey = response.get(0).binaryValue(); return; } switch (r.getType()) { case OtpKeyRequest.TYPE: log.info("Deleting key"); otpKey = null; break; default: break; } }
From source file:com.redhat.lightblue.util.JsonNodeDocTest.java
@Test public void getArray() { JsonNode node = createJsonNode("array"); JsonDoc doc = new JsonDoc(node); JsonNode result = doc.get(new Path("array")); Assert.assertNotNull(result);//from w w w . ja v a 2 s. c om Assert.assertTrue("unexpected class", result instanceof ArrayNode); Assert.assertTrue(node.size() > 0); JsonNodeCursor i = doc.cursor(new Path("array")); Assert.assertTrue(i.firstChild()); for (int a = 0; i.nextSibling(); a++) { Assert.assertEquals(String.valueOf(a), ((TextNode) i.getCurrentNode()).textValue()); } }
From source file:com.googlecode.jsonschema2pojo.rules.AdditionalPropertiesRule.java
/** * Applies this schema rule to take the required code generation steps. * <p>/*from w w w . j a v a 2s. c o m*/ * If additionalProperties is specified and set to the boolean value * <code>false</code>, this rule does not make any change to the generated * Java type (the type does not allow additional properties). * <p> * If the additionalProperties node is <code>null</code> (not specified in * the schema) or empty, then a new bean property named * "additionalProperties", of type {@link Map}{@literal <String,Object>} is * added to the generated type (with appropriate accessors). The accessors * are annotated to allow unrecognised (additional) properties found in JSON * data to be marshalled/unmarshalled from/to this map. * <p> * If the additionalProperties node is present and specifies a schema, then * an "additionalProperties" map is added to the generated type. This time * the map values will be restricted and must be instances of a newly * generated Java type that will be created based on the * additionalProperties schema provided. If the schema does not specify the * javaType property, the name of the newly generated type will be derived * from the nodeName and the suffix 'Property'. * * @param nodeName * the name of the schema node for which the additionalProperties * node applies * @param node * the additionalProperties node itself, found in the schema (may * be null if not specified in the schema) * @param jclass * the Java type that is being generated to represent this schema * @return the given Java type jclass */ @Override public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) { if (node != null && node.isBoolean() && node.asBoolean() == false) { // no additional properties allowed return jclass; } JType propertyType; if (node != null && node.size() != 0) { propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, jclass, schema); } else { propertyType = jclass.owner().ref(Object.class); } JFieldVar field = addAdditionalPropertiesField(jclass, propertyType); addGetter(jclass, field); addSetter(jclass, propertyType, field); return jclass; }
From source file:com.attribyte.essem.MGraphResponseGenerator.java
protected void parseGraph(JsonNode sourceParent, List<String> fields, EnumSet<Option> options, RateUnit rateUnit, ArrayNode targetGraph) { DateTimeFormatter parser = ISODateTimeFormat.basicDateTime(); SimpleDateFormat formatter = new SimpleDateFormat(DT_FORMAT); Map<MetricKey, List<ObjectNode>> outputGraphs = Maps.newHashMapWithExpectedSize(4); JsonNode hitsObj = sourceParent.get("hits"); if (hitsObj != null) { JsonNode hitsArr = hitsObj.get("hits"); if (hitsArr != null) { for (JsonNode hitObj : hitsArr) { JsonNode fieldsObj = hitObj.get("fields"); if (fieldsObj != null) { ObjectNode outObj = mapper.createObjectNode(); DateTime timestamp = parser.parseDateTime(getStringField(fieldsObj, "ts")); outObj.put("timestamp", timestamp.getMillis()); outObj.put("date", formatter.format(timestamp.getMillis())); MetricKey key = new MetricKey(getStringField(fieldsObj, "name"), getStringField(fieldsObj, "application"), getStringField(fieldsObj, "host"), getStringField(fieldsObj, "instance")); Iterator<Map.Entry<String, JsonNode>> fieldIter = fieldsObj.fields(); while (fieldIter.hasNext()) { Map.Entry<String, JsonNode> currField = fieldIter.next(); if (!graphIgnoreProperties.contains(currField.getKey())) { JsonNode currValueNode = currField.getValue(); if (currValueNode.isArray() && currValueNode.size() > 0) { setFieldValue(rateUnit, outObj, currField.getKey(), currValueNode.get(0)); } else if (!currValueNode.isArray()) { setFieldValue(rateUnit, outObj, currField.getKey(), currValueNode); }/*from ww w . j a v a 2 s . co m*/ } } List<ObjectNode> graph = outputGraphs.get(key); if (graph == null) { graph = Lists.newArrayListWithExpectedSize(1024); outputGraphs.put(key, graph); } graph.add(outObj); } } } } if (outputGraphs.size() == 1) { List<ObjectNode> graphNodes = outputGraphs.values().iterator().next(); for (ObjectNode outObj : graphNodes) { targetGraph.add(outObj); } } else { for (Map.Entry<MetricKey, List<ObjectNode>> graphEntry : outputGraphs.entrySet()) { MetricKey key = graphEntry.getKey(); ObjectNode outputGraphNode = targetGraph.addObject(); outputGraphNode.put("name", key.name); outputGraphNode.put("application", key.application); outputGraphNode.put("host", key.host); outputGraphNode.put("instance", key.instance); ArrayNode currOutputGraph = outputGraphNode.putArray("graph"); for (ObjectNode outObj : graphEntry.getValue()) { currOutputGraph.add(outObj); } } } }