1

Let's take this example. I have a pojo class as below.

public class MyRecord{
    private String name;
    private String id;

    //constructors and getters,setters
}

when I get the toJson(new MyRecord("MyName","myId") output for above I can get.

{
  "name": "MyName",
  "id": "123" 
}

And I have inherited one as follows to add the dateTime.

public class MyRecordWithDateTime extends MyRecord{
        private String DateTime;           

        //constructors and getters,setters
}

so when I called toJson(new MyRecordWithDateTime("2016-01-01", "MyName", "myId"))

The output is this

{
  "name": "MyName",
  "id": "123",
  "dateTime": "2016-01-01" 
}

but I actually need that as follows. (dateTime should come first.)

{          
  "dateTime": "2016-01-01", 
  "name": "MyName",
  "id": "123"
}

Is there anyway to do that with keeping inheritance?

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 1
    Object fields/members do not have an order (nor should they). If you need members ordered you should use a list/array structure – Richard H Aug 10 '16 at 07:34
  • @RichardH if you have looked closely Json.toJson() extracts fields by getters and exactly follows the order of getters. i.e. If you skip a getter for some field, it will not appear. The reason I asked this is, I wonder whether there are any annotation at least. – Supun Wijerathne Aug 10 '16 at 07:38
  • Relying on the order in which the getters are written in your class is terrible design. You'd be better off calling the getters yourself in the order you want – Richard H Aug 10 '16 at 07:41
  • But that's how JSON.toJson() works. It's the library implementation and I cannot do anything about that. The problem about changing the order is "How can I do that for an inherited class?" as in my example. It extracts the parent class getters first. – Supun Wijerathne Aug 10 '16 at 07:46
  • But that is my point: class fields do not have an order. If you want fields or values ordered you need to use a data structure that has an order, i.e an array, list or ordered hash of some sort. – Richard H Aug 10 '16 at 07:59
  • may i ask why you need this ? just curious – Yazan Aug 10 '16 at 08:08
  • @Yazan its just for presentation purpose. Indeed there is a front-end client who can work with this json, without worrying about the order. But if we refer the rest service directly anyway, the order I want, is pretty clear to understand. :) – Supun Wijerathne Aug 10 '16 at 08:42

3 Answers3

3

Maybe kinda late but just in case there is this annotation @JsonPropertyOrder

RoCaSo
  • 41
  • 3
1

Before you put it in your JSON file, you can try to make an ordered List ( LinkedList, ArrayList or something like), then sort it as you want and after that put it in JSON.

But obviously, a better idea than mine exists!!

But the fact is: JSON doesn't need to be sorted! You just use the getter and it will find your value associated to the key! Even if it's in the last position.

Souin
  • 94
  • 1
  • 11
  • I can't get you. I want to change the order of fields?? – Supun Wijerathne Aug 10 '16 at 07:34
  • 1
    Just use a list in your constroctor. Something like : `private LinkedList values = new LinkedList(); ` Object is an example. Here it's a String – Souin Aug 10 '16 at 07:37
  • I can use a List as you propose, but that changes the response format in terms of indentation right? – Supun Wijerathne Aug 10 '16 at 07:40
  • 1
    Imagine you have you LinkedHashMap (name : linkHM) sorted and completed properly : `JSONObject jo = new JSONObject(); for (Map.Entry elem : linkHM.entrySet()) { jo.put(elem.getKey(),elem.getValue()); }` – Souin Aug 10 '16 at 07:44
  • But to do that, I should completely move from object nature right? which is some what conflicting. – Supun Wijerathne Aug 10 '16 at 08:48
  • I can't understand what you said. I didn't tried what I said, but I think it could work. The order: (1)you initialize the LinkedHashMap, (2)you put the elements in the order ( with name as the key, and the value as a string (or not, Json preserves the type)) (3) you create the JSONObject with the for statement I posted above, and (4) your write the JSONObject into the file! – Souin Aug 10 '16 at 09:23
1

Field/member/attributes in a JSON collection do not have an order, and as far as this JSON data structure is concerned, the "order" doesn't matter.

The only reason I can imagine you are concerned with the order is for printing/presentation purposes. In that case I suggest you manually construct the JSON string yourself.

Richard H
  • 38,037
  • 37
  • 111
  • 138
  • So does your answer implicitly suggest that ->"Don't worry about the order of keys, at all when you creating JSON responses" ??? – Supun Wijerathne Aug 10 '16 at 08:44
  • 1
    Correct. JSON is just a string representation of a javascript object. The order of keys in a javascript object absolutely does not matter - indeed there is no order defined. If you want to print out a JSON string with the keys in a particular order you'll have to implement this yourself instead of relying on a library method. – Richard H Aug 10 '16 at 11:58