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.

Initializing a Json instance on a background thread fails in native

See original GitHub issue

In 0.11.0, when you create a Json instance on a background thread, K/N throws an IncorrectDereferenceException because initializing one touches the private defaultJsonModule global here. I believe the fix is to just mark that global as @SharedImmutable.

You can workaround this by declaring a global Json that’s immutable, which (I think) will get setup and frozen during K/N runtime init. Seems to be working locally

@SharedImmutable
private val DEFAULT_JSON = Json(your_config)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
MichaelOliveiraBxcommented, May 9, 2019

You need to add kotlin.native.initRuntimeIfNeeded() before calling DEFAULT_JSON .parse

I had the same problem Here is my code that works :

` // INTERNAL @kotlin.native.ThreadLocal private val parser = Json.nonstrict // or strict in your case

@UnstableDefault
private fun parseDocument(document: String) {

    // mandatory if call in another thread that main thread
    kotlin.native.initRuntimeIfNeeded()

    val parsedJson = try {
        parser.parse(
            Serializer,
            document
        )
    } catch (e: JsonParsingException) {
        println("parseDocument ERROR $e")
        return
    }
}
0reactions
benasher44commented, May 23, 2019

Yep agree. They can be closed at the same time, or feel free to close this as dup 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

unable to create new native thread while using Executor ...
The main idea of the solution is to initialize the ExecutorService instance only once and reuse it on each request (see comments above)....
Read more >
JSON and serialization - Flutter documentation
The cookbook contains a more comprehensive worked example of using JSON model classes, using an isolate to parse the JSON file on a...
Read more >
Loading JSON data with async/await – iOS & SwiftUI Tutorial
The tutorial and sample project shows how JSON data is loaded in the background using the new Xcode 13 feature async/await via the ......
Read more >
Karate | Test Automation Made Simple.
For example a lot of Java projects directly (or indirectly) depend on Netty or ... Now that we have seen how JSON is...
Read more >
After recent update json schema validation fails - Airbyte
Maybe some magic is happening in the background?! In short, I keep getting that json schema validation error and cannot run my connections....
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