public class
BsonUtils
extends
Object
Internal API for operations on
Bson
elements that can be either
Document
or
DBObject
.
Since:
Author:
Christoph Strobl, Mark Paluch
static org.bson.Document
asDocument
(org.bson.conversions.Bson bson,
org.bson.codecs.configuration.CodecRegistry codecRegistry)
Return the
Bson
object as
Document
.
asMap
(org.bson.conversions.Bson bson)
Return the
Bson
object as
Map
.
asMap
(org.bson.conversions.Bson bson,
org.bson.codecs.configuration.CodecRegistry codecRegistry)
Return the
Bson
object as
Map
.
static org.bson.Document
Return the
Bson
object as mutable
Document
containing all entries from
Bson
.
static boolean
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
Returns whether the underlying
bson
has a value (null or non-null) for the given
key
.
static boolean
Check if a given String looks like
parsable
json array.
static boolean
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 the given json to
Document
applying transformations as specified by a potentially given
Codec
.
static boolean
Remove
_id : null
from the given
Bson
if present.
Resolve a the value for a given key.
static org.bson.BsonValue
Convert a given simple value (eg.
static boolean
Returns the given source can be used/converted as
Bson
.
static org.bson.Document
Extract the corresponding plain value from
BsonValue
.
toJson
(org.bson.Document source)
Serialize the given
Document
as Json applying default codecs if necessary.
EMPTY_DOCUMENT
public static final
org.bson.Document
EMPTY_DOCUMENT
The empty document (immutable). This document is serializable.
Since:
3.2.5
asMap
public static
Map
<
String
,
Object
>
asMap
(org.bson.conversions.Bson bson)
Return the
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).
Parameters:
bson
-
Returns:
asMap
public static
Map
<
String
,
Object
>
asMap
(
@Nullable
org.bson.conversions.Bson bson,
org.bson.codecs.configuration.CodecRegistry codecRegistry)
Return the
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) using the given
CodecRegistry
to
obtain
codecs
that might be required for conversion.
Parameters:
bson
- can be null.
codecRegistry
- must not be null.
Returns:
never null. Returns an empty
Map
if input
Bson
is null.
Since:
asDocument
public static
org.bson.Document
asDocument
(org.bson.conversions.Bson bson)
Return the
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).
Parameters:
bson
-
Returns:
Since:
3.2.5
asDocument
public static
org.bson.Document
asDocument
(org.bson.conversions.Bson bson,
org.bson.codecs.configuration.CodecRegistry codecRegistry)
Return the
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) using the given
CodecRegistry
to obtain
codecs
that might be required for conversion.
Parameters:
bson
-
codecRegistry
- must not be null.
Returns:
never null.
Since:
asMutableDocument
public static
org.bson.Document
asMutableDocument
(org.bson.conversions.Bson bson)
Return the
Bson
object as mutable
Document
containing all entries from
Bson
.
Parameters:
bson
-
Returns:
a mutable
Document
containing all entries from
Bson
.
Since:
3.2.5
addAllToMap
public static
void
addAllToMap
(org.bson.conversions.Bson target,
Map
<
String
,
?> source)
Add all entries from the given source
Map
to the target.
Parameters:
target
- must not be null.
source
- must not be null.
Since:
@Nullable
Object
value)
Check if a given entry (key/value pair) is present in the given
Bson
.
Parameters:
bson
- must not be null.
key
- must not be null.
value
- can be null.
Returns:
true if (key/value pair) is present.
Since:
removeNullId
public static
boolean
removeNullId
(org.bson.conversions.Bson bson)
Remove
_id : null
from the given
Bson
if present.
Parameters:
bson
- must not be null.
Since:
toJavaType
public static
Object
toJavaType
(org.bson.BsonValue value)
Extract the corresponding plain value from
BsonValue
. Eg. plain
String
from
BsonString
.
Parameters:
value
- must not be null.
Returns:
Since:
simpleToBsonValue
public static
org.bson.BsonValue
simpleToBsonValue
(
Object
source)
Convert a given simple value (eg.
String
,
Long
) to its corresponding
BsonValue
.
Parameters:
source
- must not be null.
Returns:
the corresponding
BsonValue
representation.
Throws:
IllegalArgumentException
- if source does not correspond to a
BsonValue
type.
Since:
merge
public static
org.bson.Document
merge
(org.bson.Document... documents)
Merge the given
documents
into on in the given order. Keys contained within multiple documents are
overwritten by their follow ups.
Parameters:
documents
- must not be null. Can be empty.
Returns:
the document containing all key value pairs.
Since:
toDocumentOrElse
public static
org.bson.Document
toDocumentOrElse
(
String
source,
Function
<
String
,
org.bson.Document> orElse)
Parameters:
source
-
orElse
-
Returns:
Since:
public static
String
toJson
(
@Nullable
org.bson.Document source)
Serialize the given
Document
as Json applying default codecs if necessary.
Parameters:
source
-
Returns:
Since:
2.2.1
Check if a given String looks like
parsable
json.
Parameters:
value
- can be null.
Returns:
true if the given value looks like a json document.
Since:
Check if a given String looks like
parsable
json array.
Parameters:
value
- can be null.
Returns:
true if the given value looks like a json array.
Since:
Parse the given json to
Document
applying transformations as specified by a potentially given
Codec
.
Parameters:
json
- must not be null.
codecRegistryProvider
- can be null. In that case the default
DocumentCodec
is used.
Returns:
never null.
Throws:
IllegalArgumentException
- if the required argument is null.
Since:
resolveValue
Resolve a the value for a given key. If the given
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.
Parameters:
bson
- the source to inspect. Must not be null.
key
- the key to lookup. Must not be null.
Returns:
can be null.
Since:
3.0.8
public 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
.
Parameters:
bson
- the source to inspect. Must not be null.
key
- the key to lookup. Must not be null.
Returns:
true if no non null value present.
Since:
3.0.8
asBson
public static
org.bson.conversions.Bson
asBson
(
Object
source)
Parameters:
source
-
Returns:
the converted/casted source object.
Throws:
IllegalArgumentException
- if
source
cannot be converted/cast to
Bson
.
Since:
3.2.3
See Also:
supportsBson(Object)
supportsBson
public static
boolean
supportsBson
(
Object
source)
Returns the given source can be used/converted as
Bson
.
Parameters:
source
-
Returns:
true if the given source can be converted to
Bson
.
Since:
3.2.3
asCollection
Returns given object as
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.
Parameters:
source
- must not be null.
Returns:
never null.
Since: