Discovered something fun while testing my JSON representation and Oracle Java 7

284 views
Skip to first unread message

Glenn McAllister

unread,
Mar 4, 2013, 9:51:24 AM3/4/13
to dropwiz...@googlegroups.com
I've just started using dropwizard, so this may already be well known to the community; my apologies if I'm repeating common wisdom.

I started testing my json representation using the .asJson(), .fromJson(), and .jsonFixture() methods. Pretty straightforward, but nothing was working. It took me a little while to notice that the generated json was always putting the properties in random order. And that's when I remembered the pain our team just went through recently when we had to fix a bunch of badly written unit tests when we moved from Oracle JRE 1.6 to 1.7, where the test writer assumed that things put into a collection would come out of that collection in the same order, even though the interface does not guarantee that. In JRE 1.7 they really do randomize the iteration order.

The way I got around the problem (there are probably other, better ways, but I'm new to Jackson) was to use @JsonPropertyOrder to explicitly state the order in which I wanted the properties to be written out. This ensured my tests would pass because the normalized strings produced by the JsonHelpers methods now matched the stable order produced by Jackson.

At any rate, I wanted to put this out there in case any other newbie was caught scratching their head for a while like I was.

Glenn McAllister

Dan Everton

unread,
Mar 4, 2013, 4:45:15 PM3/4/13
to dropwiz...@googlegroups.com
You can also modify the object mapper to sort properties alphabetically. Something like

    bootstrap.getObjectMapperFactory().enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);

Should result in all JSON output with keys in alphabetical order.

You might also need

    bootstrap.getObjectMapperFactory().enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);

To ensure that maps come out ordered as well.

Cheers,

Dan

Glenn McAllister

unread,
Mar 5, 2013, 10:53:23 AM3/5/13
to dropwiz...@googlegroups.com
I'll give that a shot and see if that simplifies things, thanks.

Glenn


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages