salat / salat

Salat is a simple serialization library for case classes.
https://github.com/salat/salat
Other
483 stars 106 forks source link

Case class not serialising in order #208

Open shrikantjoveo opened 7 years ago

shrikantjoveo commented 7 years ago

I have a case class case class A(a: Int, b: Int)

I m seeing sometimes that the resulting serialized value is {a:1,b:1} and sometimes {b:1,a:1}

Does salat provide any guarantees as to the order of serialization in a case class? Can this be done?

noahlz commented 7 years ago

Are you using salat for serialization into mongo, or for generating JSON from case classes?

In either case, I'm not aware of a guarantee that order of field declaration is preserved.

Might I ask what problem this is causing for you?

shrikantjoveo commented 7 years ago

We are using salat for serialization of a case class which is used as a compound key for inserting document into a Mongo collection. Since, the order of field declaration is not preserved, Multiple documents are getting inserted into Mongo Collection with same value for Compound Key.

noahlz commented 7 years ago

This is somewhat related to #110

Field order preservation has not been a guarantee of Salat, and to be fair it seems that at the time Salat was first written Mongo itself did not support it. Salat itself does not rely on ordinal fields in the BSON serialized form, as it uses field names to map values from the DBObject to the case class.

You are welcome to dig around in Mongo source and propose a fix. Since Salat is a community/volunteer project, I can't offer a turn-around time for resolving this otherwise.

I suggest that the Context needs a new configuration option for serialization, something like "ordinal fields = true." I would prefer this because of the potential overhead of maintaining field order (it might not be that big, of course).