List of usage examples for com.fasterxml.jackson.databind.node ArrayNode get
public JsonNode get(String paramString)
From source file:com.github.fge.jsonpatch.diff.JsonDiff.java
private static void generateArrayDiffs(final DiffProcessor processor, final JsonPointer pointer, final ArrayNode source, final ArrayNode target) { final int firstSize = source.size(); final int secondSize = target.size(); final int size = Math.min(firstSize, secondSize); /*/*w w w. j av a2s . c o m*/ * Source array is larger; in this case, elements are removed from the * target; the index of removal is always the original arrays's length. */ for (int index = size; index < firstSize; index++) processor.valueRemoved(pointer.append(size), source.get(index)); for (int index = 0; index < size; index++) generateDiffs(processor, pointer.append(index), source.get(index), target.get(index)); // Deal with the destination array being larger... for (int index = size; index < secondSize; index++) processor.valueAdded(pointer.append("-"), target.get(index)); }
From source file:com.msopentech.odatajclient.testservice.utils.Commons.java
public static InputStream getLinksAsJSON(final String entitySetName, final Map.Entry<String, Collection<String>> link) throws IOException { final ObjectNode links = new ObjectNode(JsonNodeFactory.instance); links.put(JSON_ODATAMETADATA_NAME, ODATA_METADATA_PREFIX + entitySetName + "/$links/" + link.getKey()); final ArrayNode uris = new ArrayNode(JsonNodeFactory.instance); for (String uri : link.getValue()) { final String absoluteURI; if (URI.create(uri).isAbsolute()) { absoluteURI = uri;//from w w w. j a v a 2 s . co m } else { absoluteURI = DEFAULT_SERVICE_URL + uri; } uris.add(new ObjectNode(JsonNodeFactory.instance).put("url", absoluteURI)); } if (uris.size() == 1) { links.setAll((ObjectNode) uris.get(0)); } else { links.set("value", uris); } return IOUtils.toInputStream(links.toString()); }
From source file:org.apache.olingo.fit.utils.Commons.java
public static InputStream getLinksAsJSON(final String entitySetName, final Map.Entry<String, Collection<String>> link) throws IOException { final ObjectNode links = new ObjectNode(JsonNodeFactory.instance); links.put(Constants.get(ConstantKey.JSON_ODATAMETADATA_NAME), Constants.get(ConstantKey.ODATA_METADATA_PREFIX) + entitySetName + "/$links/" + link.getKey()); final ArrayNode uris = new ArrayNode(JsonNodeFactory.instance); for (String uri : link.getValue()) { final String absoluteURI; if (URI.create(uri).isAbsolute()) { absoluteURI = uri;//from w w w . j a v a 2 s . com } else { absoluteURI = Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + uri; } uris.add(new ObjectNode(JsonNodeFactory.instance).put("url", absoluteURI)); } if (uris.size() == 1) { links.setAll((ObjectNode) uris.get(0)); } else { links.set("value", uris); } return IOUtils.toInputStream(links.toString(), Constants.ENCODING); }
From source file:net.opentsdb.ConfigReader.java
public static String[] load(final Config config, final String[] args) { InputStream is = null;// w ww . ja va2 s . c om String[] noConfigArgs = {}; try { is = ConfigReader.class.getClassLoader().getResourceAsStream(CONFIG_PATH); ArrayNode an = (ArrayNode) JSON.getMapper().readTree(is); final LinkedHashMap<String, ConfigurationItem> citems = new LinkedHashMap<String, ConfigurationItem>( an.size()); final HashMap<String, ConfigurationItem> clOptions = new HashMap<String, ConfigurationItem>(); for (int i = 0; i < an.size(); i++) { JsonNode jn = an.get(i); ConfigurationItem ci = JSON.getMapper().convertValue(jn, ConfigurationItem.class); ci.resolve(); if (ci.getCl() != null) { clOptions.put(ci.getCl(), ci); } if (ci.getValue() != null) { //config.overrideConfig(ci.getKey(), ci.getValue().toString()); ci.validate(); citems.put(ci.getKey(), ci); } //System.out.println(ci.dump()); } final ArgP argp = newArgP(); noConfigArgs = argp.parse(args); final Map<String, String> argpOptions = argp.getParsed(); if (!argpOptions.isEmpty()) { for (Map.Entry<String, String> entry : argpOptions.entrySet()) { ConfigurationItem argCi = clOptions.get(entry.getKey()); if (argCi != null) { argCi.setValueAsText(entry.getValue()); citems.put(argCi.getKey(), argCi); } } } // Write the configuration to an OpenTSDB config for (ConfigurationItem configItem : citems.values()) { config.overrideConfig(configItem.getKey(), configItem.getValueStr()); } return noConfigArgs; } catch (Exception ex) { throw new RuntimeException("Failed to load resource [" + CONFIG_PATH + "]", ex); } }
From source file:com.redhat.lightblue.util.JsonDoc.java
private static JsonNode modifyArrayNode(ArrayNode parentNode, JsonNode newValue, String last, Path p) { JsonNode oldValue;//from w w w .java 2 s.co m ArrayNode arr = (ArrayNode) parentNode; int index; try { index = Integer.valueOf(last); } catch (NumberFormatException e) { throw new IllegalArgumentException(UtilConstants.ERR_EXPECTED_ARRAY_INDEX + p); } int size = arr.size(); while (size < index) { arr.addNull(); size++; } if (index < 0) { index = size + index; } if (index < size && newValue != null) { oldValue = arr.get(index); arr.set(index, newValue); } else if (newValue == null) { oldValue = arr.get(index); arr.remove(index); } else { oldValue = null; arr.add(newValue); } return oldValue; }
From source file:org.jsfr.json.provider.JacksonProvider.java
@Override public Object resolve(ArrayNode array, int index) { return array.get(index); }
From source file:com.basistech.rosette.dm.json.plain.LanguageCodeJsonTest.java
@Test public void testRoundTrip() throws Exception { List<LanguageDetection.DetectionResult> detectionResults = Lists.newArrayList(); LanguageDetection.DetectionResult detectionResult = new LanguageDetection.DetectionResult.Builder( LanguageCode.KOREAN).encoding("uff-8").script(ISO15924.Hang).confidence(1.0).build(); detectionResults.add(detectionResult); LanguageDetection.Builder builder = new LanguageDetection.Builder(0, 100, detectionResults); LanguageDetection languageDetection = builder.build(); ObjectMapper mapper = objectMapper(); String json = mapper.writeValueAsString(languageDetection); // now read back as a tree. JsonNode tree = mapper.readTree(json); JsonNode resultsNode = tree.get("detectionResults"); ArrayNode resultArray = (ArrayNode) resultsNode; ObjectNode detectionNode = (ObjectNode) resultArray.get(0); assertEquals("kor", detectionNode.get("language").textValue()); assertEquals("Hang", detectionNode.get("script").textValue()); languageDetection = mapper.readValue(json, LanguageDetection.class); assertSame(LanguageCode.KOREAN, languageDetection.getDetectionResults().get(0).getLanguage()); }
From source file:lumbermill.internal.geospatial.GeoIPTest.java
@Ignore @Test//from w ww . j a va2 s . c o m public void test_geoip_all_fields() { GeoIP geoIP = GeoIP.Factory.create("client_ip", Optional.of("geoip"), Optional.of(new File("/tmp/GeoLite2-City.mmdb")), Optional.empty()); JsonEvent event = Codecs.TEXT_TO_JSON.from("Hello").put("client_ip", "37.139.156.40"); geoIP.decorate(event); JsonNode geoip = event.unsafe().get("geoip"); assertThat(geoip.get("country_code2").asText()).isEqualTo("SE"); assertThat(geoip.get("country_code3").asText()).isEqualTo("SE"); assertThat(geoip.get("timezone").asText()).isEqualTo("Europe/Stockholm"); assertThat(geoip.get("continent_code").asText()).isEqualTo("EU"); assertThat(geoip.get("continent_name").asText()).isEqualTo("Europe"); assertThat(geoip.get("country_name").asText()).isEqualTo("Sweden"); assertThat(geoip.get("city_name").asText()).isEqualTo("Sodra Sandby"); assertThat(geoip.get("longitude").asDouble()).isEqualTo(13.3333); assertThat(geoip.get("latitude").asDouble()).isEqualTo(55.7167); ArrayNode location = (ArrayNode) geoip.get("location"); assertThat(location.get(0).asDouble()).isEqualTo(13.3333); assertThat(location.get(1).asDouble()).isEqualTo(55.7167); System.out.println(event); }
From source file:lumbermill.internal.geospatial.GeoIPTest.java
@Ignore @Test//from w ww.jav a2 s . c o m public void test_geoip_some_fields() { GeoIP geoIP = GeoIP.Factory.create("client_ip", Optional.of("geoip2"), Optional.of(new File("/tmp/GeoLite2-City.mmdb")), Optional.of(asList("timezone", "location"))); JsonEvent event = Codecs.TEXT_TO_JSON.from("Hello").put("client_ip", "37.139.156.40"); geoIP.decorate(event); JsonNode geoip = event.unsafe().get("geoip2"); assertThat(geoip.get("country_code2")).isNull(); assertThat(geoip.get("country_code3")).isNull(); assertThat(geoip.get("timezone").asText()).isEqualTo("Europe/Stockholm"); assertThat(geoip.get("continent_code")).isNull(); assertThat(geoip.get("continent_name")).isNull(); assertThat(geoip.get("country_name")).isNull(); assertThat(geoip.get("city_name")).isNull(); assertThat(geoip.get("longitude")).isNull(); assertThat(geoip.get("latitude")).isNull(); ArrayNode location = (ArrayNode) geoip.get("location"); assertThat(location.get(0).asDouble()).isEqualTo(13.3333); assertThat(location.get(1).asDouble()).isEqualTo(55.7167); System.out.println(event); }
From source file:org.springframework.social.vkontakte.api.impl.AbstractVKontakteOperations.java
protected <T> VKArray<T> deserializeArray(VKGenericResponse response, Class<T> itemClass) { checkForError(response);// w ww .j ava2 s .co m Assert.isTrue(response.getResponse().isArray()); ArrayNode items = (ArrayNode) response.getResponse(); int count = items.get(0).asInt(); List<T> elements = new ArrayList<T>(); for (int i = 1; i < items.size(); i++) { elements.add(objectMapper.convertValue(items.get(i), itemClass)); } return new VKArray<T>(count, elements); }