Kotlin / kmp-production-sample

This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you can download it from the App Store and Google Play. It's been designed to demonstrate how KMM can be used in real production projects.
https://kotlinlang.org/lp/mobile/
MIT License
2.01k stars 224 forks source link

Exception serialization json with unknown keys #67

Closed AnD010 closed 1 year ago

AnD010 commented 1 year ago

I have in my kmm this

fun createJson() = Json { isLenient = true; ignoreUnknownKeys = true; prettyPrint = true;}

fun createHttpClient(httpClientEngine: HttpClientEngine, json: Json, enableNetworkLogs: Boolean) = HttpClient(httpClientEngine) {
    install(ContentNegotiation) {
        json(json)
    }

    if (enableNetworkLogs) {
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.ALL

        }
    }
}

I had define my model like this

@Serializable
data class ResourceResponse (

  @SerialName("Id"            ) var Id               : String?           = null,
  @SerialName("Titulo"        ) var Titulo           : String?           = null,
  @SerialName("Descripcion"   ) var Descripcion      : String?           = null,
  @SerialName("TipoRecurso"   ) var TipoRecurso      : Int?              = null,
  @SerialName("RecursoData"   ) var RecursoData      : String?           = null,
  @SerialName("UrlRecurso"    ) var UrlRecurso       : String?           = null,
  @SerialName("FileType"      ) var FileType         : String?           = null,
  @SerialName("FileName"      ) var FileName         : String?           = null,
  @SerialName("File"          ) var File             : String?           = null,
  @SerialName("DataSrc"       ) var DataSrc          : String?           = null,
  @SerialName("IdYoutube"     ) var IdYoutube        : String?           = null,
  @SerialName("IdTrabajadores") var IdTrabajadores   : ArrayList<String>? = null,
)

My backend team add new filed to json "IdCreador" is a String. I don´t need this field, therefore, I dont´add this field to my ResourceResponse object, I had define ignoreUnknownKeys = true but I get the following error

  kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 278: Encountered an unknown key 'IdCreador' at path: $[0].IdTrabajadores Use 'ignoreUnknownKeys = true' in 'Json {}' builder to ignore unknown keys.
 JSON input: .....":null,"IdTrabajadores":null,"IdCreador":null

my dependencies file defines the following

        val ktorVersion = "2.3.2"
                //Ktor
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")
                implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
                implementation("io.ktor:ktor-client-auth:$ktorVersion")

I have tried to add it manually without using koin the dependency

    install(ContentNegotiation) {
        val json = Json {
            ignoreUnknownKeys = true
        }
        json(json)
    }

But the result is the same.... JsonDecodingException

pahill commented 1 year ago

Hi there - I assume this is your own code and not related to this sample?

May I ask that you please log a YouTrack issue for us for our support engineers to take a look at? That way they can involve you in any discussions easily.

You can create it using this link: https://youtrack.jetbrains.com/newIssue. I would log it under Ktor.

Kindly also send me the link here and I'll keep an eye out for it.

Thanks so much!

pahill commented 1 year ago

If you have any issues with creating it, please let me know and I'll see how I can help further.

Thank you

AnD010 commented 1 year ago

Yes, I wrote to Ktor team. Thanks @pahill