com.fasterxml.jackson.databind.node.MissingNode Java Examples

The following examples show how to use com.fasterxml.jackson.databind.node.MissingNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BidRequest.java    From XRTB with Apache License 2.0 7 votes vote down vote up
/**
 * Returns the asset id in the bid request of the requested index
 * 
 * @param type String. The type of asset
 * @param subtype String. sub type of the asset
 * @param value int. The integer representation of the entity.
 * @return int. Returns the index in the asset object. If not found, returns
 *         -1
 */
public int getNativeAdAssetIndex(String type, String subtype, int value) {
	JsonNode nat = rootNode.path("imp");
	if (nat == null || nat.isArray() == false)
		return -1;
	ArrayNode array = (ArrayNode) nat;
	JsonNode node = array.get(0).path("native").path("assets");
	ArrayNode nodes = (ArrayNode) node;
	for (int i = 0; i < nodes.size(); i++) {
		JsonNode asset = nodes.get(i);
		JsonNode n = asset.path(type);
		JsonNode id = asset.path("id");
		if (n instanceof MissingNode == false) {
			if (subtype != null) {
				n = n.path(subtype);
				if (n != null) {
					if (n.intValue() == value)
						return id.intValue();
				}
			} else {
				return id.intValue();
			}
		}
	}
	return -1;
}
 
Example #2
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Allocates json data model tree on json pointer path with specific leaf type.
 *
 * @param ptr      json pointer to allocate
 * @param leaftype type of leaf node
 * @return json data model tree
 * @throws WorkflowException workflow exception
 */
private JsonDataModelTree alloc(JsonPointer ptr, Nodetype leaftype) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }

    switch (leaftype) {
        case MAP:
            alloc(root, ptr, JsonNodeType.OBJECT);
            break;
        case ARRAY:
            alloc(root, ptr, JsonNodeType.ARRAY);
            break;
        default:
            throw new WorkflowException("Not supported leaftype(" + leaftype + ")");
    }
    return this;
}
 
Example #3
Source File: DefaultWorkplaceDescription.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Creating workplace description from json tree.
 * @param root root node for workplace description
 * @return workplace description
 * @throws WorkflowException workflow exception
 */
public static DefaultWorkplaceDescription valueOf(JsonNode root) throws WorkflowException {

    JsonNode node = root.at(ptr(WP_NAME));
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("invalid workplace name for " + root);
    }

    Builder builder = builder()
        .name(node.asText());

    node = root.at(ptr(WP_DATA));
    if (node != null && !(node instanceof MissingNode)) {
        if (!(node instanceof ObjectNode) && !(node instanceof ArrayNode)) {
            throw new WorkflowException("invalid workplace data for " + root);
        }
        builder.data(node);
    }

    return builder.build();
}
 
Example #4
Source File: JsonNodeValue.java    From mybatis-jackson with MIT License 6 votes vote down vote up
/**
 * Return COPY of JSON node value (will parse node from string at first call).
 * WARNING if object constructed with invalid JSON string, exception will be thrown.
 *
 * @return Copy of valid JsonNode or MissingNode if no data.
 * @throws RuntimeException On JSON parsing errors.
 */
public JsonNode get() throws RuntimeException {
    if (!isPresent()) {
        return MissingNode.getInstance();
    }

    if (value == null) {
        synchronized (this) {
            if (value == null) {
                try {
                    value = ReaderWriter.readTree(source);
                } catch (Exception ex) {
                    throw new RuntimeException("Can not parse JSON string. " + ex.getMessage(), ex);
                }
            }
        }
    }
    return value.deepCopy();
}
 
Example #5
Source File: RemoveIfExistsOperation.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
JsonNode apply(final JsonNode node) {
    if (path.toString().isEmpty()) {
        return MissingNode.getInstance();
    }

    final JsonNode found = node.at(path);
    if (found.isMissingNode()) {
        return node;
    }

    final JsonNode parentNode = node.at(path.head());
    final String raw = path.last().getMatchingProperty();
    if (parentNode.isObject()) {
        ((ObjectNode) parentNode).remove(raw);
    } else if (parentNode.isArray()) {
        ((ArrayNode) parentNode).remove(Integer.parseInt(raw));
    }
    return node;
}
 
