Skip to content
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

Empty json and Serializable class with no properties or only @EncodedDefault properties #1850

Closed
JavierSegoviaCordoba opened this issue Jan 27, 2022 · 3 comments

Comments

@JavierSegoviaCordoba
Copy link

JavierSegoviaCordoba commented Jan 27, 2022

Describe the bug

I am getting a server response which is an empty json { }, I am setting a Serializable class with no properties but I get this exception:

To Reproduce

Set an empty json ({}) and a @Serializable class SomeClass with no properties.

Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected start of the object '{', but had '{' instead
JSON input: {}

Expected behavior

If I get an empty json and I set a @Serializable class with no properties, I expect it to directly work (use case strong typing some error responses by the backend).

If I set a data class which contains one or more @EncodedDefault properties, they should be set.

Environment

  • Kotlin version: 1.6.10
  • Library version: 1.3.2
  • Kotlin platforms: KMP with JVM
  • Gradle version: 7.3.3
@ArcticLampyrid
Copy link

There is no @EncodedDefault but @EncodeDefault.
And @EncodeDefault is not for marking a property to be optional. What you mean may be a property with default value.

@Target(allowedTargets = [AnnotationTarget.PROPERTY])
annotation class EncodeDefault(mode: EncodeDefault.Mode)

Controls whether the target property is serialized when its value is equal to a default value, regardless of the format settings. Does not affect decoding and deserialization process.

@sandwwraith
Copy link
Member

I executed the following test:

    @Serializable class SomeClass

    @Test
    fun testEmpty() {
        val input = "{}"
        println(Json.decodeFromString<SomeClass>(input))
        println(default.decodeFromString<SomeClass>(input))
        println(lenient.decodeFromString<SomeClass>(input))
    }

and it works just fine.

Maybe it has something to do with the encoding of server's response. For example, it may be incorrectly inserted BOM in UTF-8: #1168

@JavierSegoviaCordoba
Copy link
Author

Yeah, looks like Ktor issue, I will move to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants