Skip to content

Commit 93be1cf

Browse files
committed
When encoding a Document, encode all Iterable instances as BSON arrays, rather than just List instances. This is consistent with the behavior of the DBObject encoder,
so this will make it easier to migrate from DBObject to Document JAVA-1761
1 parent 180ada2 commit 93be1cf

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

bson/src/main/org/bson/codecs/DocumentCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ private boolean skipField(final EncoderContext encoderContext, final String key)
166166
private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) {
167167
if (value == null) {
168168
writer.writeNull();
169-
} else if (List.class.isAssignableFrom(value.getClass())) {
170-
writeList(writer, (List<Object>) value, encoderContext.getChildContext());
169+
} else if (Iterable.class.isAssignableFrom(value.getClass())) {
170+
writeIterable(writer, (Iterable<Object>) value, encoderContext.getChildContext());
171171
} else if (Map.class.isAssignableFrom(value.getClass())) {
172172
writeMap(writer, (Map<String, Object>) value, encoderContext.getChildContext());
173173
} else {
@@ -191,7 +191,7 @@ private void writeMap(final BsonWriter writer, final Map<String, Object> map, fi
191191
writer.writeEndDocument();
192192
}
193193

194-
private void writeList(final BsonWriter writer, final List<Object> list, final EncoderContext encoderContext) {
194+
private void writeIterable(final BsonWriter writer, final Iterable<Object> list, final EncoderContext encoderContext) {
195195
writer.writeStartArray();
196196
for (final Object value : list) {
197197
writeValue(writer, encoderContext, value);

bson/src/test/unit/org/bson/codecs/DocumentCodecTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.IOException;
4141
import java.nio.ByteBuffer;
4242
import java.util.Date;
43+
import java.util.HashSet;
4344
import java.util.List;
4445

4546
import static java.util.Arrays.asList;
@@ -90,14 +91,18 @@ public void testPrimitiveBSONTypeCodecs() throws IOException {
9091
@Test
9192
public void testIterableEncoding() throws IOException {
9293
DocumentCodec documentCodec = new DocumentCodec();
93-
Document doc = new Document();
94-
doc.put("array", asList(1, 2, 3, 4, 5));
94+
Document doc = new Document()
95+
.append("list", asList(1, 2, 3, 4, 5))
96+
.append("set", new HashSet<Integer>(asList(1, 2, 3, 4)));
9597

9698
documentCodec.encode(writer, doc, EncoderContext.builder().build());
9799

98100
BsonInput bsonInput = createInputBuffer();
99101
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
100-
assertEquals(doc, decodedDocument);
102+
assertEquals(new Document()
103+
.append("list", asList(1, 2, 3, 4, 5))
104+
.append("set", asList(1, 2, 3, 4)),
105+
decodedDocument);
101106
}
102107

103108
@Test

0 commit comments

Comments
 (0)