Hi guys, is it possible to deserialize json into u...
# serialization
b
Hi guys, is it possible to deserialize json into unknown class (class provided by library consumer) like in gson for example via
Gson().fromJson(<str>, <any java class>.class)
v
Unknown or not under your control?
b
Precisely
I mean unknown to me = out of my control
v
That was not a yes / no question, but an either / or question
Do you know the class and could say "put field X from JSON into field Y of that class"?
b
Essentially a class that I cannot guarantee will have @Serializable annotation
Also we're talking only about dumb POJO classes
v
You can provide a serializer outside of the class, for example like this: https://github.com/Vampire/setup-wsl/blob/master/buildSrc/src/main/kotlin/net/kautler/dao/ResultSerializer.kt
b
Ah, but I do not know the class, it's provided by the consumer
(I understand your initial question now)
SO to clarify, we're talking about unknown classes
v
Then he probably also has to provide the serializer. At least as far as I know there is no "just take this pojo and do magic" option like with Jackson unfortunately.
If you find a way please let me know, I would also be interested 🙂
b
I remember there being some
ImplicitReflectionSerializer
that enabled kotlinx.serialization as content negotiator module for krot-client
Just can't find any docs on it
v
I just found https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#serializing-3rd-party-classes when I searched, so I wrote that custom serializer, which is actually just serializing a POJO tree
Changelog 0.9.0: Introduce @ImplicitReflectionSerializer for API which involves reflection Changelog 1.0.0-RC:
@ImplicitReflectionSerializer
is deprecated
😕 2
b
Yeah, while it's super useful, it does not solve my issue of deserializing unknown classes 😕
Does it say anything about what it was deprecated in favour of?
v
Not in the changelog
s
How you actually interact using Gson with that unknown class? Via
Class<*>
? If so, your api for kotlinx.serialization can have
KSerializer<*>
as parameter or
KType
to use it in
serializer()
function. So the consumer of your library provides you the class and the serializer/type token
You can still use this: https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx-serialization-core/kotlinx.serialization/serializer-or-null.html but it is not an ideal way and read carefully 'constraints' section first.
b
Yeah, that also does not work for my case. I need implicit reflection deserializer that does not need any explicit serializer (like GSON).
@sandwwraith, are there any plans of providing reflection serializer on jvm?
s
Gson needs java type token/Class<*> to work, same as
serializer()
method in kx.serialization. Or do you mean that this class can be NOT marked as
@Serializable
? If so, we do not have plans to support such classes via reflection. Goal of this project is a code generation via compiler plugin
b
Or do you mean that this class can be NOT marked as 
@Serializable
?
I meant exactly that. Now since this is missing, I'm forced to have both, kotlinx.serialization and gson for this edge-case only.
e
@sandwwraith I know there was supposed to be a feature where the serialization plugin will generate a custom serializer for 3rd party classes (ie classes not in the same module) by defining an object and annotating it. Is this correct? IIRC it wasnt implemented yet/ wasnt working
e
No, talking about the last 2 sections in that document. But now I read it properly that doesn't apply in this scenario as he's talking about consumers of his library whereas that would be for a case where he is the consumer of some unannotated classes instead
v
Ah, I see, nice
111 Views