List of usage examples for com.fasterxml.jackson.databind.node MissingNode getInstance
public static MissingNode getInstance()
From source file:com.squarespace.template.JsonUtils.java
/** * Attempt to decode the input as JSON. Returns the {@link JsonNode} if * the decode is successful.//www .ja va 2 s.c o m * * If the decode fails and the {@code failQuietly} flag is true, returns a * {@link MissingNode}. Otherwise an {@link IllegalArgumentException} will * be thrown. */ public static JsonNode decode(String input, boolean failQuietly) { try { return MAPPER.readTree(input); } catch (IOException e) { if (failQuietly) { return MissingNode.getInstance(); } throw new IllegalArgumentException("Unable to decode JSON", e); } }
From source file:com.spotify.hamcrest.jackson.IsJsonMissingTest.java
@Test public void testType() throws Exception { final Matcher<JsonNode> sut = jsonMissing(); assertThat(MissingNode.getInstance(), is(sut)); }
From source file:com.spotify.hamcrest.jackson.IsJsonMissingTest.java
@Test public void testMatch() throws Exception { final Matcher<JsonNode> sut = jsonMissing(); assertThat(MissingNode.getInstance(), is(sut)); }
From source file:org.eel.kitchen.jsonschema.ref.IdFragment.java
@Override public JsonNode resolve(final JsonNode node) { /*//www. j a v a2 s . c o m * Non object instances are not worth being considered */ if (!node.isObject()) return MissingNode.getInstance(); /* * If an id node exists and is a text node, see if that node's value * (minus the initial #) matches our id, if yes we have a match */ final JsonNode idNode = node.path("id"); if (idNode.isTextual()) { final String s = idNode.textValue(); if (asString.equals(s)) return node; } /* * Otherwise, go on with children. As this is an object, * JsonNode will cycle through the values, which is what we want. */ JsonNode ret; for (final JsonNode subNode : node) { ret = resolve(subNode); if (!ret.isMissingNode()) return ret; } return MissingNode.getInstance(); }
From source file:com.github.fge.jsonpatch.RemoveOperation.java
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (path.isEmpty()) return MissingNode.getInstance(); if (path.path(node).isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchPath")); final JsonNode ret = node.deepCopy(); final JsonNode parentNode = path.parent().get(ret); final String raw = Iterables.getLast(path).getToken().getRaw(); if (parentNode.isObject()) ((ObjectNode) parentNode).remove(raw); else//from ww w . j a va 2 s . c om ((ArrayNode) parentNode).remove(Integer.parseInt(raw)); return ret; }
From source file:com.qubole.presto.kinesis.decoder.json.JsonKinesisRowDecoder.java
private static JsonNode locateNode(JsonNode tree, KinesisColumnHandle columnHandle) { String mapping = columnHandle.getMapping(); checkState(mapping != null, "No mapping for %s", columnHandle.getName()); JsonNode currentNode = tree;//from w ww. j a va2s . co m for (String pathElement : Splitter.on('/').omitEmptyStrings().split(mapping)) { if (!currentNode.has(pathElement)) { return MissingNode.getInstance(); } currentNode = currentNode.path(pathElement); } return currentNode; }
From source file:com.facebook.presto.kinesis.decoder.json.JsonKinesisRowDecoder.java
private static JsonNode locateNode(JsonNode tree, KinesisColumnHandle columnHandle) { String mapping = columnHandle.getMapping(); checkState(mapping != null, "No mapping for %s", columnHandle.getName()); JsonNode currentNode = tree;/*from ww w . java 2 s .c o m*/ for (String pathElement : Splitter.on('/').omitEmptyStrings().split(mapping)) { String baseName = pathElement; int index = -1; if (pathElement.contains("[")) { String[] splits = pathElement.split("[\\[\\]]"); if (splits.length == 2) { if (splits[1].matches("[0-9]+")) { index = Integer.parseInt(splits[1]); } } baseName = splits[0]; } if (!currentNode.has(baseName)) { return MissingNode.getInstance(); } currentNode = currentNode.path(baseName); if (index >= 0 && currentNode.isArray()) { currentNode = currentNode.get(index); // From Jackson docs: If index is less than 0, or equal-or-greater than node.size(), null is returned; // no exception is thrown for any index. if (currentNode == null) { return MissingNode.getInstance(); } else if (currentNode.isMissingNode() || currentNode.isNull()) { return currentNode; // don't continue } } } return currentNode; }
From source file:org.eel.kitchen.jsonschema.ref.JsonFragmentTest.java
@DataProvider public Iterator<Object[]> getData() { String input, errmsg;/*www .j a v a 2 s .c om*/ JsonNode expected; final ImmutableSet.Builder<Object[]> builder = new ImmutableSet.Builder<Object[]>(); /* * Empty fragments */ expected = schema; input = ""; errmsg = "empty fragment does not resolve to self"; builder.add(new Object[] { input, expected, errmsg }); /* * JSON Pointers -- existing and non existing */ errmsg = "existing JSON Pointer lookup failed"; input = "/properties"; expected = schema.get("properties"); builder.add(new Object[] { input, expected, errmsg }); input = "/properties/additionalProperties/type/1"; expected = schema.get("properties").get("additionalProperties").get("type").get(1); builder.add(new Object[] { input, expected, errmsg }); errmsg = "non existing JSON Pointer lookup failed"; expected = MissingNode.getInstance(); input = "/foobar"; // an ID exists, but not a pointer builder.add(new Object[] { input, expected, errmsg }); /* * IDs -- existing and non existing */ input = "foobar"; expected = schema.get("properties").get("patternProperties"); errmsg = "id lookup failed (non # prefixed)"; builder.add(new Object[] { input, expected, errmsg }); input = "moo"; expected = MissingNode.getInstance(); errmsg = "non existing id lookup failed"; builder.add(new Object[] { input, expected, errmsg }); return builder.build().iterator(); }
From source file:io.wcm.caravan.pipeline.impl.operators.ExtractOperator.java
@Override public Subscriber<? super JsonPipelineOutput> call(Subscriber<? super JsonPipelineOutput> subscriber) { return new Subscriber<JsonPipelineOutput>() { @Override/* ww w. ja v a 2 s . c o m*/ public void onCompleted() { subscriber.onCompleted(); } @Override public void onError(Throwable e) { Exceptions.throwIfFatal(e); subscriber.onError(e); } @Override public void onNext(JsonPipelineOutput output) { ArrayNode result = new JsonPathSelector(jsonPath).call(output.getPayload()); JsonNode extractedPayload = result.size() == 0 ? MissingNode.getInstance() : result.get(0); if (isNotBlank(targetProperty)) { extractedPayload = JacksonFunctions.wrapInObject(targetProperty, extractedPayload); } JsonPipelineOutput extractedOutput = output.withPayload(extractedPayload); subscriber.onNext(extractedOutput); } }; }
From source file:org.springframework.security.jackson2.UserDeserializer.java
private JsonNode readJsonNode(JsonNode jsonNode, String field) { return jsonNode.has(field) ? jsonNode.get(field) : MissingNode.getInstance(); }