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

How to serialize enum to actual json number #2623

Closed
AngryGami opened this issue Apr 10, 2024 · 6 comments
Closed

How to serialize enum to actual json number #2623

AngryGami opened this issue Apr 10, 2024 · 6 comments
Labels

Comments

@AngryGami
Copy link

@Serializable
enum class TestEnum(val value: Int) {
    @SerialName(value = "1")
    V1(1),

    @SerialName(value = "2")
    V2(2);

}

fun main() {
    val json = Json.encodeToString(listOf(TestEnum.V1, TestEnum.V2))
    println(json)
}

Code above prints:

["1","2"]

How to make it print:

[1,2]

I.e. use proper json number as enum encoded value?

@iseki0
Copy link

iseki0 commented Apr 14, 2024

As a workaround you may have to implement a customized serializer

@Kantis
Copy link
Contributor

Kantis commented May 8, 2024

You can use enumAsOrdinalSerializer from my complementary library if you'd like. Or just yank code from there to get a starting point. Note that it uses the ordinal value for the enum entries, so V1 in your example would be 0

@AngryGami
Copy link
Author

@Kantis thanks, I've already made my own custom (de)serializer, though I'll take a look at your solution.

@sandwwraith
Copy link
Member

Note that ordinal will differ from val value: Int in your case.

@AngryGami
Copy link
Author

@sandwwraith Yes, I know
And I would still prefer to have control over this via some annotation or parameter instead of writing custom serializer

@pdvrieze
Copy link
Contributor

pdvrieze commented May 8, 2024

And I would still prefer to have control over this via some annotation or parameter instead of writing custom serializer

I don't think that is really possible at the moment. However it is certainly possible to have "canned" serializers that you just apply on a type. It would be interesting though to have an additional parameter to SerializationStrategy.encode and DeserializationStrategy.decode` that passes along the element annotations to the serializer. That would allow customization of such a serializer to work in a platform independent way (where annotations aren't available on all platforms).

There is a bit of a challenge in implementing this. It is possible to implement the new function as a default interface implementation that passes to the original function. This retains compatibility. This however means that for implementation of the new interface the old functions will also need to be implemented, perhaps this is still better though than having both functions default implemented and referring to each other (which would also be binary compatible, but be poor API design).

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

No branches or pull requests

5 participants