All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.features.LongAsStringTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.features

import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.json.*
import kotlin.test.*

class LongAsStringTest : JsonTestBase() {
    @Serializable
    data class HasLong(@Serializable(LongAsStringSerializer::class) val l: Long)

    @Test
    fun canSerializeAsStringAndParseBack() = parametrizedTest { jsonTestingMode ->
        val original = HasLong(Long.MAX_VALUE - 1)
        val str = default.encodeToString(HasLong.serializer(), original, jsonTestingMode)
        assertEquals("""{"l":"9223372036854775806"}""", str)
        val restored = default.decodeFromString(HasLong.serializer(), str, jsonTestingMode)
        assertEquals(original, restored)
    }

    @Test
    fun canNotDeserializeInvalidString() = parametrizedTest { jsonTestingMode ->
        val str = """{"l": "this is definitely not a long"}"""
        assertFailsWith { default.decodeFromString(HasLong.serializer(), str, jsonTestingMode) }
        val str2 = """{"l": "1000000000000000000000"}""" // toooo long for Long
        assertFailsWith { default.decodeFromString(HasLong.serializer(), str2, jsonTestingMode) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy