0

I'm using JsonSerializer to convert an Object to a JSON String but the order is wrong. The attributes are automatically sorted alphabetically but I want them in the order they have been listed in the class (e.g. "endPoint" should not appear before "startPoint").

When I call JsonSerializer. I only see 4 methods (toJava or toJson). I tried doing includes with the order but it doesn't work/I'm not doing it right. The way I'm doing it is like this:

return new String[]{"errorDescription", "searchResultRecord.billOfLadingNumber", 
"searchResultRecord.bookingNumber", "searchResultRecord.advancedManifest", 
"searchResultRecord.inboundCustomsClearanceStatus", "searchResultRecord.cargoReleaseStatus", "searchResultRecord.freightChargeReleaseStatus", 
"searchResultRecord.container.containerNumber", "searchResultRecord.container.latestEvent.event", 
"searchResultRecord.container.latestEvent.location", "searchResultRecord.container.latestEvent.time"};

As you can see there is obj.obj.attr parts being included. But when I try to run this code I only see "errorDescription" in the output string. I then tried "searchResultRecord" without attributes but it only showed 2 of the attributes in the output :x

Any idea how to fix the order so its not alphabetical? All examples online seem to be from an older version of the class with more accessible methods~ Thanks for any info.

1 Answers1

2

You can try using

@XmlType(propOrder={"startPoint", "endPoint"})

on the class you defined as @XmlRootElement. Those attributes and any other you want will sorted as you defined on propOrder.

Florent
  • 12,310
  • 10
  • 49
  • 58