Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added kotlinx.json JsonElement serialization support #1459

Merged
merged 9 commits into from
Sep 13, 2024

Conversation

rozza
Copy link
Member

@rozza rozza commented Jul 24, 2024

@rozza rozza requested a review from vbabanin July 24, 2024 15:14
@@ -38,19 +38,27 @@ description = "Bson Kotlinx Codecs"

ext.set("pomName", "Bson Kotlinx")

java {
registerFeature("jsonSupport") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required to make the dependency optional - see: JAVA-5540

@@ -45,34 +50,93 @@ import org.bson.types.ObjectId
*
* For custom serialization handlers
*/
public sealed interface BsonDecoder {
@ExperimentalSerializationApi
internal sealed interface BsonDecoder : Decoder, CompositeDecoder {
Copy link
Member Author

@rozza rozza Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is sealed there is no need for it to have been public - as all implementations are private. So its effectively private.

@@ -37,31 +38,57 @@ import org.bson.types.ObjectId
*
* For custom serialization handlers
*/
public sealed interface BsonEncoder {
@ExperimentalSerializationApi
internal sealed interface BsonEncoder : Encoder, CompositeEncoder {
Copy link
Member Author

@rozza rozza Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the BsonDecoder interface - As this is sealed there is no need for it to have been public - as all implementations are private. So its effectively private.

import org.bson.internal.UuidHelper

@OptIn(ExperimentalSerializationApi::class)
internal interface JsonBsonDecoder : BsonDecoder, JsonDecoder {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provides Json decoding support

import org.bson.types.Decimal128

@OptIn(ExperimentalSerializationApi::class)
internal class JsonBsonEncoder(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provides Json encoding support

@@ -172,13 +172,13 @@ private constructor(
}

override fun encode(writer: BsonWriter, value: T, encoderContext: EncoderContext) {
serializer.serialize(DefaultBsonEncoder(writer, serializersModule, bsonConfiguration), value)
serializer.serialize(BsonEncoder.createBsonEncoder(writer, serializersModule, bsonConfiguration), value)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the factory to check which encoder / decoder to load.

}

@Test
fun testDataClassWithNestedJsonElements() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test to ensure complex nested types also support json elements.

@@ -100,6 +100,7 @@ configure(javaProjects) { project ->
artifact sourcesJar
artifact javadocJar

suppressAllPomMetadataWarnings()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A side effect of using gradle features. We dont need to publish the metadata in the pom at this time - so I've disabled the warnings.

Copy link
Member

@vbabanin vbabanin Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] We could suppress warnings for specific variants using suppressPomMetadataWarningsFor(variant) instead of all at once to ensure that we don't overlook any warnings unrelated to this change in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rozza rozza requested a review from vbabanin July 30, 2024 08:06
@@ -100,6 +100,7 @@ configure(javaProjects) { project ->
artifact sourcesJar
artifact javadocJar

suppressAllPomMetadataWarnings()
Copy link
Member

@vbabanin vbabanin Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional] We could suppress warnings for specific variants using suppressPomMetadataWarningsFor(variant) instead of all at once to ensure that we don't overlook any warnings unrelated to this change in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that the current tests may not be covering the JsonBsonEncoder, as the coverage analysis indicates that the class has not been accessed. Are these tests intended to evaluate the JsonBsonEncoder? If not, could we consider adding tests to ensure this functionality is covered by tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vbabanin turns out that wasn't really being used at all, rather it was left to some other implementation of the JsonEncoder. I overrode encodeSerializableValue to handle JsonElements and added extra tests for coverage.

I also tested to make sure the encoding configuration was honored.

rozza added 3 commits August 12, 2024 16:27
Types from Json are now converted to the lowest Bson size for their type instead of all
being Doubles.

Also, improve test coverage
@rozza rozza requested a review from vbabanin August 12, 2024 16:47
put("string", JsonPrimitive("the fox ..."))
put("timestamp", JsonPrimitive(1311768464867721221))
})
assertDecodesTo("""{"value": $jsonAllSupportedTypesDocument}""", dataClassWithAllSupportedJsonTypes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we are missing a test case for encoding jsonAllSupportedTypesDocument, which includes a more comprehensive nested structure within JsonElement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - it is not roundtrippable so encodes and decodes can be different.

@rozza
Copy link
Member Author

rozza commented Aug 29, 2024

@vbabanin going to pause until #1462 is merged then rebase before final review as I think there will be some code share between the two.

@rozza rozza requested a review from vbabanin September 3, 2024 12:29
@rozza rozza merged commit 03f32ea into mongodb:master Sep 13, 2024
56 of 59 checks passed
@rozza rozza deleted the JAVA-5239 branch September 13, 2024 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants