When I convert my java class into jsonobject it reshuffles the order of class members

117 Views Asked by At

I am using objectmapper to convert the class into json. Following is the code:

JSONParser parser = new JSONParser();
ObjectMapper mapper = new ObjectMapper();
String obj = mapper.writeValueAsString(myClass);
JSONObject jsonObject = (JSONObject)parser.perse(obj);

now line number 4 where I get the jsobObject reshuffles the order of class member. See the attached image, before it converting to jsonobject "authorisation" was first element then signatries. enter image description here But at line 4 it becomes 2nd ..... why ? I want order to be preserved, that is the requirement of my project. Please help !

1

There are 1 best solutions below

1
Tom On

You can configure the ordering this way :

@JsonPropertyOrder({ "authorisation", "signatoryDetails", "requestedLimits" })
public class MyClass { ... }