Package org.bson.codecs
Class BsonDocumentCodec
java.lang.Object
org.bson.codecs.BsonDocumentCodec
- All Implemented Interfaces:
Codec<BsonDocument>
,CollectibleCodec<BsonDocument>
,Decoder<BsonDocument>
,Encoder<BsonDocument>
A codec for BsonDocument instances.
- Since:
- 3.0
-
Constructor Summary
ConstructorDescriptionCreates a new instance with a default codec registry that uses theBsonValueCodecProvider
.BsonDocumentCodec
(CodecRegistry codecRegistry) Creates a new instance initialised with the given codec registry. -
Method Summary
Modifier and TypeMethodDescriptiondecode
(BsonReader reader, DecoderContext decoderContext) Decodes a BSON value from the given reader into an instance of the type parameterT
.boolean
documentHasId
(BsonDocument document) Returns true if the given document has an _id.void
encode
(BsonWriter writer, BsonDocument value, EncoderContext encoderContext) Encode an instance of the type parameterT
into a BSON value.generateIdIfAbsentFromDocument
(BsonDocument document) Generates a value for the _id field on the given document, if the document does not have one.Gets theCodecRegistry
for thisCodec
.getDocumentId
(BsonDocument document) Gets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException
.Returns the Class instance that this encodes.protected BsonValue
readValue
(BsonReader reader, DecoderContext decoderContext) This method may be overridden to change the behavior of reading the current value from the givenBsonReader
.
-
Constructor Details
-
BsonDocumentCodec
public BsonDocumentCodec()Creates a new instance with a default codec registry that uses theBsonValueCodecProvider
. -
BsonDocumentCodec
Creates a new instance initialised with the given codec registry.- Parameters:
codecRegistry
- theCodecRegistry
to use to look up the codecs for encoding and decoding to/from BSON
-
-
Method Details
-
getCodecRegistry
Gets theCodecRegistry
for thisCodec
.- Returns:
- the registry
-
decode
Description copied from interface:Decoder
Decodes a BSON value from the given reader into an instance of the type parameterT
.- Specified by:
decode
in interfaceDecoder<BsonDocument>
- Parameters:
reader
- the BSON readerdecoderContext
- the decoder context- Returns:
- an instance of the type parameter
T
.
-
readValue
This method may be overridden to change the behavior of reading the current value from the givenBsonReader
. It is required that the value be fully consumed before returning.- Parameters:
reader
- the read to read the value fromdecoderContext
- the context- Returns:
- the non-null value read from the reader
-
encode
Description copied from interface:Encoder
Encode an instance of the type parameterT
into a BSON value.- Specified by:
encode
in interfaceEncoder<BsonDocument>
- Parameters:
writer
- the BSON writer to encode intovalue
- the value to encodeencoderContext
- the encoder context
-
getEncoderClass
Description copied from interface:Encoder
Returns the Class instance that this encodes. This is necessary because Java does not reify generic types.- Specified by:
getEncoderClass
in interfaceEncoder<BsonDocument>
- Returns:
- the Class instance that this encodes.
-
generateIdIfAbsentFromDocument
Description copied from interface:CollectibleCodec
Generates a value for the _id field on the given document, if the document does not have one.- Specified by:
generateIdIfAbsentFromDocument
in interfaceCollectibleCodec<BsonDocument>
- Parameters:
document
- the document for which to generate a value for the _id.- Returns:
- the document with the _id
-
documentHasId
Description copied from interface:CollectibleCodec
Returns true if the given document has an _id.- Specified by:
documentHasId
in interfaceCollectibleCodec<BsonDocument>
- Parameters:
document
- the document in which to look for an _id- Returns:
- true if the document has an _id
-
getDocumentId
Description copied from interface:CollectibleCodec
Gets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException
. To avoid the latter case, calldocumentHasId
first to check.- Specified by:
getDocumentId
in interfaceCollectibleCodec<BsonDocument>
- Parameters:
document
- the document from which to get the _id- Returns:
- the _id of the document
-