https://kotlinlang.org logo
#serialization
Title
# serialization
k

khairil.ushan

08/01/2018, 12:01 PM
I'm trying to use kotlinx.serialization in a MPP and run into this error. Is there something that I do wrong? 😞
Copy code
Exception in thread "ForkJoinPool.commonPool-worker-1" kotlinx.coroutines.experimental.CompletionHandlerException: Exception in completion handler InvokeOnCompletion[InvokeOnCompletion@55c536ef] for DeferredCoroutine{Completed}@48184828
	at kotlinx.coroutines.experimental.JobSupport.notifyCompletion(JobSupport.kt:1022)
	at kotlinx.coroutines.experimental.JobSupport.completeUpdateState(JobSupport.kt:229)
	at kotlinx.coroutines.experimental.JobSupport.updateState(JobSupport.kt:157)
	at kotlinx.coroutines.experimental.JobSupport.makeCompletingInternal(JobSupport.kt:568)
	at kotlinx.coroutines.experimental.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.kt:552)
	at kotlinx.coroutines.experimental.AbstractCoroutine.resume(AbstractCoroutine.kt:103)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
	at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:150)
	at kotlinx.coroutines.experimental.AbstractContinuation.run(AbstractContinuation.kt:19)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.NoClassDefFoundError: kotlinx/serialization/json/JSON
	at com.khairilushan.mpp.datasource.ProjectDataSource$Network$searchProject$$inlined$requestJson$1.invoke(NetworkService.kt:59)
	at com.khairilushan.mpp.datasource.ProjectDataSource$Network$searchProject$$inlined$requestJson$1.invoke(NetworkService.kt:10)
	at io.ktor.common.client.Promise.complete(utils.kt:20)
	at io.ktor.common.client.JvmUtilsKt$promise$$inlined$also$lambda$1.invoke(JvmUtils.kt:13)
	at io.ktor.common.client.JvmUtilsKt$promise$$inlined$also$lambda$1.invoke(JvmUtils.kt)
	at kotlinx.coroutines.experimental.InvokeOnCompletion.invoke(JobSupport.kt:906)
	at kotlinx.coroutines.experimental.JobSupport.notifyCompletion(JobSupport.kt:1018)
	... 22 more
Caused by: java.lang.ClassNotFoundException: kotlinx.serialization.json.JSON
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 29 more
Here is my NetworkService.kt
Copy code
abstract class NetworkService {

  open val protocol = URLProtocol.HTTPS

  open val port = 443

  open val baseUrl = "<http://api.github.com|api.github.com>"

  open val httpMethod = HttpMethod.Get

  open val requestBody: String? = null

  open val headers: Map<String, List<String>> = mapOf()

  abstract val path: String

  val client: HttpClient by lazy { HttpClient() }

//  inline fun <reified ResultType : Any> requestJson(
//    params: Map<String, String>,
//    crossinline completion: (ResultType?, Throwable?) -> Unit
//  ) {
//    request(params) { result: String?, error: Throwable? ->
//      if (result != null && error == null) {
//        completion(JSON.parse(result), null)
//      } else if (error != null) {
//        completion(null, error)
//      }
//    }
//  }

  inline fun <reified ResultType: Any>requestJson(
    params: Map<String, String>,
    crossinline completion: (ResultType?, Throwable?) -> Unit
  ) {
    promise {
      client.request {
        method = httpMethod
        body = requestBody
        headers = this@NetworkService.headers.toMutableMap()
        url.apply {
          protocol = this@NetworkService.protocol
          port = this@NetworkService.port
          host = baseUrl
          val query = params.map { "${it.key}=${it.value}" }.joinToString("&")
          encodedPath = "$path?$query"
        }
      }
    }.then {
      completion(JSON.parse(it.body), null)
    }.catch {
      completion(null, it)
    }
  }

}
That
NetworkService.kt:59
is this one >
completion(JSON.parse(it.body), null)
k

karelpeeters

08/01/2018, 12:14 PM
Does it work fine without any coroutine stuff?
It just looks like you don't have the runtime dependencies in the classpath or something.
k

khairil.ushan

08/01/2018, 12:47 PM
On my Project level build.gradle I have this
Copy code
classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serialization_version"
On my common module's build.gradle, I added this
Copy code
compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
And in my jvm module's build.gradle I have this
Copy code
compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
Aaah I add another buildScript block in my jvm module that contains serializations classpath and the error is gone. hmm Before i put serializations classpath on my project level build.gradle.
k

karelpeeters

08/01/2018, 12:56 PM
If it works... 🙂
k

khairil.ushan

08/01/2018, 1:01 PM
ahah. Oh ya btw @karelpeeters another question if you dont mind. If i have large json, but in my model i just want to parse some field, why I got error like
Copy code
Caused by: kotlinx.serialization.SerializationException: Strict JSON encountered unknown key: id
I dont want to parse that
id
field. Is there any configuration for this?
k

karelpeeters

08/01/2018, 1:03 PM
I don't know really, better ask in the main channel.
k

khairil.ushan

08/01/2018, 1:06 PM
Oh sorry, just do a little Googling and found the answer.
JSON.nonstrict.parse()
😄
k

karelpeeters

08/01/2018, 1:06 PM
Even better!
6 Views