Add more custom serializers for kotlinx serialization#2978
Open
Luna712 wants to merge 3 commits into
Open
Conversation
This also seperates the tests, and test serializers that are in library, using commonMain tests rather than testing library serializers in androidTest also. The tests in androidTest have been changed to just test UriSerializer, which is the only one actually there. This adds numerous other serializers to replicate Jackson mapper configuration behavior, that some extensions will need (such as [InternetArchiveProvider](https://github.com/recloudstream/extensions/blob/cf16b3b45cda911a36ddca69f9acbe4763c6a4b1/InternetArchiveProvider/src/main/kotlin/recloudstream/InternetArchiveProvider.kt#L47-L56)) This adds support for replicating the following behaviors: ```kt jacksonObjectMapper().apply { configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true) configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, true) } ``` For `DeserializationFeature.ACCEPT_FLOAT_AS_INT`, which allows converting both Int or Long to a Float, it was split into two separate serializers, `FloatAsIntSerializer` and `FloatAsLongSerializer`, as it was complex to do both at once and I also figured it would be nice to allow splitting this before for more explicit control. Another difference is that unlike custom mappers that can be applied to the whole thing, when migrated from Jackson to Kotlinx, it would have to drop custom mappers entirely, use the normal `parseJson`/`tryParseJson`/etc... and be explicit about what classes need this, which in my personal opinion is better to be more explicit and allows us to be more unified in how the `AppUtils` methods are used more accept under some way more rare circumstances where you may need a custom `Json` instance. I also explained how to use all the serializers in doc comments. We could also later add more serializers if necessary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This also seperates the tests, and test serializers that are in library, using commonMain tests rather than testing library serializers in androidTest also. The tests in androidTest have been changed to just test UriSerializer, which is the only one actually there.
This adds numerous other serializers to replicate Jackson mapper configuration behavior, that some extensions will need (such as InternetArchiveProvider) This adds support for replicating the following behaviors:
For
DeserializationFeature.ACCEPT_FLOAT_AS_INT, which allows converting both Int or Long to a Float, it was split into two separate serializers,FloatAsIntSerializerandFloatAsLongSerializer, as it was complex to do both at once and I also figured it would be nice to allow splitting this before for more explicit control.Another difference is that unlike custom mappers that can be applied to the whole thing, when migrated from Jackson to Kotlinx, it would have to drop custom mappers entirely, use the normal
parseJson/tryParseJson/etc... and be explicit about what classes need this, which in my personal opinion is better to be more explicit and allows us to be more unified in how theAppUtilsmethods are used more accept under some way more rare circumstances where you may need a customJsoninstance.I also explained how to use all the serializers in doc comments. We could also later add more serializers if necessary.