-
Notifications
You must be signed in to change notification settings - Fork 880
Add support for using kotlinx-serialization rather than Jackson #2791
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
Open
Luna712
wants to merge
5
commits into
recloudstream:master
Choose a base branch
from
Luna712:kotlinx-serialization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81588b4
Add support for using kotlinx-serialization rather than Jackson
Luna712 460601b
Fixes and add support everywhere
Luna712 20ccccc
Add test
Luna712 ea34000
Support in SimklApi
Luna712 3046481
Suggested changes + fixes + necessary serializers + tests
Luna712 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
127 changes: 127 additions & 0 deletions
127
app/src/androidTest/java/com/lagradost/cloudstream3/SerializationClassTester.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package com.lagradost.cloudstream3 | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.lagradost.cloudstream3.utils.AppUtils.toJson | ||
| import io.github.classgraph.ClassGraph | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.InternalSerializationApi | ||
| import kotlinx.serialization.KSerializer | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.serializer | ||
| import kotlinx.serialization.serializerOrNull | ||
| import org.instancio.Instancio | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import kotlin.reflect.KClass | ||
| import kotlin.reflect.jvm.jvmName | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotNull | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class SerializationClassTester { | ||
| // Same as app, or using app reference | ||
| val jacksonMapper = mapper | ||
| val kotlinxMapper = json | ||
|
|
||
| @Test | ||
| fun isIdenticalSerialization() { | ||
| val serializableClasses = findSerializableClasses("com.lagradost") | ||
| println("Number of serializable classes: ${serializableClasses.size}") | ||
|
|
||
| serializableClasses.forEach { kClass -> | ||
| val instance = Instancio.create(kClass.java) | ||
|
|
||
| val jacksonJson = jacksonMapper.writeValueAsString(instance) | ||
| val kotlinxJson = serializeWithKotlinx(kClass, instance) | ||
|
|
||
| assertEquals( | ||
| jacksonJson, | ||
| kotlinxJson, | ||
| """ | ||
| Serialization mismatch for: | ||
| ${kClass.qualifiedName} | ||
|
|
||
| Jackson: | ||
| $jacksonJson | ||
|
|
||
| Kotlinx: | ||
| $kotlinxJson | ||
|
|
||
| """.trimIndent() | ||
| ) | ||
| println("Identical serialization for: ${kClass.jvmName}") | ||
| } | ||
| } | ||
|
|
||
| @OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class) | ||
| @Test | ||
| fun isIdenticalDeserialization() { | ||
| val serializableClasses = findSerializableClasses("com.lagradost") | ||
| println("Number of serializable classes: ${serializableClasses.size}") | ||
|
|
||
| serializableClasses.forEach { kClass -> | ||
| val instance = Instancio.create(kClass.java) | ||
| // Convert to JSON to get example JSON object | ||
| // We prefer jackson here because the app may have many jackson JSON strings in local storage | ||
| val originalJson = jacksonMapper.writeValueAsString(instance) | ||
|
|
||
| // Create an object from the JSON using kotlinx | ||
| val serializer = | ||
| kClass.serializerOrNull() ?: kotlinxMapper.serializersModule.getContextual(kClass) | ||
| assertNotNull(serializer, "The class: ${kClass.jvmName} must be serializable!") | ||
| val kotlinxDecoded = kotlinxMapper.decodeFromString(serializer, originalJson) | ||
|
|
||
| // Create an object from the JSON using jackson | ||
| val mapperDecoded = jacksonMapper.readValue(originalJson, kClass.java) | ||
|
|
||
|
|
||
| // Deep inspect both object using the mapper toJson function. | ||
| // This deep equality check can be performed using other methods, but this just works. | ||
| val jacksonJson = mapperDecoded.toJson() | ||
| val kotlinxJson = kotlinxDecoded.toJson() | ||
|
|
||
| assertEquals( | ||
| jacksonJson, | ||
| kotlinxJson, | ||
| """ | ||
| Serialization mismatch for: | ||
| ${kClass.qualifiedName} | ||
|
|
||
| Jackson: | ||
| $jacksonJson | ||
|
|
||
| Kotlinx: | ||
| $kotlinxJson | ||
|
|
||
| """.trimIndent() | ||
| ) | ||
| println("Identical deserialization for: ${kClass.jvmName}") | ||
| } | ||
| } | ||
|
|
||
| private fun findSerializableClasses(packageName: String): List<KClass<*>> { | ||
| val context = InstrumentationRegistry | ||
| .getInstrumentation() | ||
| .targetContext | ||
|
|
||
| return ClassGraph() | ||
| .enableClassInfo() | ||
| .enableAnnotationInfo() | ||
| .overrideClassLoaders(context.classLoader) | ||
| .acceptPackages(packageName) | ||
| .scan() | ||
| .getClassesWithAnnotation(Serializable::class.java.name) | ||
| .mapNotNull { runCatching { Class.forName(it.name, false, context.classLoader).kotlin }.getOrNull() } | ||
| } | ||
|
|
||
| @OptIn(InternalSerializationApi::class) | ||
| @Suppress("UNCHECKED_CAST") | ||
| private fun serializeWithKotlinx( | ||
| kClass: KClass<*>, | ||
| value: Any | ||
| ): String { | ||
| val serializer = kClass.serializer() as KSerializer<Any> | ||
| return kotlinxMapper.encodeToString(serializer, value) | ||
| } | ||
| } | ||
157 changes: 157 additions & 0 deletions
157
app/src/androidTest/java/com/lagradost/cloudstream3/utils/serializers/SerializerTest.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package com.lagradost.cloudstream3.utils.serializers | ||
|
|
||
| import android.net.Uri | ||
| import com.lagradost.cloudstream3.utils.AppUtils.parseJson | ||
| import com.lagradost.cloudstream3.utils.AppUtils.toJson | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.KeepGeneratedSerializer | ||
| import kotlinx.serialization.Serializable | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| @KeepGeneratedSerializer | ||
| @Serializable(with = NonEmptyData.Serializer::class) | ||
| data class NonEmptyData( | ||
| val title: String = "", | ||
| val tags: List<String> = emptyList(), | ||
| val meta: Map<String, String> = emptyMap(), | ||
| val name: String = "hello", | ||
| ) { | ||
| object Serializer : NonEmptySerializer<NonEmptyData>(NonEmptyData.generatedSerializer()) | ||
| } | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| @KeepGeneratedSerializer | ||
| @Serializable(with = WriteOnlyData.Serializer::class) | ||
| data class WriteOnlyData( | ||
| val fieldA: String = "", | ||
| val fieldB: String = "", | ||
| ) { | ||
| object Serializer : WriteOnlySerializer<WriteOnlyData>( | ||
| WriteOnlyData.generatedSerializer(), | ||
| setOf("fieldB"), | ||
| ) | ||
| } | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| @KeepGeneratedSerializer | ||
| @Serializable(with = MultiWriteOnly.Serializer::class) | ||
| data class MultiWriteOnly( | ||
| val fieldA: String = "", | ||
| val fieldB: String = "", | ||
| val fieldC: String = "", | ||
| ) { | ||
| object Serializer : WriteOnlySerializer<MultiWriteOnly>( | ||
| MultiWriteOnly.generatedSerializer(), | ||
| setOf("fieldB", "fieldC"), | ||
| ) | ||
| } | ||
|
|
||
| @Serializable | ||
| data class UriData( | ||
| @Serializable(with = UriSerializer::class) | ||
| val uri: Uri = Uri.EMPTY, | ||
| ) | ||
|
|
||
| class SerializerTest { | ||
|
|
||
| @Test | ||
| fun nonEmptySerializerOmitsEmptyStrings() { | ||
| val data = NonEmptyData(title = "", name = "hello") | ||
| val result = data.toJson() | ||
| assertFalse(result.contains("title")) | ||
| assertTrue(result.contains("name")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonEmptySerializerOmitsEmptyLists() { | ||
| val data = NonEmptyData(tags = emptyList(), name = "hello") | ||
| val result = data.toJson() | ||
| assertFalse(result.contains("tags")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonEmptySerializerOmitsEmptyMaps() { | ||
| val data = NonEmptyData(meta = emptyMap(), name = "hello") | ||
| val result = data.toJson() | ||
| assertFalse(result.contains("meta")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonEmptySerializerKeepsNonEmptyFields() { | ||
| val data = NonEmptyData(title = "hello", tags = listOf("a"), meta = mapOf("k" to "v")) | ||
| val result = data.toJson() | ||
| assertTrue(result.contains("title")) | ||
| assertTrue(result.contains("tags")) | ||
| assertTrue(result.contains("meta")) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonEmptySerializerDoesNotAffectDeserialization() { | ||
| val input = """{"title":"hello","tags":["a"],"meta":{"k":"v"},"name":"world"}""" | ||
| val result = parseJson<NonEmptyData>(input) | ||
| assertEquals("hello", result.title) | ||
| assertEquals(listOf("a"), result.tags) | ||
| assertEquals(mapOf("k" to "v"), result.meta) | ||
| assertEquals("world", result.name) | ||
| } | ||
|
|
||
| @Test | ||
| fun writeOnlySerializerOmitsFieldOnSerialize() { | ||
| val data = WriteOnlyData(fieldA = "hello", fieldB = "secret") | ||
| val result = data.toJson() | ||
| assertTrue(result.contains("fieldA")) | ||
| assertFalse(result.contains("fieldB")) | ||
| } | ||
|
|
||
| @Test | ||
| fun writeOnlySerializerDeserializesNormally() { | ||
| val input = """{"fieldA":"hello","fieldB":"secret"}""" | ||
| val result = parseJson<WriteOnlyData>(input) | ||
| assertEquals("hello", result.fieldA) | ||
| assertEquals("secret", result.fieldB) | ||
| } | ||
|
|
||
| @Test | ||
| fun writeOnlySerializerDeserializesMissingAsDefault() { | ||
| val input = """{"fieldA":"hello"}""" | ||
| val result = parseJson<WriteOnlyData>(input) | ||
| assertEquals("hello", result.fieldA) | ||
| assertEquals("", result.fieldB) | ||
| } | ||
|
|
||
| @Test | ||
| fun writeOnlySerializerHandlesMultipleKeys() { | ||
| val data = MultiWriteOnly(fieldA = "hello", fieldB = "secret1", fieldC = "secret2") | ||
| val result = data.toJson() | ||
| assertTrue(result.contains("fieldA")) | ||
| assertFalse(result.contains("fieldB")) | ||
| assertFalse(result.contains("fieldC")) | ||
| } | ||
|
|
||
| @Test | ||
| fun uriSerializerSerializesUriToString() { | ||
| val data = UriData(uri = Uri.parse("https://example.com/path?query=1")) | ||
| val result = data.toJson() | ||
| assertTrue(result.contains("https://example.com/path?query=1")) | ||
| } | ||
|
|
||
| @Test | ||
| fun uriSerializerDeserializesStringToUri() { | ||
| val input = """{"uri":"https://example.com/path?query=1"}""" | ||
| val result = parseJson<UriData>(input) | ||
| assertEquals(Uri.parse("https://example.com/path?query=1"), result.uri) | ||
| } | ||
|
|
||
| @Test | ||
| fun uriSerializerRoundtripsCorrectly() { | ||
| val data = UriData(uri = Uri.parse("https://example.com/path?query=1")) | ||
| val encoded = data.toJson() | ||
| val decoded = parseJson<UriData>(encoded) | ||
| assertEquals(data.uri, decoded.uri) | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
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.
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.
hmm... I just thought of something... unless I am missing something.... this may not actually match a lot of times because things will be different when we swap
@JsonPropertywith@SerialNameand a couple of other changes.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.
I guess we could keep
@JsonPropertyfor now until migration is complete and then remove them all so we would have both@JsonPropertywith@SerialNamein the meantime, which this may be better anyway to ensure the fallback also properly works if something goes wrong with kotlinx serialization during migration as well...