Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of JSON objects using Jackson's ObjectMapper

Tags:

java

json

jackson

I'm using ObjectMapper to do my java-json mapping.

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

this is my java class:

public class Relation {  private String id; private String source; private String target; private String label; private List<RelAttribute> 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<RelAttribute> attributes) {     this.attributes = attributes; }  public List<RelAttribute> getAttributes() {     return attributes; }  } 

this is what I get:

{     "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"   } 

So my question now is, how can I get this json output:

{     "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" : ""     } ]    } 

I want it with same order as in my java declaration. Is there a way to specify it ? Maybe with annotations or stuff like that ?

like image 541
justSaid Avatar asked Oct 09 '13 13:10

justSaid


People also ask

Does the order of JSON elements matter?

Any error or exception? The JSON RFC (RFC 4627) says that order of object members does not matter.

How do I read JSON file with ObjectMapper?

Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);

How do I change the order of fields in JSON?

You cannot change the order if you are using the JSON function. It will always be alphabetical.

What is JSON property order?

The @JsonPropertyOrder is an annotation to be used at the class-level. It takes as property a list of fields that defines the order in which fields can appear in the string resulting from the object JSON serialization.


2 Answers

@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" }) public class Relation { ... } 
like image 185
Andrey Atapin Avatar answered Sep 21 '22 04:09

Andrey Atapin


Do you know there is a convenient way to specify alphabetic ordering?

@JsonPropertyOrder(alphabetic = true) public class Relation { ... } 

If you have specific requirements, here how you configure custom ordering:

@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" }) public class Relation { ... } 
like image 34
naXa Avatar answered Sep 18 '22 04:09

naXa