1

I am attempting to populate a Kotlin data class from a corresponding string value. I looked at: Kotlin Data Class from Json using GSON but what I am attempting to do is not tracking exactly the same:

fun convertStringToObject(stringValue: String?, clazz: Class<*>): Any? {
    return if (stringValue != null) {
        try {
            val gson = Gson()

            gson.fromJson<Any>(stringValue, clazz)
        }
        catch (exception: Exception) {
            // yes, we are swallowing the possible 
            // java.lang.IllegalStateException
            null
        }
    }
    else {
        null
    }
}

Calling this function and attempting to populate the following class:

data class LoggedUser(
    @SerializedName("id") val id: Long,
    @SerializedName("name") val name: String,
    @SerializedName("first_name") val firstName: String,
    @SerializedName("last_name") val lastName: String,
    @SerializedName("email") val email: String
)

It executes but LoggedUser values are empty (null).

The stringValue is currently:

{"nameValuePairs":{"id":"1654488452866661","name":"Bob Smith","email":"bob.smith@test.net","first_name":"Bob","last_name":"Smith"}}

I even tried using a hardcoded class value like this:

gson.fromJson(stringValue, LoggedUser::class.java)

but there was no joy. The stringValue is what gson.toJson(value) generated where value was a JSONObject.

Anybody have an idea what my disconnect is?

Perry Hoekstra
  • 2,687
  • 3
  • 33
  • 52

2 Answers2

2

Your JSON string actually has two JSON objects, the outer value (which has a key called nameValuePairs) and the value you actually want to deserialize (which is the value at key nameValuePairs). You need to dive one level deeper, either through an outer wrapper class which holds your User object or by manually getting the JsonObject at key nameValuePairs as a String and then passing that to Gson. If the string was just {"id":"1654488452866661","name":"Bob Smith","email":"bob.smith@test.net","first_name":"Bob","last_name":"Smith"} it would deserialize fine.

Eric Bachhuber
  • 3,872
  • 2
  • 19
  • 26
  • Yes, as I understand it, I am running into the issue documented in: https://stackoverflow.com/questions/26022133/how-can-i-delete-the-namevaluepairs-key-from-the-jsonobject ... And unfortunately, I am stuck with working with JSONObject as opposed to JsonObject. – Perry Hoekstra Jul 03 '18 at 02:55
2

Create a wrapper class

data class LoggedUserWrapper{
    @SerializedName("nameValuePairs") val nameValuePairs: LoggedUser
}

and execute

val loggedUser = convertStringToObject(yourJsonString, LoggedUserWrapper::class.java)

This will help you.

Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
  • Thank you for the suggestion but it did not work. I believe because nameValuePairs is not a JSON key but a member variable that is part of the JSON Object class. – Perry Hoekstra Jul 03 '18 at 11:08
  • Because nameValueParis is not a JSON key, identifying it as such (with SerializedName) means that it is not found and results in an empty data class. – Perry Hoekstra Jul 03 '18 at 11:22