b

    bissell

    4 years ago
    ^ just checked Gson and it looks like they do this as well, at least with default settings. You can freely mix
    5
    and
    "5"
    and into fields of int/String type and the parser will not generate an error. I dunno, seems to me like "the wrong thing" to do
    s

    sandwwraith

    4 years ago
    Is there any example of library that actually supports such unions?
    b

    bissell

    4 years ago
    There are at least a couple of approaches that work in Jackson: https://gist.github.com/abissell/a93f642c54daad3fdae4b0714c134e57
    One method is to register alternative deserializers for String, Integer, and Long, which inspect the JSON type of the current token and refuse to deserialize a mismatched one. This works -- my implementation uses exception generation but I think it might be possible to get it to work with
    null
    instead. The other method is to just deserialize to a
    Map<String, Object>
    and then inspect the type that Jackson picked.
    Maybe I need to play with
    SerialContext
    a bit, I see the docs say they're similar to Jackson modules. Would still prefer to create a
    KSerializer
    that can be resolved by the plugin at compile time if possible though.