|
| 1 | +/* |
| 2 | + * Copyright (c) 2008-2014 MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.bson.codecs; |
| 18 | + |
| 19 | +import org.bson.BsonBinarySubType; |
| 20 | +import org.bson.BsonReader; |
| 21 | +import org.bson.BsonType; |
| 22 | +import org.bson.BsonWriter; |
| 23 | +import org.bson.Transformer; |
| 24 | +import org.bson.codecs.configuration.CodecRegistry; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.UUID; |
| 30 | + |
| 31 | +import static java.util.Arrays.asList; |
| 32 | +import static org.bson.assertions.Assertions.notNull; |
| 33 | +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; |
| 34 | + |
| 35 | +/** |
| 36 | + * A Codec for Map instances. |
| 37 | + * |
| 38 | + * @since 3.5 |
| 39 | + */ |
| 40 | +public class MapCodec implements Codec<Map<String, Object>> { |
| 41 | + |
| 42 | + private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(), |
| 43 | + new DocumentCodecProvider(), new IterableCodecProvider(), new MapCodecProvider())); |
| 44 | + private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap(); |
| 45 | + private final BsonTypeCodecMap bsonTypeCodecMap; |
| 46 | + private final CodecRegistry registry; |
| 47 | + private final Transformer valueTransformer; |
| 48 | + |
| 49 | + /** |
| 50 | + * Construct a new instance with a default {@code CodecRegistry} |
| 51 | + */ |
| 52 | + public MapCodec() { |
| 53 | + this(DEFAULT_REGISTRY); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + Construct a new instance with the given registry |
| 58 | + * |
| 59 | + * @param registry the registry |
| 60 | + */ |
| 61 | + public MapCodec(final CodecRegistry registry) { |
| 62 | + this(registry, DEFAULT_BSON_TYPE_CLASS_MAP); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Construct a new instance with the given registry and BSON type class map. |
| 67 | + * |
| 68 | + * @param registry the registry |
| 69 | + * @param bsonTypeClassMap the BSON type class map |
| 70 | + */ |
| 71 | + public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap) { |
| 72 | + this(registry, bsonTypeClassMap, null); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Construct a new instance with the given registry and BSON type class map. The transformer is applied as a last step when decoding |
| 77 | + * values, which allows users of this codec to control the decoding process. For example, a user of this class could substitute a |
| 78 | + * value decoded as a Document with an instance of a special purpose class (e.g., one representing a DBRef in MongoDB). |
| 79 | + * |
| 80 | + * @param registry the registry |
| 81 | + * @param bsonTypeClassMap the BSON type class map |
| 82 | + * @param valueTransformer the value transformer to use as a final step when decoding the value of any field in the map |
| 83 | + */ |
| 84 | + public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) { |
| 85 | + this.registry = notNull("registry", registry); |
| 86 | + this.bsonTypeCodecMap = new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry); |
| 87 | + this.valueTransformer = valueTransformer != null ? valueTransformer : new Transformer() { |
| 88 | + @Override |
| 89 | + public Object transform(final Object value) { |
| 90 | + return value; |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void encode(final BsonWriter writer, final Map<String, Object> map, final EncoderContext encoderContext) { |
| 97 | + writer.writeStartDocument(); |
| 98 | + for (final Map.Entry<String, Object> entry : map.entrySet()) { |
| 99 | + writer.writeName(entry.getKey()); |
| 100 | + writeValue(writer, encoderContext, entry.getValue()); |
| 101 | + } |
| 102 | + writer.writeEndDocument(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Map<String, Object> decode(final BsonReader reader, final DecoderContext decoderContext) { |
| 107 | + Map<String, Object> map = new HashMap<String, Object>(); |
| 108 | + |
| 109 | + reader.readStartDocument(); |
| 110 | + while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { |
| 111 | + String fieldName = reader.readName(); |
| 112 | + map.put(fieldName, readValue(reader, decoderContext)); |
| 113 | + } |
| 114 | + |
| 115 | + reader.readEndDocument(); |
| 116 | + return map; |
| 117 | + } |
| 118 | + |
| 119 | + @SuppressWarnings("unchecked") |
| 120 | + @Override |
| 121 | + public Class<Map<String, Object>> getEncoderClass() { |
| 122 | + return (Class<Map<String, Object>>) ((Class) Map.class); |
| 123 | + } |
| 124 | + |
| 125 | + private Object readValue(final BsonReader reader, final DecoderContext decoderContext) { |
| 126 | + BsonType bsonType = reader.getCurrentBsonType(); |
| 127 | + if (bsonType == BsonType.NULL) { |
| 128 | + reader.readNull(); |
| 129 | + return null; |
| 130 | + } else if (bsonType == BsonType.ARRAY) { |
| 131 | + return decoderContext.decodeWithChildContext(registry.get(List.class), reader); |
| 132 | + } else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) { |
| 133 | + return decoderContext.decodeWithChildContext(registry.get(UUID.class), reader); |
| 134 | + } |
| 135 | + return valueTransformer.transform(bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext)); |
| 136 | + } |
| 137 | + |
| 138 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 139 | + private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) { |
| 140 | + if (value == null) { |
| 141 | + writer.writeNull(); |
| 142 | + } else { |
| 143 | + Codec codec = registry.get(value.getClass()); |
| 144 | + encoderContext.encodeWithChildContext(codec, writer, value); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments