FasterXML / FasterXML/jackson-databind

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

Offen
#2,260 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
3.7k
Forks
1.5k
Ø Merge
3 T. 6 Std.
Gemergte PRs (30 T.)
28

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.