FasterXML / FasterXML/jackson-databind

Support for Array Sorting during Deserialization

Open
#4,124 16 comments 0 reactions 0 assignees View on GitHub
3.x
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

Let's assume that we want to deserialize the following object, and we want `list` to be treated as immutable:

```java
public class MyObject {
private String name;
private List list;

@JsonCreator
public MyObject(
@JsonProperty("name")
String name,
@JsonProperty("list")
List list
) {
this.name = name;
this.list = list.stream().sorted().toList();
}

// getters and setters
}
```

However, it would be very useful to be able to **sort** the `list` during deserialization (often, we do not deem the other end of the communication trustworthy enough to deliver properly sorted lists).

### Describe the solution you'd like

I would like to see Jackson databinding support for array sorting. When Jackson deserializes the JSON array and converts it to a `List`, the list is sorted **before** it is passed to the POJO constructor. This could be controlled via an annotation:

```java
public @interface JsonSort {

public enum Order {
ASCENDING, DESCENDING
}

/** The sort order to apply to the array. */
public JsonSort.Order order() default Order.ASCENDING;

/** The comparator class to use. By default, Comparators.naturalOrder() will be used. Must implement Comparator. Must have a default constructor. */
public Class comparatorClass() default void.class;

/** Defines whether outgoinglists should be sorted before being converted to JSON. */
public boolean sortDuringSerialization() default true;

/** Defines whether incoming lists should be sorted before being passed to the JSON creator. */
public boolean sortDuringDeserialization() default true;

}
```

### Usage example

Minimal usage (note that we no longer need a `@JsonCreator` at all):

```java
public class MyObject {
@JsonProperty("name")
private String name;
@JsonSort
@JsonProperty("list")
private List list;

// getters and setters
}
```

Advanced usage:

```java
public class MyObject {
@JsonProperty("name")
private String name;
@JsonSort(
order = Order.ASCENDING,
comparator = ChildObjectCoparator::class,
sortDuringSerialization = true,
sortDuringDeserialization = true,
)
@JsonProperty("list")
private List list;

// getters and setters
}
```

### Additional context

For Java developers, this would be a quality of life feature. For Kotlin developers, it would be an even bigger deal because the workaround to have the field type as `List` while also being able to sort the incoming data is rather ugly:

```kotlin
class MyObject(
val name: String,
list: List, // the constructor parameter is a list, but note that it's not a "val" here!
) {

val list: List // here we define the immutable list which is sorted

init {
// here we take the constructor argument list, create a sorted immutable copy and assign it to the field
this.list = list.sorted()
}

}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.