mailru / easyjson

Fast JSON serializer for golang.
MIT License
4.48k stars 421 forks source link

Unstable JSON when struct contains a map #292

Open horgh opened 4 years ago

horgh commented 4 years ago

Hi!

I have a use case where I need a stable JSON representation of a struct that contains a map[string]string as one of its fields. I found that when I have easyjson generated code for the struct, the JSON changes non deterministically as the keys end up in differing orders. encoding/json on the other hand states that it orders the keys and ends up being stable.

I have a program demonstrating this here: https://github.com/horgh/easyjson-unstable

Would this be something that we could change in easyjson? I realise it could perhaps have performance implications, though it might be nice to match encoding/json in this regard.

Thank you!

GoWebProd commented 4 years ago

Hello. Unfortunately, this is not so simple and will greatly affect performance. I think that can make this functionality optional if it really is in great need.

horgh commented 4 years ago

I see. Yeah, it sounds like an option would be the way to go if it was added then! I have a workaround where I create a different type and then marshal that, so it isn't insurmountable as is.

Perhaps documenting this in the limitations part of the README would be sufficient? I'm kind of loathe to add toggles unless they are very critical :-).

Thank you!