package org.bson.codecs;
import org.bson.BsonBinarySubType;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.Transformer;
import org.bson.codecs.configuration.CodecRegistry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static java.util.Arrays.asList;
import static org.bson.assertions.Assertions.notNull;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
public class MapCodec implements Codec<Map<String, Object>> {
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(),
new DocumentCodecProvider(), new IterableCodecProvider(), new MapCodecProvider()));
private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
private final BsonTypeCodecMap bsonTypeCodecMap;
private final CodecRegistry registry;
private final Transformer valueTransformer;
public MapCodec() {
this(DEFAULT_REGISTRY);
}
public MapCodec(final CodecRegistry registry) {
this(registry, DEFAULT_BSON_TYPE_CLASS_MAP);
}
public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap) {
this(registry, bsonTypeClassMap, null);
}
public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
this.registry = notNull("registry", registry);
this.bsonTypeCodecMap = new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry);
this.valueTransformer = valueTransformer != null ? valueTransformer : new Transformer() {
@Override
public Object transform(final Object value) {
return value;
}
};
}
@Override
public void encode(final BsonWriter writer, final Map<String, Object> map, final EncoderContext encoderContext) {
writer.writeStartDocument();
for (final Map.Entry<String, Object> entry : map.entrySet()) {
writer.writeName(entry.getKey());
writeValue(writer, encoderContext, entry.getValue());
}
writer.writeEndDocument();
}
@Override
public Map<String, Object> decode(final BsonReader reader, final DecoderContext decoderContext) {
Map<String, Object> map = new HashMap<String, Object>();
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
map.put(fieldName, readValue(reader, decoderContext));
}
reader.readEndDocument();
return map;
}
@SuppressWarnings("unchecked")
@Override
public Class<Map<String, Object>> getEncoderClass() {
return (Class<Map<String, Object>>) ((Class) Map.class);
}
private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
BsonType bsonType = reader.getCurrentBsonType();
if (bsonType == BsonType.NULL) {
reader.readNull();
return null;
} else if (bsonType == BsonType.ARRAY) {
return decoderContext.decodeWithChildContext(registry.get(List.class), reader);
} else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
return decoderContext.decodeWithChildContext(registry.get(UUID.class), reader);
}
return valueTransformer.transform(bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext));
}
@SuppressWarnings({"unchecked", "rawtypes"})
private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) {
if (value == null) {
writer.writeNull();
} else {
Codec codec = registry.get(value.getClass());
encoderContext.encodeWithChildContext(codec, writer, value);
}
}
}