Jackson-databind: `@JsonPropertyOrder(alphabetic = true)` does not include `@JsonAnyGetter`

Created on 7 Aug 2014  路  6Comments  路  Source: FasterXML/jackson-databind

The JsonPropertyOrder annotation does only sort the explicitly defined properties of an object, additional properties which may be stored using the JsonAnySetter/JsonAnyGetter annotations are not taken into account and just appended at the end.

This is not a bug as the order of properties in a JSON object should never be of any importance but when dumping objects into logs or diffing JSON outputs in unit tests it would be nice if the ordering would be alphabetic for all properties.

(if a TreeMap is used for the additionalProperties at least they for themselves are orderd alphabetic too)

3.x

Most helpful comment

+1 for this also, however would like to be able to specify the property order explicitly (rather than alphabetic). Would really like to specify the location of the @JsonAnyGetter property and have all of it's fields all serialised in that location. eg.

@JsonPropertyOrder({ "propFirst", "propJsonAnyGetter", "propLast" })

All 6 comments

Hmmmh. True, sorting does not apply there, nor is it easy to implement the way sorting is implemented, as it applies to property accessor objects (name of which are known statically), and "any getter" applies to fully dynamic names.
Regardless, I can see why it would be good for sorting to apply to any-getters as well. Just need to think of a good way to do that.

+1 for this. Would also be happy to have the default behavior for anygetter either be alphabetical or have an option for that.

Adding sorting just for any getter (without merging with 'non-any' properties) could be simpler first step. Although not sure if it'd be valuable without the other part.

+1 for this.

+1 for this also, however would like to be able to specify the property order explicitly (rather than alphabetic). Would really like to specify the location of the @JsonAnyGetter property and have all of it's fields all serialised in that location. eg.

@JsonPropertyOrder({ "propFirst", "propJsonAnyGetter", "propLast" })

Dumb work-around if it fits your needs: You can use a TreeMap - the default comparator sorts keys lexicographically. You can use a custom comparator to sort keys differently.

Was this page helpful?
0 / 5 - 0 ratings