2

I have a class that represents a set of attributes, but when serializing it I noticed these were re-ordered alphabetically. On investigation, this was because of how the attributes were being held in memory.

Class declaration:

enter image description here

Objects in memory, before any serialization is done:

enter image description here

Is this a "feature" of android studio running in Debug, or is something else going on?

AntG
  • 1,291
  • 2
  • 19
  • 29
  • It seems like you're using GSON for serialization; in that case, see [this answer](https://stackoverflow.com/questions/6365851/how-to-keep-fields-sequence-in-gson-serialization) – OhleC Aug 13 '18 at 14:54
  • Why does it matter? The attributes go back to being hashed when being deserialized back into the memory. – jingx Aug 13 '18 at 14:56
  • @jingx it matters because we sometimes switch on debug logging on the host API where the json is being sent and having end-datetime before start-datetime is one more mental gymnastic it would be good not to have to do when analysing the data, not critical though. – AntG Aug 14 '18 at 11:32
  • @ohlec I think this is correct, however i thought I had disproved it was GSON by checking the data in debug before it gets serialized, and as you can see from the OP it is already in alphabetic order. I now think this is a double issue with Android Studio making it *look* like the data is alphabetic and then GSON *actually* switching it to alphabetic. – AntG Aug 14 '18 at 11:34

2 Answers2

3

Android Studio/IntelliJ has a handful of options for configuring how the debugger displays the data. This is described in the docs for Variables pane. The sorting is controlled by Sort Values Alphabetically option in the Debug Tool Window.

This is purely cosmetic and unrelated to actual object layout in memory. Java comes with JOL tool but I'm not sure it will work in Android:

JOL (Java Object Layout) is the tiny toolbox to analyze object layout schemes in JVMs. These tools are using Unsafe, JVMTI, and Serviceability Agent (SA) heavily to decoder the actual object layout, footprint, and references. This makes JOL much more accurate than other tools relying on heap dumps, specification assumptions, etc.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Hmm, my version (3.1.4) doesn't have the Sort Values Alphabetically option. But it is having a material impact as values are being sent to my host API in alphabetic order. However I suspect (as @ohlec has pointed out) it is GSON doing this, and Android Studio also doing it in the debug window created a red-herring in my analysis. +1 for the reference though, thanks. – AntG Aug 14 '18 at 11:30
  • My mistake, debugger has to running to show that option in the settings. – AntG Aug 14 '18 at 11:52
1

As others have pointed out, the debug variables view in IDEA does not directly reflect the memory layout; it's just a GUI choice made by IntelliJ. You should generally not think of variables having any kind of order. The order in which you put them in the source code is irrelevant.

Since there is no order to the fields, and since the JSON spec also states that fields aren't ordered, GSON is free to choose an ordering for serialization. Both IntelliJ (for GUI) and GSON (for serialization) chose alphabetical order, because that's the most user-friendly.

If you must have a different order in JSON (maybe because your data is consumed by software that violates the JSON spec by depending on a certain order), see this answer

OhleC
  • 2,821
  • 16
  • 29