FasterXML / FasterXML/jackson-databind

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

Aperta
#2,260 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
3.7k
Fork
1.5k
Merge medio
3g 6h
PR unite (30g)
28

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.