Example #6
Source File: RemoveOperation.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
JsonNode apply(final JsonNode node) {
    if (path.toString().isEmpty()) {
        return MissingNode.getInstance();
    }
    ensureExistence(node);

    final JsonNode parentNode = node.at(path.head());
    final String raw = path.last().getMatchingProperty();
    if (parentNode.isObject()) {
        ((ObjectNode) parentNode).remove(raw);
    } else {
        ((ArrayNode) parentNode).remove(Integer.parseInt(raw));
    }
    return node;
}
 
Example #7
Source File: Watcher.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a forked {@link Watcher} based on an existing {@link JsonNode}-watching {@link Watcher}.
 *
 * @param jsonPointer a <a href="https://tools.ietf.org/html/rfc6901">JSON pointer</a> that is encoded
 *
 * @return A new child {@link Watcher}, whose transformation is a
 *         <a href="https://tools.ietf.org/html/rfc6901">JSON pointer</a> query.
 */
static Watcher<JsonNode> atJsonPointer(Watcher<JsonNode> watcher, String jsonPointer) {
    requireNonNull(watcher, "watcher");
    requireNonNull(jsonPointer, "jsonPointer");
    return watcher.newChild(new Function<JsonNode, JsonNode>() {
        @Override
        public JsonNode apply(JsonNode node) {
            if (node == null) {
                return MissingNode.getInstance();
            } else {
                return node.at(jsonPointer);
            }
        }

        @Override
        public String toString() {
            return "JSON pointer " + jsonPointer;
        }
    });
}
 
Example #8
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void attach(String path, DataModelTree tree) throws WorkflowException {

    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }

    JsonPointer ptr = JsonPointer.compile(path);

    if (!(tree instanceof JsonDataModelTree)) {
        throw new WorkflowException("Invalid subTree(" + tree + ")");
    }
    JsonNode attachingNode = ((JsonDataModelTree) tree).root();

    attach(ptr, attachingNode);
}
 
Example #9
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets json node on specific json pointer as ArrayNode.
 *
 * @param ptr json pointer
 * @return ArrayNode type json node on specific json pointer.
 * @throws WorkflowException workflow exception
 */
public ArrayNode arrayAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    if (node instanceof MissingNode) {
        return null;
    }
    if (!(node instanceof ArrayNode)) {
        throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
    }
    return (ArrayNode) node;
}
 
Example #10
Source File: ConfluenceAdapter.java    From ods-provisioning-app with Apache License 2.0 5 votes vote down vote up
private boolean checkGroupExists(String group) throws IOException {

    logger.info("checking if group '{}' exists!", group);

    String url =
        String.format(CONFLUENCE_API_GROUP_PATTERN, confluenceUri, confluenceApiPath, group);

    String response = null;
    try {
      response = getRestClient().execute(httpGet().url(url).returnType(String.class));
      Assert.notNull(response, "Response is null for '" + group + "'");
    } catch (HttpException e) {
      if (HttpStatus.NOT_FOUND.value() == e.getResponseCode()) {
        logger.debug("Group '{}' was not found in {}!", group, ADAPTER_NAME, e);
        return false;
      } else {
        logger.warn("Unexpected method trying to get group '{}'!", group, e);
        throw e;
      }
    }

    JsonNode json = new ObjectMapper().readTree(response);

    JsonNode haveGroup = json.at("/name");

    if (MissingNode.class.isInstance(haveGroup)) {
      logger.warn("Missing node for '{}'!", json);
      return false;
    }

    return group.equalsIgnoreCase(haveGroup.asText());
  }
 
