Jackson Objectmapper - Specify Serialization Order of Object Properties

Order of JSON objects using Jackson's ObjectMapper

@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" })
public class Relation { ... }

Jackson ObjectMapper - specify serialization order of object properties

From the Jackson Annotations documentation:

// ensure that "id" and "name" are output before other properties
@JsonPropertyOrder({ "id", "name" })

// order any properties that don't have explicit setting using alphabetic order
@JsonPropertyOrder(alphabetic=true)

Jackson ObjectMapper default property order

This post is still up-to-date. Default order depends from given implementation (aka version). You need to check implementation:

  • In com.fasterxml.jackson.databind.introspect package, e.g.: POJOPropertiesCollector class.
  • In com.fasterxml.jackson.databind.ser package, e.g.: BeanSerializer, BeanSerializerFactory classes.
  • Used annotations, e.g.: JsonIgnore.

Also it depends from:

  • Enabled features, e.g.: REQUIRE_SETTERS_FOR_GETTERS
  • Enabled modifiers, e.g.: com.fasterxml.jackson.databind.ser.BeanSerializerModifier class.

From other side:

An object is an unordered set of name/value pairs.

And you should not depend on it.

How can i specify the field serialize order on Jackson

You can use @JsonPropertyOrder annotation to set property order while serialization as shown below.

@JsonPropertyOrder({"name","age","code","time"})
public class Test extends BasicTest{
private String name;
private Integer age;

// ... Getter and Setter
}

How to set the order of json properties when sending a Map object

LinkedHashMap maintains insertion order:

Map<String, Object> response = new LinkedHashMap<>();

Jackson: Property Order and Inheritance

@JsonPropertyOrder can be used to give specific order in which the details of the data expected in the output json, so using this annotation changing the order is possible.

@JsonPropertyOrder is an annotation which can be used at class level. It takes the properties as a list of fields - these fields can be either in the parent class or in the child class that defines the order in which fields will appear in the string resulting from the object JSON serialization.

Reference:

Jackson JSON using @JsonPropertyOrder annotation to define properties serialization order

Serialising order annotations for XML on Jackson does not work

I cannot find a way to make it work with annotations. I needed to implement my custom serializer for the class just to change the order.

Jackson ObjectMapper: Can java.util.Set be serialized to JSON array in ascending sorted order?

A JSON Array is an ordered collection, exactly like a Java array is:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

However, ordered does not mean sorted. It just means that it retains the order of the given values:

  • If the Set is sorted (e.g. TreeSet), then the JSON array is sorted, according to the sort order of the Set:

    TreeSet: The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

  • If the Set is unordered (e.g. HashSet), then the JSON array is in the arbitrary order returned by the Set.

    HashSet: It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

This is unlike the fields of a JSON Object:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).



Related Topics



Leave a reply



Submit