Open radoye opened 3 years ago
Hello,
Here you use the kmongo-coroutine
dependency (jackson mapping is used)
You may prefer to use the kmongo-coroutine-serialization
and then you get a more meaningful error:
java.lang.IllegalStateException: This serializer can be used only with Json format.Expected Encoder to be JsonEncoder, got class com.github.jershell.kbson.BsonEncoder
.
At this point I do not know any way to achieve what you want with kmongo-coroutine-serialization
.
Could be a nice feature to add to https://github.com/jershell/kbson
Does anyone have solutions on how to store non-typesafe values like a Document
or JsonElement
described in the issue? Also looking for this :eyes:
Due to the type safe nature of kotlinx.serialization, this is not easy (but doable). An other option is to use kmongo jackson mapping that already supports its.
This comment: https://github.com/Kotlin/kotlinx.serialization/issues/296#issuecomment-758136040 could be be a good starting point if someone would like to implement it in kbson.
I don't think I get the point where kBson could help on this. Doesn't it enforces type safe data as well?
kbson is the lib that kmongo uses under the hood to support bson kotlinx.serialization. If you need to support generic Map (or Document) serialization, it would be better (and easier) to add the feature directly in kbson.
Got it. Never worked with that before, but I may be trying something later
@zigzago Not sure i understand the issue.
This is decodable by kotlin serialization
@Serializable
data class MyData(val id: String, val stuff: kotlinx.serialization.json.JsonObject)
Why does kmongo throw?
java.lang.IllegalStateException: This serializer can be used only with Json format.Expected Encoder to be JsonEncoder, got class com.github.jershell.kbson.BsonEncoder
kmongo uses kbson to encode/decode bson (mongodb data format). Kbson does not support JsonObject encoding or decoding for now.
HTH
Is it possible to use a serializer to transform a kotlinx JsonObject to a bson JsonObject and vice versa? As far as i know, bson JsonObject can be sent with Documents.
What you can do is to write a KSerializer that encodes and decodes a JsonObject and register this serializer. It should work. It would be a nice PR for kmongo & kbson.
bump
Hello! What is the recommended way to work with data classes containing
JsonObject
orJsonElement
fields, i.e., an untyped, open-ended key-value element?Here's a simple example
This results in the following log trace
Note how some additional metadata, e.g.,
isString=false
, gets inserted. How to avoid this?Interestingly, if we remove generic parameter in the definition of
Metadata
it completely breaks.This breaks with the following exception (com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `kotlinx.serialization.json.JsonElement)