asolfre / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Map entries sorted by keys #466

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be great to be able to configure Gson to write all Map entries sorted 
by keys.

Especially in Unit Tests an equality assertion of two larger objects 
represented as their json strings is very neat because modern IDEs bring up the 
exact diff of the two, whereas classic Object.equals only returns true or false.

Original issue reported on code.google.com by th.kiesl...@gmail.com on 23 Aug 2012 at 12:51

GoogleCodeExporter commented 9 years ago
You could create a helper method that returns a JsonObject sorted by keys. It 
could even be recursive.

We don't want to make this change because some applications depend on 
preserving the order that keys and values are returned.

Original comment by limpbizkit on 2 Sep 2012 at 9:26

GoogleCodeExporter commented 9 years ago
I created a recursive method to sort JsonObjects. It works for Maps (and 
Objects). I don't see how it could sort Sets though.

Introducing another flag on the builder to trigger sorting of Maps and Sets 
would not change the behavior if its default was false.

Original comment by th.kiesl...@gmail.com on 11 Sep 2012 at 2:11

GoogleCodeExporter commented 9 years ago
I agree. I've worked with other json libraries in other languages (e.g., perl), 
and having the ability (optionally, of course) to sort by keys is very useful 
in some cases. I'm currently trying to do this myself, and I'm finding I have 
to write the code by hand.

Original comment by paul.cle...@steeleye.com on 18 Dec 2014 at 5:18