Package org.bson.codecs
Class BsonDocumentCodec
- java.lang.Object
- 
- org.bson.codecs.BsonDocumentCodec
 
- 
- All Implemented Interfaces:
- Codec<BsonDocument>,- CollectibleCodec<BsonDocument>,- Decoder<BsonDocument>,- Encoder<BsonDocument>
 
 public class BsonDocumentCodec extends Object implements CollectibleCodec<BsonDocument> A codec for BsonDocument instances.- Since:
- 3.0
 
- 
- 
Constructor SummaryConstructors Constructor Description BsonDocumentCodec()Creates 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 SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description BsonDocumentdecode(BsonReader reader, DecoderContext decoderContext)Decodes a BSON value from the given reader into an instance of the type parameterT.booleandocumentHasId(BsonDocument document)Returns true if the given document has an _id.voidencode(BsonWriter writer, BsonDocument value, EncoderContext encoderContext)Encode an instance of the type parameterTinto a BSON value.BsonDocumentgenerateIdIfAbsentFromDocument(BsonDocument document)Generates a value for the _id field on the given document, if the document does not have one.CodecRegistrygetCodecRegistry()Gets theCodecRegistryfor thisCodec.BsonValuegetDocumentId(BsonDocument document)Gets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException.Class<BsonDocument>getEncoderClass()Returns the Class instance that this encodes.protected BsonValuereadValue(BsonReader reader, DecoderContext decoderContext)This method may be overridden to change the behavior of reading the current value from the givenBsonReader.
 
- 
- 
- 
Constructor Detail- 
BsonDocumentCodecpublic BsonDocumentCodec() Creates a new instance with a default codec registry that uses theBsonValueCodecProvider.
 - 
BsonDocumentCodecpublic BsonDocumentCodec(CodecRegistry codecRegistry) Creates a new instance initialised with the given codec registry.- Parameters:
- codecRegistry- the- CodecRegistryto use to look up the codecs for encoding and decoding to/from BSON
 
 
- 
 - 
Method Detail- 
getCodecRegistrypublic CodecRegistry getCodecRegistry() Gets theCodecRegistryfor thisCodec.- Returns:
- the registry
 
 - 
decodepublic BsonDocument decode(BsonReader reader, DecoderContext decoderContext) Description copied from interface:DecoderDecodes a BSON value from the given reader into an instance of the type parameterT.- Specified by:
- decodein interface- Decoder<BsonDocument>
- Parameters:
- reader- the BSON reader
- decoderContext- the decoder context
- Returns:
- an instance of the type parameter T.
 
 - 
readValueprotected BsonValue readValue(BsonReader reader, DecoderContext decoderContext) 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 from
- decoderContext- the context
- Returns:
- the non-null value read from the reader
 
 - 
encodepublic void encode(BsonWriter writer, BsonDocument value, EncoderContext encoderContext) Description copied from interface:EncoderEncode an instance of the type parameterTinto a BSON value.- Specified by:
- encodein interface- Encoder<BsonDocument>
- Parameters:
- writer- the BSON writer to encode into
- value- the value to encode
- encoderContext- the encoder context
 
 - 
getEncoderClasspublic Class<BsonDocument> getEncoderClass() Description copied from interface:EncoderReturns the Class instance that this encodes. This is necessary because Java does not reify generic types.- Specified by:
- getEncoderClassin interface- Encoder<BsonDocument>
- Returns:
- the Class instance that this encodes.
 
 - 
generateIdIfAbsentFromDocumentpublic BsonDocument generateIdIfAbsentFromDocument(BsonDocument document) Description copied from interface:CollectibleCodecGenerates a value for the _id field on the given document, if the document does not have one.- Specified by:
- generateIdIfAbsentFromDocumentin interface- CollectibleCodec<BsonDocument>
- Parameters:
- document- the document for which to generate a value for the _id.
- Returns:
- the document with the _id
 
 - 
documentHasIdpublic boolean documentHasId(BsonDocument document) Description copied from interface:CollectibleCodecReturns true if the given document has an _id.- Specified by:
- documentHasIdin interface- CollectibleCodec<BsonDocument>
- Parameters:
- document- the document in which to look for an _id
- Returns:
- true if the document has an _id
 
 - 
getDocumentIdpublic BsonValue getDocumentId(BsonDocument document) Description copied from interface:CollectibleCodecGets the _id of the given document if it contains one, otherwise throwsIllegalArgumentException. To avoid the latter case, calldocumentHasIdfirst to check.- Specified by:
- getDocumentIdin interface- CollectibleCodec<BsonDocument>
- Parameters:
- document- the document from which to get the _id
- Returns:
- the _id of the document
 
 
- 
 
-