Json.encodeToString takes two arguments but docs show it taking only one
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:22
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For context, my
Project
class is actually a sealed class.Something like this:
In my tests, my imports are:
I’ve actually managed to get it working with one parameter after some struggle by adding this import:
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?!
if you have issues with the imports that have the same signature
encodeToString
, then use import alias:import kotlinx.serialization.encodeToString as myJsonEncode
and thenJson.myJsonEncode(data)