json - Kotlin serialization of a HashMap - Stack Overflow

Got an issue with kotlin serialization where I could need some help:import kotlinx.serialization.json.

Got an issue with kotlin serialization where I could need some help:

import kotlinx.serialization.json.Json

fun main() {
    val someValue: Double = 1.0
    val someKey: String? = null    
        
    HashMap(readMapFromStorage()).also {
        it[someKey?:""] = someValue
    }.let {
        Json.encodeToString(it) // Cannot infer type for this parameter. Please specify it explicitly.
    }.let {
        runBlocking<Unit> { async { writeString("SomeCategory", it) } }
    }
}

private fun readMapFromStorage(): Map<String, Double> {
    return mapOf(
        "" to 1.0,
        "A" to 1.5, 
        "B" to 2.0,
    )
}

private fun writeString(key: String, value: String) {
    // do something clever
}

I receive an error in my IDE in line 10 in the call to Json.encodeToString(it):

Cannot infer type for this parameter. Please specify it explicitly.

Why is that? it is a HashMap<String, Double> here which (AFAIK) should be serializable. All tutorials I found demonstrate exactly what I tried (as far as I can tell). So why doesn't his work?

Got an issue with kotlin serialization where I could need some help:

import kotlinx.serialization.json.Json

fun main() {
    val someValue: Double = 1.0
    val someKey: String? = null    
        
    HashMap(readMapFromStorage()).also {
        it[someKey?:""] = someValue
    }.let {
        Json.encodeToString(it) // Cannot infer type for this parameter. Please specify it explicitly.
    }.let {
        runBlocking<Unit> { async { writeString("SomeCategory", it) } }
    }
}

private fun readMapFromStorage(): Map<String, Double> {
    return mapOf(
        "" to 1.0,
        "A" to 1.5, 
        "B" to 2.0,
    )
}

private fun writeString(key: String, value: String) {
    // do something clever
}

I receive an error in my IDE in line 10 in the call to Json.encodeToString(it):

Cannot infer type for this parameter. Please specify it explicitly.

Why is that? it is a HashMap<String, Double> here which (AFAIK) should be serializable. All tutorials I found demonstrate exactly what I tried (as far as I can tell). So why doesn't his work?

Share Improve this question asked Nov 20, 2024 at 19:48 arkaschaarkascha 43k8 gold badges61 silver badges95 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I suspect you miss an import for kotlinx.serialization.encodeToString. Without it we use a member function encodeToString which accepts 2 arguments, not 1 and the first argument is SerializationStrategy<T>. Extension function kotlinx.serialization.encodeToString accepts a single argument T. Still, error message is very confusing and misleading.

Please see a working example: https://pl.kotl.in/1g4sPuGDs

import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

fun main() {
    val someValue: Double = 1.0
    val someKey: String? = null

    HashMap(readMapFromStorage()).also {
        it[someKey?:""] = someValue
    }.let {
        Json.encodeToString(it) // Cannot infer type for this parameter. Please specify it explicitly.
    }.let {
        runBlocking<Unit> { async { writeString("SomeCategory", it) } }
    }
}

private fun readMapFromStorage(): Map<String, Double> {
    return mapOf(
        "" to 1.0,
        "A" to 1.5,
        "B" to 2.0,
    )
}

private fun writeString(key: String, value: String) {
    // do something clever
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742333393a4424168.html

相关推荐

  • json - Kotlin serialization of a HashMap - Stack Overflow

    Got an issue with kotlin serialization where I could need some help:import kotlinx.serialization.json.

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信