kbson icon indicating copy to clipboard operation
kbson copied to clipboard

Add support to encode Json in BsonEncoder

Open ageorgousakis opened this issue 3 years ago • 1 comments

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.

ageorgousakis avatar Oct 16 '21 11:10 ageorgousakis

@zeletrik Thanks for adding support for Json encode

I did the following changes to fix test parseMapAny

fix_parseMapAny_test1.patch

ageorgousakis avatar Apr 03 '23 16:04 ageorgousakis