I\'m using Jackson 2.2.3 and need to convert a JsonNode tree into a string with sorted field keys. It\'s completely unclear to me how to do this, especially since the opposi
This is the easiest way to do it, as provided by one of Jackson's authors. There's currently no way to go straight from JsonNode
to String
with sorted keys.
private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}
private String convertNode(final JsonNode node) throws JsonProcessingException {
final Object obj = SORTED_MAPPER.treeToValue(node, Object.class);
final String json = SORTED_MAPPER.writeValueAsString(obj);
return json;
}