117 votes

Ordre des objets JSON en utilisant l'ObjectMapper de Jackson

Je suis en train d'utiliser ObjectMapper pour faire la correspondance java-json.

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);

voici ma classe java:

public class Relation {

private String id;
private String source;
private String target;
private String label;
private List attributes;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public String getTarget() {
    return target;
}

public void setTarget(String target) {
    this.target = target;
}

public String getLabel() {
    return label;
}
public void setLabel(String label) {
    this.label = label;
}

public void setAttributes(List attributes) {
    this.attributes = attributes;
}

public List getAttributes() {
    return attributes;
}

}

voici ce que j'obtiens :

{
    "id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
    "attributes" : [ {
      "attrName" : "ID",
      "attrValue" : ""
    }, {
      "attrName" : "Description",
      "attrValue" : "Primary Actor"
    }, {
      "attrName" : "Status",
      "attrValue" : ""
    } ],
    "label" : "new Label",
    "target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
    "source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0"
  }

Donc ma question maintenant est, comment puis-je obtenir cette sortie json :

{
    "id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
    "label" : "new Label",
    "target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
    "source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0",
    "attributes" : [ {
      "attrName" : "ID",
      "attrValue" : ""
    }, {
      "attrName" : "Description",
      "attrValue" : "Primary Actor"
    }, {
      "attrName" : "Status",
      "attrValue" : ""
    } ]

  }

Je le veux dans le même ordre que dans ma déclaration java. Y a-t-il un moyen de le spécifier ? Peut-être avec des annotations ou des choses comme ça ?

0voto

Praj Points 71

Conformément à cette documentation, vous pouvez configurer Jackson2ObjectMapperBuilder globalement. Cette classe est disponible dans la dépendance spring-web.

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
      builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
      return builder;
}

De plus, vous pouvez utiliser @JsonProperty(index) pour déterminer l'ordre dans les classes héritées également.

class animal {
    @JsonProperty(index=2)
    int p1;
    @JsonProperty(index=3)
    int p2;
}

class cat extends animal{
    @JsonProperty(index=1)
    int p3;
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X