Class JsonUtils


  • public class JsonUtils
    extends Object
    Json utilities.
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void bind​(com.fasterxml.jackson.databind.JsonNode node, Object object)
      Binds the given JsonNode to the provided object.
      static void bind​(com.fasterxml.jackson.databind.JsonNode node, Map<String,​com.fasterxml.jackson.databind.JsonNode> values)
      Binds values in the map to the given JsonNode.
      static com.fasterxml.jackson.databind.JsonNode deserialize​(String value)
      Deserializes the string to a JsonNode.
      static com.fasterxml.jackson.databind.JsonNode dotPath​(com.fasterxml.jackson.databind.JsonNode node, String dotPath)
      Return the found JsonNode after following the dot notation path.
      static boolean matchesProjection​(String field, Collection<String> fields)
      Tests whether or not the given field is part of the field projection denoted by the given fields.
      static com.fasterxml.jackson.databind.JsonNode project​(com.fasterxml.jackson.databind.JsonNode node, Collection<String> fields)
      Projects the given fields of the given node onto a new node.
      static String serialize​(Object document)
      Serializes the document to a string.
      static com.fasterxml.jackson.databind.JsonNode toJsonNode​(Object value)
      Converts an object to a JsonNode.
    • Constructor Detail

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • bind

        public static void bind​(com.fasterxml.jackson.databind.JsonNode node,
                                Map<String,​com.fasterxml.jackson.databind.JsonNode> values)
        Binds values in the map to the given JsonNode. The key in the map is the name of the variable in the node. The value in the map replaces the ${VARIABLE_NAME} in the node.
        Parameters:
        node - the node to modify
        values - the values to bind to the node
      • bind

        public static void bind​(com.fasterxml.jackson.databind.JsonNode node,
                                Object object)
        Binds the given JsonNode to the provided object.
        Parameters:
        node - the values to bind
        object - values are bound to this object
      • deserialize

        public static com.fasterxml.jackson.databind.JsonNode deserialize​(String value)
        Deserializes the string to a JsonNode.
        Parameters:
        value - the string to deserialize
        Returns:
        the deserialized node
      • dotPath

        public static com.fasterxml.jackson.databind.JsonNode dotPath​(com.fasterxml.jackson.databind.JsonNode node,
                                                                      String dotPath)
        Return the found JsonNode after following the dot notation path.
        Parameters:
        node - the subect
        dotPath - the path
        Returns:
        the found node or missing node if none found
      • serialize

        public static String serialize​(Object document)
        Serializes the document to a string.
        Parameters:
        document - the document to serialize.
        Returns:
        the serialized string
      • toJsonNode

        public static com.fasterxml.jackson.databind.JsonNode toJsonNode​(Object value)
        Converts an object to a JsonNode.
        Parameters:
        value - the object to convert
        Returns:
        the resulting JsonNode
      • project

        public static com.fasterxml.jackson.databind.JsonNode project​(com.fasterxml.jackson.databind.JsonNode node,
                                                                      Collection<String> fields)
        Projects the given fields of the given node onto a new node. An empty fields projects the entire document.

        Note: projection follows MongoDB's projection pattern (https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/). The projection is only applied to child nodes of objects. Arrays or nested arrays are iterated over and projection picks up again when objects are found. Primitive values that are not part of a terminal projection are removed.

        Parameters:
        node - the subject of the projection
        fields - dot-notated fields denoting what should be projected
        Returns:
        the projected node
      • matchesProjection

        public static boolean matchesProjection​(String field,
                                                Collection<String> fields)
        Tests whether or not the given field is part of the field projection denoted by the given fields.

        Note: projection follows MongoDB's projection pattern (https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/).

        Parameters:
        field - the field to test
        fields - the projection
        Returns:
        if the field is part of the projection