public class BsonUtils extends Object
Bson elements that can be either Document or DBObject.| Modifier and Type | Field and Description |
|---|---|
static org.bson.Document |
EMPTY_DOCUMENT
The empty document (immutable).
|
| Constructor and Description |
|---|
BsonUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addAllToMap(org.bson.conversions.Bson target,
Map<String,?> source)
Add all entries from the given source
Map to the target. |
static void |
addToMap(org.bson.conversions.Bson bson,
String key,
Object value) |
static org.bson.conversions.Bson |
asBson(Object source)
Returns the given source object as
Bson, i.e. |
static Collection<?> |
asCollection(Object source)
Returns given object as
Collection. |
static org.bson.Document |
asDocument(org.bson.conversions.Bson bson)
Return the
Bson object as Document. |
static Map<String,Object> |
asMap(org.bson.conversions.Bson bson)
Return the
Bson object as Map. |
static org.bson.Document |
asMutableDocument(org.bson.conversions.Bson bson)
Return the
Bson object as mutable Document containing all entries from Bson. |
static boolean |
contains(org.bson.conversions.Bson bson,
String key,
Object value)
Check if a given entry (key/value pair) is present in the given
Bson. |
static <T> T |
get(org.bson.conversions.Bson bson,
String key) |
static boolean |
hasValue(org.bson.conversions.Bson bson,
String key)
Returns whether the underlying
bson has a value (null or non-null) for the given
key. |
static boolean |
isJsonArray(String value)
Check if a given String looks like
parsable json array. |
static boolean |
isJsonDocument(String value)
Check if a given String looks like
parsable json. |
static org.bson.Document |
merge(org.bson.Document... documents)
Merge the given
documents into on in the given order. |
static org.bson.Document |
parse(String json,
CodecRegistryProvider codecRegistryProvider)
Parse the given json to
Document applying transformations as specified by a potentially given
Codec. |
static boolean |
removeNullId(org.bson.conversions.Bson bson)
Remove
_id : null from the given Bson if present. |
static Object |
resolveValue(org.bson.conversions.Bson bson,
String key)
Resolve a the value for a given key.
|
static org.bson.BsonValue |
simpleToBsonValue(Object source)
Convert a given simple value (eg.
|
static boolean |
supportsBson(Object source)
Returns the given source can be used/converted as
Bson. |
static org.bson.Document |
toDocumentOrElse(String source,
Function<String,org.bson.Document> orElse) |
static Object |
toJavaType(org.bson.BsonValue value)
Extract the corresponding plain value from
BsonValue. |
static String |
toJson(org.bson.Document source)
Serialize the given
Document as Json applying default codecs if necessary. |
public static final org.bson.Document EMPTY_DOCUMENT
public static Map<String,Object> asMap(org.bson.conversions.Bson bson)
Bson object as Map. Depending on the input type, the return value can be either a casted
version of bson or a converted (detached from the original value).bson - public static org.bson.Document asDocument(org.bson.conversions.Bson bson)
Bson object as Document. Depending on the input type, the return value can be either a
casted version of bson or a converted (detached from the original value).bson - public static org.bson.Document asMutableDocument(org.bson.conversions.Bson bson)
Bson object as mutable Document containing all entries from Bson.bson - Document containing all entries from Bson.public static void addToMap(org.bson.conversions.Bson bson,
String key,
@Nullable
Object value)
public static void addAllToMap(org.bson.conversions.Bson target,
Map<String,?> source)
Map to the target.target - must not be null.source - must not be null.public static boolean contains(org.bson.conversions.Bson bson,
String key,
@Nullable
Object value)
Bson.bson - must not be null.key - must not be null.value - can be null.public static boolean removeNullId(org.bson.conversions.Bson bson)
_id : null from the given Bson if present.bson - must not be null.public static Object toJavaType(org.bson.BsonValue value)
value - must not be null.public static org.bson.BsonValue simpleToBsonValue(Object source)
source - must not be null.BsonValue representation.IllegalArgumentException - if source does not correspond to a BsonValue type.public static org.bson.Document merge(org.bson.Document... documents)
documents into on in the given order. Keys contained within multiple documents are
overwritten by their follow ups.documents - must not be null. Can be empty.public static org.bson.Document toDocumentOrElse(String source, Function<String,org.bson.Document> orElse)
source - orElse - @Nullable public static String toJson(@Nullable org.bson.Document source)
Document as Json applying default codecs if necessary.source - public static boolean isJsonDocument(@Nullable String value)
parsable json.value - can be null.public static boolean isJsonArray(@Nullable String value)
parsable json array.value - can be null.public static org.bson.Document parse(String json, @Nullable CodecRegistryProvider codecRegistryProvider)
Document applying transformations as specified by a potentially given
Codec.json - must not be null.codecRegistryProvider - can be null. In that case the default DocumentCodec is used.IllegalArgumentException - if the required argument is null.@Nullable public static Object resolveValue(org.bson.conversions.Bson bson, String key)
Bson value contains the key the value is immediately
returned. If not and the key contains a path using the dot (.) notation it will try to resolve the path by
inspecting the individual parts. If one of the intermediate ones is null or cannot be inspected further
(wrong) type, null is returned.bson - the source to inspect. Must not be null.key - the key to lookup. Must not be null.public static boolean hasValue(org.bson.conversions.Bson bson,
String key)
bson has a value (null or non-null) for the given
key.bson - the source to inspect. Must not be null.key - the key to lookup. Must not be null.public static org.bson.conversions.Bson asBson(Object source)
Bson, i.e. Documents and maps as is or throw
IllegalArgumentException.source - IllegalArgumentException - if source cannot be converted/cast to Bson.supportsBson(Object)public static boolean supportsBson(Object source)
Bson.source - Bson.public static Collection<?> asCollection(Object source)
Collection. Will return the Collection as is if the source is a
Collection already, will convert an array into a Collection or simply create a single element
collection for everything else.source - must not be null.Copyright © 2011–2023 Pivotal Software, Inc.. All rights reserved.