question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Json.encodeToString takes two arguments but docs show it taking only one

See original GitHub issue

Describe the bug

I am trying to use Kotlin serialization for JSON.

The docs show that the latest version is 1.0.1 so I believe I have the correct version, but the examples don’t work:

val data = Project("kotlinx.serialization", "Kotlin")
val string = Json.encodeToString(data)  

When I try the same thing I get this error:

Type mismatch: inferred type is Project but SerializationStrategy<TypeVariable(T)> was expected
e: test.kt: (13, 56): No value passed for parameter 'value'

Looking at the signature of that method, I see it takes two arguments, not just one:

fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String

I found out I can just do this instead and it works (but it’s pretty verbose and I would prefer if the documented behaviour worked!):

val serializer = serializer(Project::class.java)
val string = Json.encodeToString(serializer, data)  

Can you tell me whether the docs are wrong or the released code is wrong?

Environment

  • Kotlin version: 1.4.21
  • Library version: 1.0.1
  • Kotlin platforms: JVM
  • Gradle version: 6.7

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:22
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
renatoathaydescommented, Jan 11, 2021

For context, my Project class is actually a sealed class.

Something like this:

@Serializable
sealed class Base {
    data class Project(val name: String): Base()
}

In my tests, my imports are:

import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer

I’ve actually managed to get it working with one parameter after some struggle by adding this import:

import kotlinx.serialization.encodeToString

Not entirely sure this was the problem, but it looks like it… would be great if you could improve the docs to point this out?!

6reactions
gs-tscommented, Apr 21, 2021

if you have issues with the imports that have the same signature encodeToString, then use import alias:

import kotlinx.serialization.encodeToString as myJsonEncode and then Json.myJsonEncode(data)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type mismatch when serializing data class - Stack Overflow
The function that only requires the value parameter is implemented as an extension function, so you need to add import
Read more >
Serialization: Json.encodeToString uses only changed variables
Hello. I'm trying to encode a data class this way: Json.encodeToString(RequestData.serializer(), requestData) But in the result I got only ...
Read more >
json_encode - Manual - PHP
Returns a string containing the JSON representation of the supplied value . If the parameter is an array or object, it will be...
Read more >
Migrating from 1.6.x to 2.0.x - Ktor
import io.ktor.serialization.kotlinx.json.* ... Let's take a look at several examples of migrating 1.6.x tests to 2.0.0: ...
Read more >
Karate | karate - GitHub Pages
The features parameter in the annotation can take an array, so if you wanted to associate multiple feature files with a JUnit test,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found