Skip to content

Provide error details in case of [de]serialization failure in a custom serializer #2165

Open
@LibertyPaul

Description

@LibertyPaul

What is your use-case and why do you need this feature?

We are using custom serializers extensively.
It is significantly more difficult to locate an incorrect value in case a custom serializer is used.

❌ Custom serializer failure. The exception does not provide any information about the incorrect field or value:

java.lang.NumberFormatException: Invalid number format: '0x'
	at kotlin.text.StringsKt__StringNumberConversionsKt.numberFormatError(StringNumberConversions.kt:203)
	at kotlin.text.UStringsKt.toUByte(UStrings.kt:66)
	at com.company.app_name.config.serializers.ByteListAsHexSerializer.deserialize(ByteListAsHexSerializer.kt:34)
        ...

✅ Native serializer failure. The exception contains a detailed exception:

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 661: Unexpected symbol 'a' in numeric literal at path: $.field_name
JSON input: .....: "0x00",
    "field_name": "asdfgh",
    "another_field_name.....

Describe the solution you'd like

kotlinx.serialization could intercept exceptions raised by custom serializers and throw it's own kotlinx.serialization.json.internal.JsonDecodingException with the error context (field name + incorrect value) and the original exception linked to it (as cause).

Alternatively (yet it does not look as good as the previous option), a custom serializer can be given with an access to the serialization context (field name / value / surrounding JSON) to be able to construct an appropriate error message on its own.

Environment:
Kotlin: 1.8.0 / JVM
Serialization plugin: 1.8.0
Serialization library: 1.4.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions