org.bson.codecs.DocumentCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
The newest version!
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bson.codecs;
import org.bson.BsonBinarySubType;
import org.bson.BsonDocument;
import org.bson.BsonDocumentWriter;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonValue;
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.Transformer;
import org.bson.assertions.Assertions;
import org.bson.codecs.configuration.CodecRegistry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static java.util.Arrays.asList;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
/**
* A Codec for Document instances.
*
* @see org.bson.Document
* @since 3.0
*/
public class DocumentCodec implements CollectibleCodec {
private static final String ID_FIELD_NAME = "_id";
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider()));
private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
private final BsonTypeClassMap bsonTypeClassMap;
private final CodecRegistry registry;
private final IdGenerator idGenerator;
private final Transformer valueTransformer;
/**
* Construct a new instance with a default {@code CodecRegistry} and
*/
public DocumentCodec() {
this(DEFAULT_REGISTRY, DEFAULT_BSON_TYPE_CLASS_MAP);
}
/**
* Construct a new instance with the given registry and BSON type class map.
*
* @param registry the registry
* @param bsonTypeClassMap the BSON type class map
*/
public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap) {
this(registry, bsonTypeClassMap, null);
}
/**
* 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).
*
* @param registry the registry
* @param bsonTypeClassMap the BSON type class map
* @param valueTransformer the value transformer to use as a final step when decoding the value of any field in the document
*/
public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
this.registry = Assertions.notNull("registry", registry);
this.bsonTypeClassMap = Assertions.notNull("bsonTypeClassMap", bsonTypeClassMap);
this.idGenerator = Assertions.notNull("idGenerator", new ObjectIdGenerator());
this.valueTransformer = valueTransformer != null ? valueTransformer : new Transformer() {
@Override
public Object transform(final Object value) {
return value;
}
};
}
@Override
public boolean documentHasId(final Document document) {
return document.containsKey(ID_FIELD_NAME);
}
@Override
public BsonValue getDocumentId(final Document document) {
if (!documentHasId(document)) {
throw new IllegalStateException("The document does not contain an _id");
}
Object id = document.get(ID_FIELD_NAME);
if (id instanceof BsonValue) {
return (BsonValue) id;
}
BsonDocument idHoldingDocument = new BsonDocument();
BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
writer.writeStartDocument();
writer.writeName(ID_FIELD_NAME);
writeValue(writer, EncoderContext.builder().build(), id);
writer.writeEndDocument();
return idHoldingDocument.get(ID_FIELD_NAME);
}
@Override
public Document generateIdIfAbsentFromDocument(final Document document) {
if (!documentHasId(document)) {
document.put(ID_FIELD_NAME, idGenerator.generate());
}
return document;
}
@Override
public void encode(final BsonWriter writer, final Document document, final EncoderContext encoderContext) {
writeMap(writer, document, encoderContext);
}
@Override
public Document decode(final BsonReader reader, final DecoderContext decoderContext) {
Document document = new Document();
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
document.put(fieldName, readValue(reader, decoderContext));
}
reader.readEndDocument();
return document;
}
@Override
public Class getEncoderClass() {
return Document.class;
}
private void beforeFields(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Map document) {
if (encoderContext.isEncodingCollectibleDocument() && document.containsKey(ID_FIELD_NAME)) {
bsonWriter.writeName(ID_FIELD_NAME);
writeValue(bsonWriter, encoderContext, document.get(ID_FIELD_NAME));
}
}
private boolean skipField(final EncoderContext encoderContext, final String key) {
return encoderContext.isEncodingCollectibleDocument() && key.equals(ID_FIELD_NAME);
}
@SuppressWarnings({"unchecked", "rawtypes"})
private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) {
if (value == null) {
writer.writeNull();
} else if (Iterable.class.isAssignableFrom(value.getClass())) {
writeIterable(writer, (Iterable