Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Jackson Deserialization into a JsonNode Guarantee Property Ordering?

For example, if I deserialize the following JSON into a JsonNode:

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

and then traverse the elements with JsonNode#fields, is there any guarantee that the iterator will return the properties in the order they were defined (i.e. property1, property2, property3)?

Intuitively I would assume the answer is no, because the JSON spec defines objects as "an unordered set of name/value pairs". HOWEVER, JSON RFC (RFC 7159) says this:

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 been able to find any information about how Jackson handles this.

like image 989
mark.monteiro Avatar asked Apr 17 '26 20:04

mark.monteiro


1 Answers

looking at the source code of Jackson's com.fasterxml.jackson.databind.node.ObjectNode (version 2.5.0, you can search on the site for a different one), the map which holds the object's children is of type LinkedHashMap where the 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).

like image 144
Sharon Ben Asher Avatar answered Apr 19 '26 10:04

Sharon Ben Asher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!