2

I am using Retrofit with GSON for an app for a client, and I am having some trouble with some of my client APIs and I need to workaround this problem.

Let's say i have an API which gives me telephones:

{
  "telephones": [
  {"phoneNumber": "1234567890"},
  {"phoneNumber": "2123456789"}
  ]
}

But my client decided if there is only one telephone i am sending you:

{
   "telephones": 
   {"phoneNumber": "1234567890"}
}

And when there is no telephone:

{
   "telephones": "No telephone Available"
}

Is there any workaround i can make with Kotlin to solve this datatype problem? In iOS I could force them reimplementing the Coding method and force them to always have an array. Is it possible to do something similar in Kotlin?

This is a small example, since the original answer has between 600 and 1300 lines of JSON data.

  • You client definitely likes weird things they might regret for in the future (it really complicates things with absolutely no cause). If you can't make them change their mind, have a look how to detect single elements and arrays: https://stackoverflow.com/questions/43412261/make-gson-accept-single-objects-where-it-expects-arrays/43412985#43412985 . I guess it will be also easy to distinguish between single elements, arrays, and strings, like what you're describing. – terrorrussia-keeps-killing Jul 26 '21 at 19:10
  • I agree, but they just don't want to change it, since they originally built the API in XML, this problem doesn't apply there, so i think it is just a parsing error in their APIs, i am going to check, but it seems using a customDataType might be the answer, thanks so much @fluffy – Santiago Linietsky Jul 26 '21 at 20:32

1 Answers1

1

This might work.

I have done this in many places in my app. So, the first thing, let's say you receive multiple different telephone numbers in one JSON file and might look something like this.

  • install a plugin call JSON to Kotlin Class
  • once done, make a new file using "Kotlin data class file from JSON"
  • the plugin for me creates automatically appropriate files.
  • then I use the main file to capture the data, this has worked for me almost everytime.

And yes, my other answer was if you are doing everything manually, while retrieving the data make a data class such as: This is just for explanation purposes.

//lets say your json looks something like this
"records": [
        {
            "id": "1",
            "telephones": [
      {"phoneNumber": "1234567890"},
      {"phoneNumber": "2123456789"}
      ]
        },
        {
            "id": "2",
            "telephones": 
      {"phoneNumber": "1234567890"}
        },
        {
            "id": "3",
            "telephones": "No telephone Available"
        }
]

my example code would look something like this.

data class records(
//some id 
val id: Int,
var telephones: List<Long>
)

so, now check inside telephones how many elements are there and add them to the list one by one.

lets start coding
  • 1,839
  • 1
  • 10
  • 19