16
16
17
17
package org .bson .codecs ;
18
18
19
- import org .bson .BsonBinarySubType ;
20
19
import org .bson .BsonDocument ;
21
20
import org .bson .BsonDocumentWriter ;
22
21
import org .bson .BsonReader ;
25
24
import org .bson .BsonWriter ;
26
25
import org .bson .Document ;
27
26
import org .bson .Transformer ;
27
+ import org .bson .UuidRepresentation ;
28
28
import org .bson .codecs .configuration .CodecRegistry ;
29
29
30
30
import java .util .ArrayList ;
42
42
* @see org.bson.Document
43
43
* @since 3.0
44
44
*/
45
- public class DocumentCodec implements CollectibleCodec <Document > {
45
+ public class DocumentCodec implements CollectibleCodec <Document >, OverridableUuidRepresentationCodec < Document > {
46
46
47
47
private static final String ID_FIELD_NAME = "_id" ;
48
48
private static final CodecRegistry DEFAULT_REGISTRY = fromProviders (asList (new ValueCodecProvider (),
@@ -54,6 +54,7 @@ public class DocumentCodec implements CollectibleCodec<Document> {
54
54
private final CodecRegistry registry ;
55
55
private final IdGenerator idGenerator ;
56
56
private final Transformer valueTransformer ;
57
+ private final UuidRepresentation uuidRepresentation ;
57
58
58
59
/**
59
60
* Construct a new instance with a default {@code CodecRegistry}.
@@ -92,15 +93,27 @@ public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTy
92
93
* @param valueTransformer the value transformer to use as a final step when decoding the value of any field in the document
93
94
*/
94
95
public DocumentCodec (final CodecRegistry registry , final BsonTypeClassMap bsonTypeClassMap , final Transformer valueTransformer ) {
96
+ this (registry , new BsonTypeCodecMap (notNull ("bsonTypeClassMap" , bsonTypeClassMap ), registry ),
97
+ new ObjectIdGenerator (), valueTransformer , UuidRepresentation .JAVA_LEGACY );
98
+ }
99
+
100
+ private DocumentCodec (final CodecRegistry registry , final BsonTypeCodecMap bsonTypeCodecMap , final IdGenerator idGenerator ,
101
+ final Transformer valueTransformer , final UuidRepresentation uuidRepresentation ) {
95
102
this .registry = notNull ("registry" , registry );
96
- this .bsonTypeCodecMap = new BsonTypeCodecMap ( notNull ( "bsonTypeClassMap" , bsonTypeClassMap ), registry ) ;
97
- this .idGenerator = new ObjectIdGenerator () ;
103
+ this .bsonTypeCodecMap = bsonTypeCodecMap ;
104
+ this .idGenerator = idGenerator ;
98
105
this .valueTransformer = valueTransformer != null ? valueTransformer : new Transformer () {
99
106
@ Override
100
107
public Object transform (final Object value ) {
101
108
return value ;
102
109
}
103
110
};
111
+ this .uuidRepresentation = uuidRepresentation ;
112
+ }
113
+
114
+ @ Override
115
+ public Codec <Document > withUuidRepresentation (final UuidRepresentation uuidRepresentation ) {
116
+ return new DocumentCodec (registry , bsonTypeCodecMap , idGenerator , valueTransformer , uuidRepresentation );
104
117
}
105
118
106
119
@ Override
@@ -216,10 +229,29 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
216
229
return null ;
217
230
} else if (bsonType == BsonType .ARRAY ) {
218
231
return readList (reader , decoderContext );
219
- } else if (bsonType == BsonType .BINARY && BsonBinarySubType .isUuid (reader .peekBinarySubType ()) && reader .peekBinarySize () == 16 ) {
220
- return registry .get (UUID .class ).decode (reader , decoderContext );
232
+ } else {
233
+ Codec <?> codec = bsonTypeCodecMap .get (bsonType );
234
+
235
+ if (bsonType == BsonType .BINARY && reader .peekBinarySize () == 16 ) {
236
+ switch (reader .peekBinarySubType ()) {
237
+ case 3 :
238
+ if (uuidRepresentation == UuidRepresentation .JAVA_LEGACY
239
+ || uuidRepresentation == UuidRepresentation .C_SHARP_LEGACY
240
+ || uuidRepresentation == UuidRepresentation .PYTHON_LEGACY ) {
241
+ codec = registry .get (UUID .class );
242
+ }
243
+ break ;
244
+ case 4 :
245
+ if (uuidRepresentation == UuidRepresentation .JAVA_LEGACY || uuidRepresentation == UuidRepresentation .STANDARD ) {
246
+ codec = registry .get (UUID .class );
247
+ }
248
+ break ;
249
+ default :
250
+ break ;
251
+ }
252
+ }
253
+ return valueTransformer .transform (codec .decode (reader , decoderContext ));
221
254
}
222
- return valueTransformer .transform (bsonTypeCodecMap .get (bsonType ).decode (reader , decoderContext ));
223
255
}
224
256
225
257
private List <Object > readList (final BsonReader reader , final DecoderContext decoderContext ) {
0 commit comments