How to use the new Codec Infrastructure

731 views
Skip to first unread message

Guillaume Forget

unread,
Sep 7, 2015, 12:49:24 PM9/7/15
to mongodb-user
Hello,

Now that I have upgraded to the latest version of the Java driver, I'm trying to understand the new Codec infrastructure. More specifically, I'm trying to figure out what I need to do to leverage it and make the reading/writing of custom classes more seamless. Do you have any code sample that show how to do that? I found 2 examples  so far, they're both very similar and both seem wrong to me:

What seems wrong is that the DocumentCodec which is used internally is always initialized with the default codecs. I would think there should be a way to retrieve the DocumentCodec that is registered and configured in the CodecRegistry rather than building a new one. Am I wrong? 

Thank you

Ross Lawley

unread,
Sep 8, 2015, 9:08:20 AM9/8/15
to mongod...@googlegroups.com
Hi Guillaume,

Have you read the Codecs guide in the documentation:   http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/

Depending on how you have configured your CodecRegistry - there may or may not be a Codec for Document.  It is possible to check by calling codecRegistry.get(Class<Document>) and that would save initialising a new one, but if a custom registry was used a Codec might not be available.

I hope that provides some insight!

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/6585ef98-c624-4b33-9133-e956c6fb38a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Guillaume Forget

unread,
Sep 9, 2015, 9:14:46 AM9/9/15
to mongodb-user
Hi Ross,

Thanks for answering. Yes I read this documentation but it doesn't address the how-to in my opinion. Basically my question is the following:
- say I have a class MyCoolObject
- I want to persist MyCoolObject in a MongoDB collection
- what are the pieces that I have to implement to leverage the new Codec infrastructure and how do I wire it all

The 2 (similar) implementations that I have found so far seem incorrect to me because they write a custom codec which wraps their own instance of a DocumentCodec. So yes, using the documentation you pointed me at, I can "fix" their code to avoid doing that. Basically write a CodecProvider so it has access to the CodecRegistry and use that to retrieve the DocumentCodec at some point or another. But I was interested to see if there was already a "preferred" approach to do this rather than me trying to figure it all out by myself.

I hope this clarifies my question

Ross Lawley

unread,
Sep 22, 2015, 6:39:24 AM9/22/15
to mongodb-user
Hi Guillaume,

No problems, its good to get feedback regarding the documentation.  Given you have a class and you know what fields and their types you can use BsonReader and BsonWriters directly in your codec.  This will be more efficient as you don't need any intermediate representation.

In the documentation the BsonReader and Writers are covered and the IntegerCodec is a exceptionally trivial example of using them directly.  However, there are some examples in the test code of the driver that provide a fuller example to follow:

 * Worker

I've added a documentation ticket: JAVA-1977 to track adding these classes to the documentation as a complete example of using BsonReader/Writers and Codecs together.  Please click the "watch" link to track the ticket and updates.

In the meantime, I hope these examples will help you create your own codecs.

All the best,

Ross

Guillaume Forget

unread,
Sep 23, 2015, 9:39:24 AM9/23/15
to mongodb-user
Thank you Ross, that is exactly what I was looking for!

Cheers

Guillaume
Reply all
Reply to author
Forward
0 new messages