Package org.restheart.utils
Class JsonUtils
- java.lang.Object
-
- org.restheart.utils.JsonUtils
-
public class JsonUtils extends Object
- Author:
- Andrea Di Cesare <andrea@softinstigate.com>
-
-
Constructor Summary
Constructors Constructor Description JsonUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancheckType(Optional<org.bson.BsonValue> o, String type)static booleancontainsUpdateOperators(org.bson.BsonValue json)static booleancontainsUpdateOperators(org.bson.BsonValue json, boolean ignoreCurrentDate)static IntegercountPropsFromPath(org.bson.BsonValue root, String path)static 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.BsonDocumentflatten(org.bson.BsonDocument json, boolean ignoreUpdateOperators)static StringgetIdAsString(org.bson.BsonValue id, boolean quote)static List<Optional<org.bson.BsonValue>>getPropsFromPath(org.bson.BsonValue root, String path)static booleanisAncestorPath(String left, String right)static booleanisUpdateOperator(String key)static Stringminify(String jsonString)static org.bson.BsonValueparse(String json)static org.bson.BsonDocumenttoBsonDocument(Map<String,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)
-
-
-
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 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- See Also:
form code examples
-
isAncestorPath
public static boolean isAncestorPath(String left, String right)
- 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
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 BsonArraymode-- 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 $currentDatejson-- 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}}
-
-