Java – Does deserialization of Jackson to JsonNode guarantee property ordering?

Does deserialization of Jackson to JsonNode guarantee property ordering?… here is a solution to the problem.

Does deserialization of Jackson to JsonNode guarantee property ordering?

For example, if I deserialize the following JSON to JsonNode:

{
   "property1": 1,
   "property2": 2,
   "property3": 3
}

Then JsonNode#fields iterates through elements, is there a guarantee that the iterator will return properties in the defined order (i.e. property1, property2, property3)?

Intuitively, I think the answer is no, because the JSON specification defines an object as “an unordered set of name/value pairs.” However, JSON RFC (RFC 7159) says so

JSON parsing libraries have been observed to differ as to whether or
not they make the ordering of object members visible to calling
software.

And I haven’t found any information on how Jackson handled this issue.

Solution

Look at source code of Jackson’s com.fasterxml.jackson.databind.node.ObjectNode (version 2.5.0, you can search for different versions on the website), the map type for saving object children is where the LinkedHashMap documentation says

This implementation differs from HashMap in that it maintains a
doubly-linked list running through all of its entries. This linked
list defines the iteration ordering, which is normally the order in
which keys were inserted into the map (insertion-order).

Related Problems and Solutions