Hi, does anyone know how to deserialise an enum fr...
# serialization
s
Hi, does anyone know how to deserialise an enum from a string?
Copy code
@Serializable
enum class Error(@SerialName("code") val code: String? = null) {
    UNEXPECTED("0001")
}
If I pass it a json string of the following I want it to create an enum response.
Copy code
{
  "code": "0001"
}
I used to use CommonEnumSerializer but its been deprecated. Is there an alternative?
Just a note, the reason I want this is because all the other examples show an enum nested in a data class. But all I need is the object
a
I had to create a custom deserializer to handle this
s
Do you have an example by any chance? I was using the custom CommonEnumSerializer but I can't seem to use it with 0.20.0
This is for polymorphic serialization but if you amend it a bit it should work for you.
s
I'll give this a try! Thank you 👏
👍 1
Ok, that didnt work for me so I decided to just use a filter.
Copy code
Error.values().first { it.errorCode == "0001" }
k
What was the error it gave?
s
I changed the approach as this was to merge two enums and return an interface, what I needed to do was construct an enum based on a param.
p
How exactly did you solve it?
s
Copy code
return YOURENUMCLASS.values().first { it.errorCode == code }
👍 1