Example #11
Source File: PatchProcessorTest.java    From data-highway with Apache License 2.0 5 votes vote down vote up
@Test
public void removeExistingRoot() throws PatchApplicationException {
  store.put(DOCUMENT_ID, jsonNode1);
  PatchSet patchSet = patchSet(REMOVE, "", null);

  JsonNode result = underTest.processPatch(patchSet);

  assertThat(result, is(MissingNode.getInstance()));
  assertThat(store.get(DOCUMENT_ID), is(nullValue()));
}
 
Example #12
Source File: AbstractResourceDiff.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected JsonNode lookupPath(JsonNode source, String path) {
    JsonNode s = source;
    for (String component : path.substring(1).split("/")) {
        if (s.isArray()) {
            try {
                s = s.path(Integer.parseInt(component));
            } catch (NumberFormatException e) {
                return MissingNode.getInstance();
            }
        } else {
            s = s.path(component);
        }
    }
    return s;
}
 
Example #13
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public JsonDataModelTree alloc(String path, Nodetype leaftype) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }

    JsonPointer ptr = JsonPointer.compile(path);
    return alloc(ptr, leaftype);
}
 
Example #14
Source File: TreeNodeTypeHandler.java    From mybatis-jackson with MIT License 5 votes vote down vote up
private TreeNode fromString(String source) {
    if (source == null || source.isEmpty()) {
        // This is where we replace null result with empty node
        return MissingNode.getInstance();
    } else {
        // I really hope that source will be valid JSON string  (^_^)
        return new TreeNodeLazyWrapper(source);
    }
}
 
Example #15
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets integer on specific json pointer.
 *
 * @param ptr json pointer
 * @return integer on specific json pointer
 * @throws WorkflowException workflow exception
 */
public Integer intAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    if (node instanceof MissingNode) {
        return null;
    }
    if (!(node instanceof NumericNode)) {
        throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
    }
    return ((NumericNode) node).asInt();
}
 
Example #16
Source File: JsonUtils.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to decode the input as JSON. Returns the {@link JsonNode} if
 * the decode is successful.
 *
 * 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);
  }
}
 
Example #17
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets text on specific json pointer.
 *
 * @param ptr json pointer
 * @return text on specific json pointer
 * @throws WorkflowException workflow exception
 */
public String textAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    if (node instanceof MissingNode) {
        return null;
    }
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
    }
    return ((TextNode) node).asText();
}
 
Example #18
Source File: JsonKinesisRowDecoder.java    From presto-kinesis with Apache License 2.0 5 votes vote down vote up
private static JsonNode locateNode(JsonNode tree, KinesisColumnHandle columnHandle)
{
    String mapping = columnHandle.getMapping();
    checkState(mapping != null, "No mapping for %s", columnHandle.getName());

    JsonNode currentNode = tree;
    for (String pathElement : Splitter.on('/').omitEmptyStrings().split(mapping)) {
        if (!currentNode.has(pathElement)) {
            return MissingNode.getInstance();
        }
        currentNode = currentNode.path(pathElement);
    }
    return currentNode;
}
 
Example #19
Source File: JsonNodeClaimTest.java    From java-jwt with MIT License 5 votes vote down vote up
@Test
public void shouldReturnBaseClaimWhenParsingMissingNode() throws Exception {
    JsonNode value = MissingNode.getInstance();
    Claim claim = claimFromNode(value);

    assertThat(claim, is(notNullValue()));
    assertThat(claim, is(instanceOf(NullClaim.class)));
    assertThat(claim.isNull(), is(true));
}
 
