[JAVA-5101] ObjectId is not supported by class kotlinx.serialization.json.internal.StreamingJsonEncoder Created: 08/Aug/23  Updated: 27/Oct/23  Resolved: 09/Aug/23

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Uroš Jarc Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
backported by JAVA-5099 Driver is using Kotlin native seriali... Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Servers like ktor is using StreamingJsonEncoder for the responses internaly but MongoDB ObjectId does not support StreamingJsonEncoder for the ...

kotlinx.serialization.SerializationException: ObjectId is not supported by class kotlinx.serialization.json.internal.StreamingJsonEncoder
	at org.bson.codecs.kotlinx.ObjectIdSerializer.serialize(BsonSerializers.kt:74) ~[bson-kotlinx-4.10.1.jar:?]
	at org.bson.codecs.kotlinx.ObjectIdSerializer.serialize(BsonSerializers.kt:66) ~[bson-kotlinx-4.10.1.jar:?]
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:228) ~[kotlinx-serialization-json-jvm-1.5.0.jar:?]
	at kotlinx.serialization.ContextualSerializer.serialize(ContextualSerializer.kt:63) ~[kotlinx-serialization-core-jvm-1.5.0.jar:1.5.0]
	at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:228) ~[kotlinx-serialization-json-jvm-1.5.0.jar:?]

How to Reproduce

When you want to send ObjectId value in the response of ktor server...
https://ktor.io/
(I'm tired and I didn't sleep for days dealing with MongoDb issues... please help the brother out)



 Comments   
Comment by Uroš Jarc [ 09/Aug/23 ]

Ohhhh I see I attached ObjectIdSerializer to Json module that server is using so the server converted Entity to plain BSON which is not supported since he needs Plain Json objects. I see now! It would be nice to rename this serializer to ObjectIdBsonSerializer?

Thank you so much for the explanation!
Most appreciated!

Comment by Ross Lawley [ 09/Aug/23 ]

Hi jar.fmf@gmail.com,

I understand your pain unfortunately, org.bson.codecs.kotlinx only handles conversion to Bson and will throw if alternative encoders / decoders are used (as you've discovered).

In the case of JSON its not known what format to use for each Bson type. There is extended JSON specification which specifies how Bson types can be roundtripped to and from JSON. But this wouldn't be appropriate in your case as you are just using hex strings.

In your scenario domain knowledge is required and as such a custom serializer.

Finally, the Bson Serializers cannot just default to encoding String as the common case is to and from BSON but others may want to use the Serialization framework to convert to alternative formats eg: toml, protobuf, avro. Each encoder / decoder instance requires domain knowledge on encoding and decoding types.

So unfortunately, this is also a "won't fix" issue.

Ross

Comment by Uroš Jarc [ 09/Aug/23 ]

The current org.bson.codecs.kotlinx.ObjectIdSerializer does not work BUT if you replace that serializer with this one...

object ObjectIdSerializer : KSerializer<ObjectId> {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ObjectId", PrimitiveKind.STRING)
    override fun serialize(encoder: Encoder, value: ObjectId) {
        encoder.encodeString(value.toHexString())
    }
    override fun deserialize(decoder: Decoder): ObjectId {
        return ObjectId(decoder.decodeString())
    }
}

The serialization by kotlinx.serialization.json.internal.StreamingJsonEncoder is then supported and running without errors...
I would suggest to replace the old Serialization with this one?

Comment by Uroš Jarc [ 08/Aug/23 ]

I'm using ObjectID serializer from here

import org.bson.codecs.kotlinx.ObjectIdSerializer

Comment by PM Bot [ 08/Aug/23 ]

Hi jar.fmf@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:03:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.