Class DocumentCodec

    • Constructor Detail

      • DocumentCodec

        public DocumentCodec()
        Construct a new instance with a default CodecRegistry.
      • DocumentCodec

        public DocumentCodec​(CodecRegistry registry)
        Construct a new instance with the given registry.
        Parameters:
        registry - the registry
        Since:
        3.5
      • DocumentCodec

        public DocumentCodec​(CodecRegistry registry,
                             BsonTypeClassMap bsonTypeClassMap)
        Construct a new instance with the given registry and BSON type class map.
        Parameters:
        registry - the registry
        bsonTypeClassMap - the BSON type class map
      • DocumentCodec

        public DocumentCodec​(CodecRegistry registry,
                             BsonTypeClassMap bsonTypeClassMap,
                             Transformer valueTransformer)
        Construct a new instance with the given registry and BSON type class map. The transformer is applied as a last step when decoding values, which allows users of this codec to control the decoding process. For example, a user of this class could substitute a value decoded as a Document with an instance of a special purpose class (e.g., one representing a DBRef in MongoDB).
        Parameters:
        registry - the registry
        bsonTypeClassMap - the BSON type class map
        valueTransformer - the value transformer to use as a final step when decoding the value of any field in the document
    • Method Detail

      • documentHasId

        public boolean documentHasId​(Document document)
        Description copied from interface: CollectibleCodec
        Returns true if the given document has an _id.
        Specified by:
        documentHasId in interface CollectibleCodec<Document>
        Parameters:
        document - the document in which to look for an _id
        Returns:
        true if the document has an _id
      • getDocumentId

        public BsonValue getDocumentId​(Document document)
        Description copied from interface: CollectibleCodec
        Gets the _id of the given document if it contains one, otherwise throws IllegalArgumentException. To avoid the latter case, call documentHasId first to check.
        Specified by:
        getDocumentId in interface CollectibleCodec<Document>
        Parameters:
        document - the document from which to get the _id
        Returns:
        the _id of the document
      • generateIdIfAbsentFromDocument

        public Document generateIdIfAbsentFromDocument​(Document document)
        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 interface CollectibleCodec<Document>
        Parameters:
        document - the document for which to generate a value for the _id.
        Returns:
        the document with the _id
      • encode

        public void encode​(BsonWriter writer,
                           Document document,
                           EncoderContext encoderContext)
        Description copied from interface: Encoder
        Encode an instance of the type parameter T into a BSON value.
        Specified by:
        encode in interface Encoder<Document>
        Parameters:
        writer - the BSON writer to encode into
        document - the value to encode
        encoderContext - the encoder context
      • decode

        public Document decode​(BsonReader reader,
                               DecoderContext decoderContext)
        Description copied from interface: Decoder
        Decodes a BSON value from the given reader into an instance of the type parameter T.
        Specified by:
        decode in interface Decoder<Document>
        Parameters:
        reader - the BSON reader
        decoderContext - the decoder context
        Returns:
        an instance of the type parameter T.
      • getEncoderClass

        public Class<Document> 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 interface Encoder<Document>
        Returns:
        the Class instance that this encodes.