Example #20
Source File: BidRequest.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Given a bid request, and a dotted string, return the value.
 * @param br BidRequest. The bid request to query.
 * @param what String. What json value to return (as a string.
 * @return String. The value as a string.
 */
String findValue(BidRequest br, String what) {
	Object node = null;
	if ((node = br.interrogate(what)) != null) {
		if (node instanceof MissingNode == false) {
			JsonNode n = (JsonNode) node;
			return n.asText();
		}
	}
	return null;
}
 
Example #21
Source File: JsonDataModelInjector.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Inhales integer data model on the filed of work-let.
 *
 * @param worklet work-let
 * @param context workflow context
 * @param field   the field of work-let
 * @param model   integer data model for the field
 * @throws WorkflowException workflow exception
 */
private static void inhaleInteger(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
        throws WorkflowException {
    if (!(Objects.equals(field.getType(), Integer.class))) {
        throw new WorkflowException("Target field (" + field + ") is not Integer");
    }

    Integer number;
    try {
        field.setAccessible(true);
        number = (Integer) field.get(worklet);
    } catch (IllegalAccessException e) {
        throw new WorkflowException(e);
    }

    if (Objects.isNull(number)) {
        return;
    }

    JsonDataModelTree tree = (JsonDataModelTree) context.data();
    JsonNode jsonNode = tree.nodeAt(model.path());

    if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
        tree.setAt(model.path(), number);
    } else if (!(jsonNode instanceof IntNode)) {
        throw new WorkflowException("Invalid integer data model on (" + model.path() + ")");
    } else {
        tree.remove(model.path());
        tree.setAt(model.path(), number);
    }
}
 
Example #22
Source File: DefaultWorkflowDescription.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creating workflow description from json tree.
 * @param root root node for workflow description
 * @return workflow description
 * @throws WorkflowException workflow exception
 */
public static DefaultWorkflowDescription valueOf(JsonNode root) throws WorkflowException {

    JsonNode node = root.at(ptr(WF_WORKPLACE));
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("invalid workflow workplace for " + root);
    }
    String wfWorkplaceName = node.asText();

    node = root.at(ptr(WF_ID));
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("invalid workflow id for " + root);
    }
    URI wfId = URI.create(node.asText());

    node = root.at(ptr(WF_DATA));
    if (node instanceof MissingNode) {
        throw new WorkflowException("invalid workflow data for " + root);
    }
    JsonNode wfData = node;

    return builder()
            .workplaceName(wfWorkplaceName)
            .id(wfId)
            .data(wfData)
            .build();
}
 
Example #23
Source File: DefaultRpcDescription.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creating workflow RPC description from json tree.
 * @param root root node for workflow RPC description
 * @return workflow RPC description
 * @throws WorkflowException workflow exception
 */
public static DefaultRpcDescription valueOf(JsonNode root) throws WorkflowException {

    JsonNode node = root.at(RPC_OP_PTR);
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("invalid RPC operation for " + root);
    }
    String rpcOp = node.asText();

    node = root.at(RPC_PARAMS_PTR);
    if (node instanceof MissingNode) {
        throw new WorkflowException("invalid RPC parameters for " + root);
    }
    JsonNode rpcParams = node;

    node = root.at(RPC_ID_PTR);
    if (!(node instanceof TextNode)) {
        throw new WorkflowException("invalid RPC invocation ID for " + root);
    }
    String rpcId = node.asText();


    return builder()
            .setOp(rpcOp)
            .setParams(rpcParams)
            .setId(rpcId)
            .build();
}
 
Example #24
Source File: BidRequest.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Handle any rtb4free extensions - like the specialized geo
 */
void handleRtb4FreeExtensions() {
	/**
	 * Now deal with RTB4FREE Extensions
	 */
	if (lat == null || lon == null)
		return;

	if (database.get("device.ua") instanceof MissingNode == false) {
		if (Configuration.getInstance().geoTagger != null)
			geoExtension = Configuration.getInstance().geoTagger.getSolution(lat, lon);
	}
}
 
Example #25
Source File: JsonDataModelInjector.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Inhales boolean data model on the filed of work-let.
 *
 * @param worklet work-let
 * @param context workflow context
 * @param field   the field of work-let
 * @param model   boolean data model for the field
 * @throws WorkflowException workflow exception
 */
