September 10, 2018

How to Sort Java-generated JSON

I was looking for a way to sort JSON by key. Why? It's easier to read and locate if you know it's order is consistent. I'm using Jackson as my parser, in Spring boot. There are several ways I'm able to see to accomplish this:
  • Spring has an application.properties/application.yml setting -- http.mappers.json-sort-keys -- which does not work with Jackson.
  • Jackson's ObjectMapper may be configured to sort keys using the ORDER_MAP_ENTRIES_BY_KEYS property. This worked, but there has got to be an easier way to dot this, which is...
  • Java itself has a TreeMap, whose default key comparator is lexical, meaning that you get this functionality out of the box. This is the approach I ended up using.

1 comment:

  1. Seminole Hard Rock Hotel Casino & Spa, Murphy
    Find 포항 출장마사지 deals and phone 순천 출장마사지 #'s 영천 출장샵 for Seminole Hard Rock Hotel Casino & Spa in Murphy, 여주 출장안마 NC. Find reviews and discounts 거제 출장안마 for AAA/AARP members, seniors,

    ReplyDelete