FasterXML / FasterXML/jackson-databind

Make it possible to use `@JsonPropertyOrder` to order properties last (not just first)

Open
#2,260 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

While using `@JsonPropertyOrder` works for many cases, in one scenario, where I need to unwrap `Map`, (since `@JsonUnwrapped` doesn't work), leveraging `@JsonAnyGetter` does the trick. The side effect is that property ordering doesn't work.

If we could put a set of properties at the bottom perhaps with a `tail` option:

```
@JsonPropertyOrder(value= {"attributes", "resource", "resources", "embedded"}, tail={"links", "templates", "metadata" })
```
This would make it possible to serialize every attribute of the "attributes" map BEFORE serializing the `links`, `templates`, and `metadata`.

An alternative would be to make the `@JsonAnyGetter` still obey ordering from the "named" attribute, but I don't know if that is flat out impossible.

The original object is this one (with boilerplate removed for this issue), used to serialize various formats. The latest addition is the "attributes" `Map` at the top which can't be unwrapped:

```
@JsonPropertyOrder({ "attributes", "resource", "resources", "embedded", "links", "templates", "metadata" })
public class HalFormsDocument {

@JsonInclude(Include.NON_NULL) //
private Map attributes;

@JsonUnwrapped //
@JsonInclude(Include.NON_NULL) //
private T resource;

@JsonInclude(Include.NON_EMPTY) @JsonIgnore //
private Collection resources;

@JsonProperty("_embedded") //
@JsonInclude(Include.NON_EMPTY) //
private Map embedded;

@JsonProperty("page") //
@JsonInclude(Include.NON_NULL) //
private PagedResources.PageMetadata pageMetadata;

@JsonProperty("_links") //
@JsonInclude(Include.NON_EMPTY) //
@JsonSerialize(using = HalLinkListSerializer.class) //
@JsonDeserialize(using = HalFormsLinksDeserializer.class) //
private Links links;

@JsonProperty("_templates") //
@JsonInclude(Include.NON_EMPTY) //
private Map templates;

...
}
```
My test case produces this:

```
{
"_links" : {
"self" : {
"href" : "/employees/1"
}
},
"_templates" : {
"default" : {
"title" : null,
"method" : "post",
"contentType" : "",
"properties" : [ {
"name" : "name",
"required" : true
} ]
}
},
"name" : "Frodo Baggins"
}
```

instead of...

```
{
"name" : "Frodo Baggins",
"_links" : {
"self" : {
"href" : "/employees/1"
}
},
"_templates" : {
"default" : {
"title" : null,
"method" : "post",
"contentType" : "",
"properties" : [ {
"name" : "name",
"required" : true
} ]
}
}
}
```

While technically equivalent the former is much harder to read than the latter.

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.