private static void inhaleBoolean(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
        throws WorkflowException {

    if (!(Objects.equals(field.getType(), Boolean.class))) {
        throw new WorkflowException("Target field (" + field + ") is not Boolean");
    }

    Boolean bool;
    try {
        field.setAccessible(true);
        bool = (Boolean) field.get(worklet);
    } catch (IllegalAccessException e) {
        throw new WorkflowException(e);
    }

    if (Objects.isNull(bool)) {
        return;
    }

    JsonDataModelTree tree = (JsonDataModelTree) context.data();
    JsonNode jsonNode = tree.nodeAt(model.path());

    if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
        tree.setAt(model.path(), bool);
    } else if (!(jsonNode instanceof BooleanNode)) {
        throw new WorkflowException("Invalid boolean data model on (" + model.path() + ")");
    } else {
        tree.remove(model.path());
        tree.setAt(model.path(), bool);
    }
}
 
Example #26
Source File: Impression.java    From XRTB with Apache License 2.0 5 votes vote down vote up
public Object getNode(String what) {
	Object o = database.get(what);
	if (o == null || o instanceof MissingNode) {
		return null;
	}
	return o;
}
 
Example #27
Source File: Appnexus.java    From XRTB with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure the Appnexus keys are available on the creative
 * @param creat Creative. The creative in question.
 * @param errorString StringBuilder. The error handling string. Add your error here if not null.
 * @returns boolean. Returns true if the Exchange and creative are compatible.
 */
@Override
public boolean checkNonStandard(Creative c, StringBuilder sb) {
	if (c.extensions == null || c.extensions.get("appnexus_crid") == null) {
		if (sb != null) 
			sb.append("Creative is not Appnexus compatible");
		return false;
	}

	///////////////////////////
	//
	// Check for seat id
	//
	Object obj = interrogate("wseat");
	if (obj instanceof MissingNode) {
		if (sb != null) {
			sb.append("appnexus seat missing");
			return false;
		}
	}
	ArrayNode list = (ArrayNode) obj;
	boolean hasSeat = false;
	for (int i = 0; i < list.size(); i++) {
		JsonNode nx = list.get(i);
		if (nx.asText().equals(Appnexus.seatId)) {
			return true;
		}
	}
	if (sb != null)
		sb.append("Not our seat");
	return false;
}
 
Example #28
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets json node on specific json pointer as ObjectNode.
 *
 * @param ptr json pointer
 * @return ObjectNode type json node on specific json pointer.
 * @throws WorkflowException workflow exception
 */
public ObjectNode objectAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    if (node instanceof MissingNode) {
        return null;
    }
    if (!(node instanceof ObjectNode)) {
        throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
    }
    return (ObjectNode) node;
}
 
Example #29
Source File: JsonDataModelTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets json node on specific json pointer.
 *
 * @param ptr json pointer
 * @return json node on specific json pointer.
 * @throws WorkflowException workflow exception
 */
public JsonNode nodeAt(JsonPointer ptr) throws WorkflowException {
    if (root == null || root instanceof MissingNode) {
        throw new WorkflowException("Invalid root node");
    }
    JsonNode node = root.at(ptr);
    return node;
}
 
Example #30
Source File: JsonDataModelInjector.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Inhales text data model on the filed of work-let.
 *
 * @param worklet work-let
 * @param context workflow context
 * @param field   the field of work-let
 * @param model   text data model for the field
 * @throws WorkflowException workflow exception
 */
private static void inhaleText(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
        throws WorkflowException {

    if (!(Objects.equals(field.getType(), String.class))) {
        throw new WorkflowException("Target field (" + field + ") is not String");
    }

    String text;
    try {
        field.setAccessible(true);
        text = (String) field.get(worklet);
    } catch (IllegalAccessException e) {
        throw new WorkflowException(e);
    }

    if (Objects.isNull(text)) {
        return;
    }

    JsonDataModelTree tree = (JsonDataModelTree) context.data();
    JsonNode jsonNode = tree.nodeAt(model.path());

    if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
        tree.setAt(model.path(), text);
    } else if (!(jsonNode instanceof TextNode)) {
        throw new WorkflowException("Invalid text data model on (" + model.path() + ")");
    } else {
        tree.remove(model.path());
        tree.setAt(model.path(), text);
    }
}