[JAVA-1358] Support encoding embedded Map within Document Created: 08/Aug/14  Updated: 31/Mar/15  Resolved: 10/Dec/14

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: None
Fix Version/s: 3.0.0

Type: Improvement Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Given

Map myJson = ... 
Document doc = new Document(myJson)

and myJson contains a value which is itself a Map, encoding fails:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.util.LinkedHashMap.
    at org.bson.codecs.configuration.RootCodecRegistry.get(RootCodecRegistry.java:74)
    at org.bson.codecs.configuration.RootCodecRegistry.get(RootCodecRegistry.java:60)
    at com.mongodb.codecs.DocumentCodec.writeValue(DocumentCodec.java:168)
    at com.mongodb.codecs.DocumentCodec.encode(DocumentCodec.java:127)
    at com.mongodb.codecs.DocumentCodec.encode(DocumentCodec.java:44)
    at com.mongodb.protocol.message.RequestMessage.addDocument(RequestMessage.java:111)
    at com.mongodb.protocol.message.RequestMessage.addCollectibleDocument(RequestMessage.java:102)



 Comments   
Comment by Jeffrey Yemin [ 31/Mar/15 ]

Closing all resolved 3.0.0 issues, as 3.0.0 has been tagged and released.

Comment by Githook User [ 30/Jan/15 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: DocumentCodec now encodes and decodes List and Map values

JAVA-1358
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/1cea194264dc9e52f95f8ce0792b52ca455ce0a1

Comment by Githook User [ 10/Dec/14 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: DocumentCodec now encodes and decodes List and Map values

JAVA-1358
Branch: 3.0.x
https://github.com/mongodb/mongo-java-driver/commit/1cea194264dc9e52f95f8ce0792b52ca455ce0a1

Comment by Jeffrey Yemin [ 13/Aug/14 ]

I think we should just do this, as there is no easy workaround

Comment by Tim Fox [ 13/Aug/14 ]

I am still struggling with this...

Most popular Java JSON libraries represent JSON as Maps. When people are using the MongoDB async Java API it's highly likely that they're going to have their JSON document in the form of a Map and they'll need to convert this to a MongoDB Document in order to use the async Java API, so this should be as simple as possible.

Right now I can see no easy way of doing this.

In more detail:

It works for a simple non nested Map:
this represents the json:

{foo: "bar"}

Map<String, Object> map = new HashMap();
map.put("foo", "bar");
Document doc = new Document(map);

^^ This works

But for a nested Map
e.g. this represents the json: {nested: {foo:"bar"}}

Map<String, Object> map = new HashMap();
Map<String, Object> nested = new HashMap();
nested.put("foo", "bar");
map.put("nested", nested);
Document doc = new Document(map);

^^ This fails

What is the correct way (currently) of converting Json in Map form into a document?

Cheers

Generated at Thu Feb 08 08:54:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.