[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: |
|
||||||||
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
||||||||
| Description |
SummaryServers like ktor is using StreamingJsonEncoder for the responses internaly but MongoDB ObjectId does not support StreamingJsonEncoder for the ...
How to ReproduceWhen you want to send ObjectId value in the response of ktor server... |
| 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! | ||||||||||
| Comment by Ross Lawley [ 09/Aug/23 ] | ||||||||||
|
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...
The serialization by kotlinx.serialization.json.internal.StreamingJsonEncoder is then supported and running without errors... | ||||||||||
| Comment by Uroš Jarc [ 08/Aug/23 ] | ||||||||||
|
I'm using ObjectID serializer from here
| ||||||||||
| 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. |