Open
Description
Some backends return polymorphic data without a descriptor:
{
"value": "someValue"
}
vs
{
"value": {
"version": 0,
"content": "someValue"
}
}
I know this is quite the edge case but I haven't found a way to decode this without going through JsonElement
and buffering everything.
Would it be possible to introduce jsonDecoder.peekToken()
:
enum class JsonToken {
BEGIN_OBJECT,
BEGIN_ARRAY,
STRING
// others?
}
public interface JsonDecoder : Decoder, CompositeDecoder {
/**
* peeks into the json and returns the next token (without consuming it)
*/
fun peekToken(): JsonToken
}
This way, users that know they are in a JSON context could do stuff like this:
override fun deserialize(decoder: Decoder): Schema {
// unsafe cast, the user needs to assume JSON but in some cases it's doable
decoder as JsonDecoder
return when (decoder.peekToken()) {
BEGIN_OJBECT -> decoder.decodeStructure(/*...*/)
STRING -> decoder.decodeString()
else -> error("unexpected token")
}
}