After upgrading from 2.10.4 to 2.12.1 I noticed that the serialization property order has changed when serializing POJOs to JSON which are annotated with JsonProperty including an index.
This is a new feature that was added with
#2555
and is definitely a very welcome addition.
However, in case a JsonPropertyOrder(alphabetic=true) annotation is present on type level I would expect that the properties are still ordered alphabetically even though Properties with indices defined are present.
The reason is that we do not only serialize our objects to JSON but to Protobuf as well, which forces us to define indices on properties as this is required by the Protobuf serializer.
The way this feature is implemented now makes it impossible (unless there is a workaround) to support serialization to JSON and Protobuf while using alphabetic ordering for JSON format.
Since JsonPropertyOrder is defined on a type level and explicit ordering using property names still has precedence over property indices I'm wondering why this is not the case for alphabetic ordering - when I use this annotation I clearly state my intention just as if I would explicitly define the property order by name.
On the other hand, if the JsonPropertyOrder annotation is absent, using the defined indices as default order makes a lot of sense, because otherwise the order would not be clearly defined at all.
However, to improve compatibility it might even be better to add an additional property (e.g. JsonPropertyOrder(index=true)) - in this way an absent annotation would default to the legacy behavior (no well defined order) and if the annotation is present, the intended order is always explicitly defined, making it possible to extend the ordering strategy with additional options in the future.
After upgrading from 2.10.4 to 2.12.1 I noticed that the serialization property order has changed when serializing POJOs to JSON which are annotated with JsonProperty including an index.
This is a new feature that was added with
#2555
and is definitely a very welcome addition.
However, in case a JsonPropertyOrder(alphabetic=true) annotation is present on type level I would expect that the properties are still ordered alphabetically even though Properties with indices defined are present.
The reason is that we do not only serialize our objects to JSON but to Protobuf as well, which forces us to define indices on properties as this is required by the Protobuf serializer.
The way this feature is implemented now makes it impossible (unless there is a workaround) to support serialization to JSON and Protobuf while using alphabetic ordering for JSON format.
Since JsonPropertyOrder is defined on a type level and explicit ordering using property names still has precedence over property indices I'm wondering why this is not the case for alphabetic ordering - when I use this annotation I clearly state my intention just as if I would explicitly define the property order by name.
On the other hand, if the JsonPropertyOrder annotation is absent, using the defined indices as default order makes a lot of sense, because otherwise the order would not be clearly defined at all.
However, to improve compatibility it might even be better to add an additional property (e.g. JsonPropertyOrder(index=true)) - in this way an absent annotation would default to the legacy behavior (no well defined order) and if the annotation is present, the intended order is always explicitly defined, making it possible to extend the ordering strategy with additional options in the future.