[PM-19627] Move Json extension functions to core module

Move JsonExtensions and related tests to `core` module.

- Moved `decodeFromStringOrNull` and related testing from `app` and `authenticator` modules to `core` module.
- Updated all usages of `decodeFromStringOrNull` in `app` and `authenticator` to the new location in `core`.
- Deleted unused `JsonExtensionsTest` and `JsonExtensions.kt` in `app` and `authenticator`.
- Updated dependencies for core.
This commit is contained in:
Patrick Honkonen
2025-03-27 17:37:45 -04:00
parent 5d5bc25a45
commit 1eced037a4
20 changed files with 19 additions and 91 deletions

View File

@@ -3,8 +3,8 @@ package com.bitwarden.authenticator.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.authenticator.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
import com.bitwarden.authenticator.data.platform.datasource.disk.model.ServerConfig
import com.bitwarden.authenticator.data.platform.util.decodeFromStringOrNull
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.decodeFromStringOrNull
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onSubscription
import kotlinx.serialization.json.Json

View File

@@ -3,8 +3,8 @@ package com.bitwarden.authenticator.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.authenticator.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration
import com.bitwarden.authenticator.data.platform.util.decodeFromStringOrNull
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.decodeFromStringOrNull
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onSubscription
import kotlinx.serialization.json.Json

View File

@@ -1,19 +0,0 @@
package com.bitwarden.authenticator.data.platform.util
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
/**
* Attempts to decode the given JSON [string] into the given type [T]. If there is an error in
* processing the JSON or deserializing it to an instance of [T], `null` will be returned.
*/
inline fun <reified T> Json.decodeFromStringOrNull(
string: String,
): T? =
try {
decodeFromString(string = string)
} catch (e: SerializationException) {
null
} catch (e: IllegalArgumentException) {
null
}

View File

@@ -1,55 +0,0 @@
package com.bitwarden.authenticator.data.platform.util
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
class JsonExtensionsTest {
private val json = Json
@Test
fun `decodeFromStringOrNull for invalid JSON should return null`() {
assertNull(
json.decodeFromStringOrNull<TestData>(
"""
{]
""",
),
)
}
@Test
fun `decodeFromStringOrNull for valid JSON but an incorrect model should return null`() {
assertNull(
json.decodeFromStringOrNull<TestData>(
"""
{}
""",
),
)
}
@Test
fun `decodeFromStringOrNull for valid JSON and a correct model should parse correctly`() {
assertEquals(
TestData(
data = "test",
),
json.decodeFromStringOrNull<TestData>(
"""
{
"data": "test"
}
""",
),
)
}
}
@Serializable
private data class TestData(
@SerialName("data") val data: String,
)