I am using kotlinx.serialisation and had a question.
I have JSON like this:
{
"state": "bad"
}
state can return bad|good|near_good
My State data class looks like this:
@Serializable
internal enum class State {
@SerialName("good") GOOD,
@SerialName("bad") BAD,
@SerialName("near_good") NEAR_GOOD
}
Is it possible to have the enum names remain all caps like this, while parsing the json value that is returned? Right now when I test this, the parsed json data returns as GOOD|BAD|NEAR_GOOD, because the enum name is uppercase. Hopefully the question makes sense. Appreciate any answers.
EDIT (Updated for clarity)
Right now I have a test that checks: assert(state.name == "bad") This fails because with the way I mentioned it above (@SerialName("bad") BAD), the state.name is equal to 'BAD'. I want the enum name to be uppercase, as per enum naming convention, but I want the value to be lowercase as per the JSON. I can fix the failing test by changing it to be
@SerialName("bad") bad
I'm not sure if it is possible or maybe I am doing something in the wrong way, but I hope this clarifies.
Thanks!
I guess the problem is your test. It's not very robust to check for string equality. You have powerful compile-time guarantees with enums, so I would suggest to just compare enum values, not strings:
assert(state == State.BAD)
If you're trying to test the serial name of the enum value, then... don't? That's the job of Kotlinx Serialization's tests to verify that the annotations work correctly.