Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin inline classes not correctly serialised in HashMap #1524

Closed
geertberkers opened this issue May 1, 2019 · 3 comments
Closed

Kotlin inline classes not correctly serialised in HashMap #1524

geertberkers opened this issue May 1, 2019 · 3 comments

Comments

@geertberkers
Copy link

I am using the following code:

inline class Identifier(val value: String)

data class FormInstance(
    var formId: Identifier,
    var createdAt: Date = Date(),
    var modifiedAt: Date = Date(),
    var fieldValues: HashMap<Identifier, String> = hashMapOf()
)

And the result is serialized as:

{
    "formId": "ADDISON_DIARY",
    "modifiedAt": "2019-05-01T23:44:22.703+0200"
    "createdAt": "2019-04-30T23:44:22.701+0200",
    "fieldValues": {
      "Identifier(value\u003dfrustration)": "VERYMUCH",
      "Identifier(value\u003dstress)": "NONE",
      "Identifier(value\u003dlimitations)": "ALITTLE",
      "Identifier(value\u003dsleep)": "ALOT",
      "Identifier(value\u003dnegative_effect_partner)": "ALITTLE",
      "Identifier(value\u003dsomber_feelings)": "VERYLITTLE",
}

So the problem is that the Identifier gets it value when its parsed itself, but when parsing the hashmap, it uses the following code: (screenshot line 207)
Screenshot 2019-05-01 23 48 31

I think the code needs to check for inline classes if this is possible, because these are experimental in kotlin 1.3, and the library is written in java.

@lyubomyr-shaydariv
Copy link
Contributor

I don't believe Gson should support Kotlin or its experimental features. It's probably a design problem that the Map type adapter uses static String.valueOf instead of a key-to-string strategy (and not rejecting non-String-keyed maps), but I would consider implementing a FormInstance to FormInstanceDto (having a map with true String keys, not inline classes) converter and vice versa.

@JakeWharton
Copy link
Contributor

You can write a TypeAdapter which unwraps the class yourself or just override toString() and write out the wrapped property value. Gson won't be seeing updates for Kotlin.

@Marcono1234
Copy link
Collaborator

Relates to #1722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants