Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BsonReader & BsonWriter #2

Closed
zigzago opened this issue Sep 22, 2019 · 6 comments
Closed

Use BsonReader & BsonWriter #2

zigzago opened this issue Sep 22, 2019 · 6 comments

Comments

@zigzago
Copy link
Collaborator

zigzago commented Sep 22, 2019

Hello,

Trying to implement a first version of org.bson.codecs.Codec ( Litote/kmongo@8b03c13#diff-43a261a5b319c5b393c2428d036ce9f4R39 ), I hit a first issue.

encode & decode methods of Codec use BsonWriter & BsonReader but kbson takes BsonDocumentWriter & BsonDocument

Is it doable to change Encoder and Decoder input type?

Thank you

@jershell
Copy link
Owner

jershell commented Sep 23, 2019

Hello,
I will check and try to change it

@jershell
Copy link
Owner

Hello. I finished.
I added BsonDecoder(with BsonReader) and related method the load()
I changed type in encoder on BsonWriter
The method parse() continue to use BsonDocumentDecoder(i renamed to avoid confuse)
The BsonDecoder required strict order of a fields

@jershell
Copy link
Owner

Litote/kmongo#104

@zigzago
Copy link
Collaborator Author

zigzago commented Sep 29, 2019

@jershell The strict order of fields during deserialization is quite problematic :(

For example, this class does not deserialize well:

@Serializable
data class Friend(
    var name: String?,
    val address: String?,
    @NonEncodeNull
    @ContextualSerialization
    val _id: ObjectId? = null)

because in the bson coming from Mongo, the _id comes always first.

But this one is ok (of course):

data class Friend2(
    @NonEncodeNull
    @ContextualSerialization
    val _id: ObjectId? = null,
    var name: String?,
    val address: String?)

For the other fields, Mongo returns the fields in the insertion order, so it works. But imagine you insert a document using the shell in a different field order... The deserialization then is also broken.

I would suggest a mechanism similar to the class of the mongo java driver org.bson.codecs.pojo.PojoCodecImpl:

  • Calculate at startup the set of properties to deserialize for the class

  • For each deserialization, use a temporary map where you store the field values

  • When you read a property during deserialization:

    1. If the property is in the set of properties to deserialize? If not: skip completely the value (perf improvement)
    2. if yes, set the value in the map
  • At the end of deserialization, create the instance with the map

@jershell
Copy link
Owner

@zigzago
I changed BsonDocumentDecoder on work with BsonReader too. It can work with another an order of fields

@zigzago
Copy link
Collaborator Author

zigzago commented Oct 5, 2019

Thank you! It works fine as you can see: https://github.com/Litote/kmongo/compare/serialization?expand=1

I hit several other issues: #3 #4 #5, the polymorphic one is the biggest - if you fix #3, I think I will be able to release a first version :)

@zigzago zigzago closed this as completed Oct 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants