0

I have an app that stores client data in a local Room DB. I have added an option to export clients list as Json file. Everything works fine except that data in exported json file is ordered alphabetically by value names.

Room Data Class

@Entity(tableName = "clients")
data class ClientData(
    @PrimaryKey(autoGenerate = true)
    val clientId: Long?,
    var name: String,
    var eMail: String? = null,
    var phone: String? = null,
    var businessNumber: String? = null,
    @Embedded
    var address: Address?
)

Address Class

data class Address(
    var fullAddress: String? = null,
    var city: String? = null,
    var state: String? = null,
    var zip: String? = null,
    var country: String? = null
)

JSON Export

private fun exportClientsJson(file: File, clients: List<ClientData>) {
        val prettyJson = GsonBuilder().setPrettyPrinting().create()
        val jsonClients = prettyJson.toJson(clients)
        file.writeText(jsonClients)
    }

Exported File

[
  {
    "address": {
      "city": "",
      "country": "",
      "fullAddress": "",
      "state": "",
      "zip": ""
    },
    "clientId": 1,
    "eMail": "nick@gmail.com",
    "name": "Nick",
    "phone": "1234567890"
  }
]

As you can see order of values is not the same as in data class. It places everything alphabetically by keys for main (ClientData) and nested class (Address). I also tried simple json string ( Gson().toJson(clients)) - it is the same thing, just in one line. I mean it is probably useful in many cases, but I do not really want this. I never worked with json in android\kotlin. Is it normal behavior? Is it possible to disable this auto sort?

Nick Wilde
  • 165
  • 3
  • 15
  • "As you can see order of values is not the same as in data class" -- it does not need to be the same. The order of fields in a JSON object does not matter, from a JSON standpoint. Moreover, the order of the properties in the class is not necessarily maintained when the code gets compiled and goes through other transformations in the build process. The general solution for this is to write a custom adapter that can take into account your rules for what order the fields should be in. – CommonsWare Jul 03 '21 at 17:03
  • I understand all you say, but I am still curious why it does specifically alphabetically. It could have been random, but it is not – Nick Wilde Jul 03 '21 at 17:24
  • Presumably, that is how the Gson developers wrote it. We cannot tell you why they made that decision. Note that Gson is no longer being maintained, so you may wish to use a different JSON parser/generator, such as [Moshi](https://github.com/square/moshi). – CommonsWare Jul 03 '21 at 17:25
  • 1
    Ok, I see. Are you sure about not being maintained? They released a new version a month ago – Nick Wilde Jul 03 '21 at 17:32
  • Ah, I had not realized that they were still issuing patches. My understanding from others is that they are not actively working on advancing Gson beyond its current functionality set. – CommonsWare Jul 03 '21 at 17:56

0 Answers0