diff options
Diffstat (limited to 'formats/json-tests/jvmTest/src/kotlinx/serialization/json/GsonCompatibilityTest.kt')
-rw-r--r-- | formats/json-tests/jvmTest/src/kotlinx/serialization/json/GsonCompatibilityTest.kt | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/formats/json-tests/jvmTest/src/kotlinx/serialization/json/GsonCompatibilityTest.kt b/formats/json-tests/jvmTest/src/kotlinx/serialization/json/GsonCompatibilityTest.kt new file mode 100644 index 00000000..99bc23f9 --- /dev/null +++ b/formats/json-tests/jvmTest/src/kotlinx/serialization/json/GsonCompatibilityTest.kt @@ -0,0 +1,56 @@ +package kotlinx.serialization.json + +import com.google.gson.* +import kotlinx.serialization.* +import org.junit.Test +import kotlin.test.* + +class GsonCompatibilityTest { + + @Serializable + data class Box(val d: Double, val f: Float) + + @Test + fun testNaN() { + checkCompatibility(Box(Double.NaN, 1.0f)) + checkCompatibility(Box(1.0, Float.NaN)) + checkCompatibility(Box(Double.NaN, Float.NaN)) + } + + @Test + fun testInfinity() { + checkCompatibility(Box(Double.POSITIVE_INFINITY, 1.0f)) + checkCompatibility(Box(1.0, Float.POSITIVE_INFINITY)) + checkCompatibility(Box(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)) + } + + @Test + fun testNumber() { + checkCompatibility(Box(23.9, 23.9f)) + } + + private fun checkCompatibility(box: Box) { + checkCompatibility(box, Gson(), Json) + checkCompatibility(box, GsonBuilder().serializeSpecialFloatingPointValues().create(), Json { allowSpecialFloatingPointValues = true }) + } + + private fun checkCompatibility(box: Box, gson: Gson, json: Json) { + val jsonResult = resultOrNull { json.encodeToString(box) } + val gsonResult = resultOrNull { gson.toJson(box) } + assertEquals(gsonResult, jsonResult) + + if (jsonResult != null && gsonResult != null) { + val jsonDeserialized: Box = json.decodeFromString(jsonResult) + val gsonDeserialized: Box = gson.fromJson(gsonResult, Box::class.java) + assertEquals(gsonDeserialized, jsonDeserialized) + } + } + + private fun resultOrNull(function: () -> String): String? { + return try { + function() + } catch (t: Throwable) { + null + } + } +} |