Class BsonUtils

java.lang.Object
org.restheart.utils.BsonUtils
Direct Known Subclasses:
JsonUtils

public class BsonUtils extends Object
Author:
Andrea Di Cesare <andrea@softinstigate.com>
  • Field Details

    • DEFAULT_CODEC_REGISTRY

      public static final org.bson.codecs.configuration.CodecRegistry DEFAULT_CODEC_REGISTRY
  • Constructor Details

    • BsonUtils

      public BsonUtils()
  • Method Details

    • 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 See https://docs.mongodb.org/manual/reference/limits/#Restrictions-on-Field-Names
      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 keys are escaped
    • escapeKeys

      public static org.bson.BsonValue escapeKeys(org.bson.BsonValue json, boolean escapeDots, boolean dontEscapeDotsInRootKeys)
      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. Root level keys containing dots can be escluded from escaping (dontEscapeDotsInRootKeys=true) to allow using the dot notation to refer to nested keys but still escaping nested keys. In the following example the root level key is used to refer to a sub document property, while the nested key with dots must be escaped because it is an aggregation stage: PATCH { "mappings.Query.TheatersByCity.find": {"location.address.city": { "$arg": "city"} } }
      Parameters:
      json -
      escapeDots -
      dontEscapeDotsInRootKeys - specify if dots in root level keys should not be escaped when escapeDots=true. root level
      Returns:
      the json object where the keys are escaped 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
    • get

      public static Optional<org.bson.BsonValue> get(org.bson.BsonDocument doc, String path)
      Parameters:
      doc -
      path - the path of the field, can use the dot notation
      Returns:
    • get

      public static Optional<org.bson.BsonValue> get(org.apache.commons.jxpath.JXPathContext ctx, String path)
      Parameters:
      ctx - the JxPathContext build from a BsonDocument
      path - the path of the field, can use the dot notation
      Returns:
    • 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
    • countPropsFromPath

      public static Integer countPropsFromPath(org.bson.BsonValue root, String path) throws IllegalArgumentException
      Parameters:
      root -
      path -
      Returns:
      then number of properties identitified by the json path expression or null if path does not exist
      Throws:
      IllegalArgumentException
    • checkType

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

      public static StringBuilder 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 orr null if argument is an blank 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 - the JsonMode
      Returns:
      the minified string representation of the bson value
    • 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,? super Object> map)
      Parameters:
      map -
      Returns:
    • isUpdateOperator

      public static boolean isUpdateOperator(String key)
      Seehttps://docs.mongodb.com/manual/reference/operator/update/
      Parameters:
      key -
      Returns:
      true if key is an update operator
    • containsUpdateOperators

      public static boolean containsUpdateOperators(org.bson.BsonValue json)
      Seehttps://docs.mongodb.com/manual/reference/operator/update/
      Parameters:
      json -
      Returns:
      true if json contains update operators
    • containsUpdateOperators

      public static boolean containsUpdateOperators(org.bson.BsonValue json, boolean ignoreCurrentDate)
      Parameters:
      json -
      ignoreCurrentDate - true to ignore $currentDate
      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 keys 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}}
    • containsKeys

      public static boolean containsKeys(org.bson.BsonValue docOrArray, Set<String> keys, boolean all)
      Verifies if the bson contains the given keys. It also takes into account the cases where the bson cotains keys using the dot notation or update operators.
      Parameters:
      docOrArray - the bson to check
      keys - the keys
      all - true to check the bson to contain all given keys, false for any of the given keys
      Returns:
      true if the bson cotains the given keys
    • bsonToDocument

      public static org.bson.Document bsonToDocument(org.bson.BsonDocument bsonDocument)
      convert BsonDocument to Document
      Parameters:
      bsonDocument -
      Returns:
    • documentToBson

      public static org.bson.BsonValue documentToBson(org.bson.Document document)
      convert Document to BsonDocument
      Parameters:
      document -
      Returns:
    • document

      public static BsonUtils.DocumentBuilder document()
      Returns:
      a DocumentBuilder to help building BsonDocument
    • document

      public static BsonUtils.DocumentBuilder document(org.bson.BsonDocument doc)
      Parameters:
      doc - the BsonDocument to wrap
      Returns:
      a DocumentBuilder to help building BsonDocument
    • array

      public static BsonUtils.ArrayBuilder array()
      Returns:
      a ArrayBuilder to help building BsonArray
    • array

      public static BsonUtils.ArrayBuilder array(org.bson.BsonArray array)
      Parameters:
      array - the BsonArray to wrap
      Returns:
      a ArrayBuilder to help building BsonArray