Class JsonUtils


  • public class JsonUtils
    extends Object
    Author:
    Andrea Di Cesare <andrea@softinstigate.com>
    • Constructor Detail

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • unescapeKeys

        public static org.bson.BsonValue unescapeKeys​(org.bson.BsonValue json)
        replaces the underscore prefixed keys (eg _$exists) with the corresponding key (eg $exists) and the dot (.) in property names. This is needed because MongoDB does not allow to store keys that starts with $ and with dots in it
        Parameters:
        json -
        Returns:
        the json object where the underscore prefixed keys are replaced with the corresponding keys
      • escapeKeys

        public static org.bson.BsonValue escapeKeys​(org.bson.BsonValue json,
                                                    boolean escapeDots)
        replaces the dollar prefixed keys (eg $exists) with the corresponding underscore prefixed key (eg _$exists). Also replaces dots if escapeDots is true. This is needed because MongoDB does not allow to store keys that starts with $ and that contains dots.
        Parameters:
        json -
        escapeDots -
        Returns:
        the json object where the underscore prefixed keys are replaced with the corresponding keys
      • getPropsFromPath

        public static List<Optional<org.bson.BsonValue>> getPropsFromPath​(org.bson.BsonValue root,
                                                                          String path)
                                                                   throws IllegalArgumentException
        Parameters:
        root - the Bson to extract properties from
        path - the path of the properties to extract
        Returns:
        the List of Optional<Object>s extracted from root ojbect and identified by the path or null if path does not exist
        Throws:
        IllegalArgumentException
        See Also:
        form code examples
      • isAncestorPath

        public static boolean isAncestorPath​(String left,
                                             String right)
        Parameters:
        left - the json path expression
        right - the json path expression
        Returns:
        true if the left json path is an acestor of the right path, i.e. left path selects a values set that includes the one selected by the right path examples: ($, $.a) -> true, ($.a, $.b) -> false, ($.*, $.a) -> true, ($.a.[*].c, $.a.0.c) -> true, ($.a.[*], $.a.b) -> false
      • checkType

        public static boolean checkType​(Optional<org.bson.BsonValue> o,
                                        String type)
        Parameters:
        o -
        type -
        Returns:
      • minify

        public static String minify​(String jsonString)
        Parameters:
        jsonString -
        Returns:
        minified json string
      • parse

        public static org.bson.BsonValue parse​(String json)
                                        throws org.bson.json.JsonParseException
        Parameters:
        json -
        Returns:
        either a BsonDocument or a BsonArray from the json string
        Throws:
        org.bson.json.JsonParseException
      • toJson

        public static String toJson​(org.bson.BsonValue bson)
        Parameters:
        bson - either a BsonDocument or a BsonArray
        Returns:
        the minified string representation of the bson value
        Throws:
        IllegalArgumentException - if bson is not a BsonDocument or a BsonArray
      • toJson

        public static String toJson​(org.bson.BsonValue bson,
                                    org.bson.json.JsonMode mode)
        Parameters:
        bson - either a BsonDocument or a BsonArray
        mode -
        Returns:
        the minified string representation of the bson value
        Throws:
        IllegalArgumentException - if bson is not a BsonDocument or a BsonArray
      • getIdAsString

        public static String getIdAsString​(org.bson.BsonValue id,
                                           boolean quote)
        Parameters:
        id -
        quote -
        Returns:
        the String representation of the id
      • toBsonDocument

        public static org.bson.BsonDocument toBsonDocument​(Map<String,​Object> map)
        Parameters:
        map -
        Returns:
      • isUpdateOperator

        public static boolean isUpdateOperator​(String key)
        Parameters:
        key -
        Returns:
        true if key is an update operator
      • containsUpdateOperators

        public static boolean containsUpdateOperators​(org.bson.BsonValue json)
        Parameters:
        json -
        Returns:
        true if json contains update operators
      • containsUpdateOperators

        public static boolean containsUpdateOperators​(org.bson.BsonValue json,
                                                      boolean ignoreCurrentDate)
        Parameters:
        ignoreCurrentDate - true to ignore $currentDate
        json -
        Returns:
        true if json contains update operators
      • unflatten

        public static org.bson.BsonValue unflatten​(org.bson.BsonValue json)
                                            throws IllegalArgumentException
        Parameters:
        json -
        Returns:
        the unflatten json replacing dot notatation fkeys with nested objects: from {"a.b":2} to {"a":{"b":2}}
        Throws:
        IllegalArgumentException
      • flatten

        public static org.bson.BsonDocument flatten​(org.bson.BsonDocument json,
                                                    boolean ignoreUpdateOperators)
        Parameters:
        json -
        ignoreUpdateOperators - true to not flatten update operators
        Returns:
        the flatten json objects using dot notation from {"a":{"b":1}, {"$currentDate": {"my.field": true}} to {"a.b":1, {"$currentDate": {"my.field": true}}