-
Notifications
You must be signed in to change notification settings - Fork 43
Description
There is a code in the JsonValueDecoder.readEnum which tries to decode enum field as integer first, which in most cases, when provided enum value is their name, causes the JsonDecodingException to be thrown but immediately caught and suppressed by mapExceptionsToNull { ... }. Only then decoder tries to deserialize enum field by name. JsonValueDecoder.writeEnum says that enum may be serialized as int in case of unrecognized value, and this is understandable, but is quite a rare case, isn't is?
fun readEnum(value: JsonElement, enumCompanion: Message.Enum.Companion<*>): Message.Enum? = try {
val p = value.jsonPrimitive
p.intOrNull?.let { return enumCompanion.fromValue(it) } // 1. decode as int first
require(p.isString) { "Non-numeric enum values must be quoted" }
enumCompanion.fromName(p.content) // 2. the decode as name
} catch (e: Exception) {
...
}But creating and throwing an exception on every enum field while running a "hot" code with many calls to deserialization leads to bad performance. Shouldn't the JsonValueDecoder.readEnum try deserialize enum by name first because this is a most common case? This will completely exclude throwing the JsonDecodingException when it's obviously known that your system operates with enum names but not unrecognized values.