Richard Gomez
11/18/2021, 3:07 AMMap<String, Any>
? It's a legitimate use case that seems to have been dismissed outright.
Saying "use JsonObject instead" but not providing any guidance is a recipe for frustration and buggy implementations:
https://github.com/Kotlin/kotlinx.serialization/issues/296
https://github.com/Kotlin/kotlinx.serialization/issues/746
https://github.com/Kotlin/kotlinx.serialization/issues/1537
https://kotlinlang.slack.com/archives/C7A1U5PTM/p1632495920135600?thread_ts=1632392673.125200&cid=C7A1U5PTMephemient
11/18/2021, 4:06 AMAny
, safely - Jackson/Gson aren't safe, in that they can fail at runtime rather than compile timeephemient
11/18/2021, 4:11 AMAny
types you actually want to handleRichard Gomez
11/18/2021, 4:36 AMthere will never be a general way to handle serializingWhile I understand the rationale, saying "just use JsonObject" is a bit of a cop-out. Requiring people to write their own serializers doesn't actually absolve kotlinx.serialization of the issue — those user-created serializers can also fail at runtime. Is people writing custom serializers that rely on reflection really better than something like, safely - Jackson/Gson aren't safe, in that they can fail at runtime rather than compile timeAny
Map<String, @Contextual Any?>
that fails if it can't find a serializer at runtime?
It just feels like clash between correctness and pragmatism.Richard Gomez
11/18/2021, 4:37 AMephemient
11/18/2021, 4:39 AMAny?
anywhere else, it's dangerous (security-wise) to allow uncontrolled input to deserialize to and instantiate arbitrary classesephemient
11/18/2021, 4:41 AMRichard Gomez
11/18/2021, 4:41 AMkotlinx.serialization.json.JsonObject
in application code. The implementation is technically correct, but difficult to use compared to javax.json.JsonObject or Vert.x's JsonObjec
e.g. https://kotlinlang.slack.com/archives/C7A1U5PTM/p1633134038183200ephemient
11/18/2021, 4:46 AMephemient
11/18/2021, 4:48 AMRichard Gomez
11/18/2021, 4:51 AMlike@ephemient: I agree; I guess my point is that people are going to do it anyway if there's a need, and in my experience it's easier to do it the unsafe way (custom serializer with reflection, or use Jackson) than the safe way. There may already be a really simple and safe way to handleanywhere else, it's dangerous (security-wise) to allow uncontrolled input to deserialize to and instantiate arbitrary classesAny?
Map<String, Any>
, but the discussions and documentation tend to leave the details as an exercise to the reader.Richard Gomez
11/18/2021, 4:54 AMephemient
11/18/2021, 5:03 AMMap<String, Any>
values could be, either JsonElement-like primitives (but numeric primitives can't round-trip, which all default serializers try to do, and it's application specific what you want them to deserialize to), or polymorphism including some subset of program types, or open polymorphism. none of this can be encoded statically in the types and so there isn't a clean way to express it in kotlinx.serialization eitherephemient
11/18/2021, 5:04 AMRichard Gomez
11/18/2021, 5:18 AMI think there's a bunch of things aAgain, I understand why they don't do certain things. But I think it'd be useful to have implementations or well documented examples for people who need to do them. As with global custom serializers or serializers for types like UUID or Java Time, the philosophy seems to be "this could be used differently 1% of the time, so we won't provide help for the other 99%". To steal a point from @thanksforallthefish: kotlin serialization often feels overly generic, which makes it tedious to use compared to something like Jackson.values could be... so there isn't a clean way to expressMap<String, Any>
Richard Gomez
11/18/2021, 5:19 AMRichard Gomez
11/18/2021, 5:21 AMephemient
11/18/2021, 5:43 AMephemient
11/18/2021, 5:46 AMBig Chungus
11/18/2021, 8:22 AMephemient
11/18/2021, 8:26 AMephemient
11/18/2021, 8:30 AMbuildList<Any> { add(this) }
buildMap<String, Any> { put("this", this) }
are legal Kotlin objects but any .toJsonElement()
will failBig Chungus
11/18/2021, 8:34 AMephemient
11/18/2021, 8:39 AMJsonElement
so I'm just saying there are reasons to make that the public interface, not Any