-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
JAVA-5239 --------- Co-authored-by: Mark <[email protected]>
bson-kotlinx/build.gradle.kts
Outdated
@@ -38,19 +38,27 @@ description = "Bson Kotlinx Codecs" | |||
|
|||
ext.set("pomName", "Bson Kotlinx") | |||
|
|||
java { | |||
registerFeature("jsonSupport") { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
gradle/publish.gradle
Outdated
@@ -100,6 +100,7 @@ configure(javaProjects) { project -> | |||
artifact sourcesJar | |||
artifact javadocJar | |||
|
|||
suppressAllPomMetadataWarnings() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
gradle/publish.gradle
Outdated
@@ -100,6 +100,7 @@ configure(javaProjects) { project -> | |||
artifact sourcesJar | |||
artifact javadocJar | |||
|
|||
suppressAllPomMetadataWarnings() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 JsonElement
s and added extra tests for coverage.
I also tested to make sure the encoding configuration was honored.
Types from Json are now converted to the lowest Bson size for their type instead of all being Doubles. Also, improve test coverage
put("string", JsonPrimitive("the fox ...")) | ||
put("timestamp", JsonPrimitive(1311768464867721221)) | ||
}) | ||
assertDecodesTo("""{"value": $jsonAllSupportedTypesDocument}""", dataClassWithAllSupportedJsonTypes) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
JAVA-5239