kbson
kbson copied to clipboard
Add support to encode Json in BsonEncoder
Some times it is required to serialise in Bson a property that is mapped to a Map<String, Any?> type. It would be nice if the Map<String, Any?> can be encoded to a Json structure. This can be easily done by adding one method in BsonReader to encode a JSON string
fun encodeJson(value: String) {
when (state) {
STATE.VALUE -> writer.pipe(JsonReader(value))
STATE.NAME -> throw SerializationException("Json is not supported as a key of map")
}
}
I created a patch that add this method. There is also a test case KBsonMapAnyTest.kt
that you can check and understand better why this method is needed.
add_support_to_encode_Json_structure.txt
It will be also nice if there was a decoder from BsonDocument
to Map<String, Any?>
. I tried to use BsonFlexibleDecoder
but maybe there is a better way to do it. You can check parseBsonDocument
inside MapAnySerializer
object in the test classes.
@zeletrik Thanks for adding support for Json encode
I did the following changes to fix test parseMapAny