From e5e1d206f1bf744cb0c962e2193462e1e874d865 Mon Sep 17 00:00:00 2001 From: amirlanesman Date: Sun, 23 Aug 2015 17:42:48 +0300 Subject: [PATCH 1/2] fixes reading Symbol objects from Mongo --- .gitignore | 1 + driver/src/main/com/mongodb/DBObjectCodec.java | 2 +- driver/src/main/com/mongodb/util/JSONSerializers.java | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index db82103..6c58243 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ codereview.rc docs/reference/public docs/landing/public docs/hugo* +/bin diff --git a/driver/src/main/com/mongodb/DBObjectCodec.java b/driver/src/main/com/mongodb/DBObjectCodec.java index 481be65..212ca02 100644 --- a/driver/src/main/com/mongodb/DBObjectCodec.java +++ b/driver/src/main/com/mongodb/DBObjectCodec.java @@ -72,7 +72,7 @@ public class DBObjectCodec implements CollectibleCodec { static BsonTypeClassMap createDefaultBsonTypeClassMap() { Map> replacements = new HashMap>(); replacements.put(BsonType.REGULAR_EXPRESSION, Pattern.class); - replacements.put(BsonType.SYMBOL, String.class); + replacements.put(BsonType.SYMBOL, Symbol.class); replacements.put(BsonType.TIMESTAMP, BSONTimestamp.class); return new BsonTypeClassMap(replacements); diff --git a/driver/src/main/com/mongodb/util/JSONSerializers.java b/driver/src/main/com/mongodb/util/JSONSerializers.java index c606939..461935d 100644 --- a/driver/src/main/com/mongodb/util/JSONSerializers.java +++ b/driver/src/main/com/mongodb/util/JSONSerializers.java @@ -20,6 +20,7 @@ import com.mongodb.BasicDBObject; import com.mongodb.Bytes; import com.mongodb.DBObject; import com.mongodb.DBRef; + import org.bson.BsonUndefined; import org.bson.types.BSONTimestamp; import org.bson.types.Binary; @@ -28,8 +29,10 @@ import org.bson.types.CodeWScope; import org.bson.types.MaxKey; import org.bson.types.MinKey; import org.bson.types.ObjectId; +import org.bson.types.Symbol; import javax.xml.bind.DatatypeConverter; + import java.lang.reflect.Array; import java.text.SimpleDateFormat; import java.util.Date; @@ -104,6 +107,7 @@ public class JSONSerializers { serializer.addObjectSerializer(ObjectId.class, new ObjectIdSerializer(serializer)); serializer.addObjectSerializer(Pattern.class, new PatternSerializer(serializer)); serializer.addObjectSerializer(String.class, new StringSerializer()); + serializer.addObjectSerializer(Symbol.class, new SymbolSerializer()); serializer.addObjectSerializer(UUID.class, new UuidSerializer(serializer)); serializer.addObjectSerializer(BsonUndefined.class, new UndefinedSerializer(serializer)); return serializer; @@ -389,6 +393,14 @@ public class JSONSerializers { } } + private static class SymbolSerializer extends AbstractObjectSerializer { + + @Override + public void serialize(final Object obj, final StringBuilder buf) { + JSON.string(buf, ((Symbol) obj).getSymbol()); + } + } + private static class UuidSerializer extends CompoundObjectSerializer { UuidSerializer(final ObjectSerializer serializer) { -- 2.3.2 (Apple Git-55) From 3639112b5540d950fa0e94e7868a7edbbe759c78 Mon Sep 17 00:00:00 2001 From: amirlanesman Date: Mon, 24 Aug 2015 15:56:20 +0300 Subject: [PATCH 2/2] adds test for Symbol handling for DBObjectCoded.decode --- .../test/functional/com/mongodb/DBObjectCodecTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/driver/src/test/functional/com/mongodb/DBObjectCodecTest.java b/driver/src/test/functional/com/mongodb/DBObjectCodecTest.java index dd053b4..2bbaadc 100644 --- a/driver/src/test/functional/com/mongodb/DBObjectCodecTest.java +++ b/driver/src/test/functional/com/mongodb/DBObjectCodecTest.java @@ -19,14 +19,18 @@ package com.mongodb; import org.bson.BSON; import org.bson.BsonArray; import org.bson.BsonDocument; +import org.bson.BsonDocumentReader; import org.bson.BsonDocumentWriter; import org.bson.BsonInt32; import org.bson.BsonNull; import org.bson.BsonObjectId; +import org.bson.BsonSymbol; import org.bson.LazyBSONCallback; import org.bson.Transformer; +import org.bson.codecs.DecoderContext; import org.bson.codecs.EncoderContext; import org.bson.codecs.ValueCodecProvider; +import org.bson.types.Symbol; import org.junit.Test; import java.util.ArrayList; @@ -194,6 +198,20 @@ public class DBObjectCodecTest extends DatabaseTestCase { assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument()); } + @Test + public void shouldDecodeSymbol() { + Symbol symbol = new Symbol("test-symbol"); + BsonSymbol symbolBson = new BsonSymbol("test-symbol"); + + DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider()))); + + BsonDocument doc = new BsonDocument("symbol", symbolBson); + + BsonDocumentReader reader = new BsonDocumentReader(doc); + + assertEquals(new BasicDBObject("symbol", symbol), dbObjectCodec.decode(reader, DecoderContext.builder().build())); + } + static class IterableMap extends HashMap implements Iterable { private static final long serialVersionUID = -5090421898469363392L; -- 2.3.2 (Apple Git-55)