Package org.restheart.utils
Class BsonUtils
java.lang.Object
org.restheart.utils.BsonUtils
- Direct Known Subclasses:
JsonUtils
- Author:
- Andrea Di Cesare <andrea@softinstigate.com>
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder to help creating BsonArraystatic classBuilder to help creating BsonDocument -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.bson.codecs.configuration.CodecRegistry -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BsonUtils.ArrayBuilderarray()static BsonUtils.ArrayBuilderarray(org.bson.BsonArray array) static org.bson.DocumentbsonToDocument(org.bson.BsonDocument bsonDocument) convert BsonDocument to Documentstatic booleanstatic booleancontainsKeys(org.bson.BsonValue docOrArray, Set<String> keys, boolean all) Verifies if the bson contains the given keys.static booleancontainsUpdateOperators(org.bson.BsonValue json) Seehttps://docs.mongodb.com/manual/reference/operator/update/static booleancontainsUpdateOperators(org.bson.BsonValue json, boolean ignoreCurrentDate) static IntegercountPropsFromPath(org.bson.BsonValue root, String path) static BsonUtils.DocumentBuilderdocument()static BsonUtils.DocumentBuilderdocument(org.bson.BsonDocument doc) static org.bson.BsonValuedocumentToBson(org.bson.Document document) convert Document to BsonDocumentstatic org.bson.BsonValueescapeKeys(org.bson.BsonValue json, boolean escapeDots) replaces the dollar prefixed keys (eg $exists) with the corresponding underscore prefixed key (eg _$exists).static org.bson.BsonValueescapeKeys(org.bson.BsonValue json, boolean escapeDots, boolean dontEscapeDotsInRootKeys) replaces the dollar prefixed keys (eg $exists) with the corresponding underscore prefixed key (eg _$exists).static org.bson.BsonDocumentflatten(org.bson.BsonDocument json, boolean ignoreUpdateOperators) static Optional<org.bson.BsonValue> static Optional<org.bson.BsonValue> static StringgetIdAsString(org.bson.BsonValue id, boolean quote) getPropsFromPath(org.bson.BsonValue root, String path) static booleanisAncestorPath(String left, String right) static booleanisUpdateOperator(String key) Seehttps://docs.mongodb.com/manual/reference/operator/update/static StringBuilderstatic org.bson.BsonValuestatic org.bson.BsonDocumenttoBsonDocument(Map<String, ? super Object> map) static StringtoJson(org.bson.BsonValue bson) static StringtoJson(org.bson.BsonValue bson, org.bson.json.JsonMode mode) static org.bson.BsonValueunescapeKeys(org.bson.BsonValue json) replaces the underscore prefixed keys (eg _$exists) with the corresponding key (eg $exists) and the dot (.) in property names.static org.bson.BsonValueunflatten(org.bson.BsonValue json)
-
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 frompath- 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
- 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 BsonDocumentpath- the path of the field, can use the dot notation- Returns:
-
isAncestorPath
- Parameters:
left- the json path expressionright- 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
- Parameters:
o-type-- Returns:
-
minify
- Parameters:
jsonString-- Returns:
- minified json string
-
parse
- 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
- 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
- Parameters:
bson- either a BsonDocument or a BsonArraymode- the JsonMode- Returns:
- the minified string representation of the bson value
-
getIdAsString
- Parameters:
id-quote-- Returns:
- the String representation of the id
-
toBsonDocument
- Parameters:
map-- Returns:
-
isUpdateOperator
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
- 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
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 checkkeys- the keysall- 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
- Returns:
- a DocumentBuilder to help building BsonDocument
-
document
- Parameters:
doc- the BsonDocument to wrap- Returns:
- a DocumentBuilder to help building BsonDocument
-
array
- Returns:
- a ArrayBuilder to help building BsonArray
-
array
- Parameters:
array- the BsonArray to wrap- Returns:
- a ArrayBuilder to help building BsonArray
-