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.

Klaxon cannot deserialize to or from raw strings

See original GitHub issue

In Klaxon 2.0.10:

  • If an object serializes to a string, it cannot be deserialized
  • cannot deserialize a list of strings
    data class Person(val id: String, val firstName: String)

    /** This converter serializes a person to a string **/
    class PersonConverter: Converter<Person> {
        override fun toJson(value: Person) = "\"${value.id}:${value.firstName}\""
        override fun fromJson(jv: JsonValue) = jv.string?.let {
            val (id, firstName) = it.split(":")
            Person(id, firstName)
        } ?: throw KlaxonException("Invalid Person: $jv")
    }

    @Test
    fun deserializeStringListToObjectList() {
        val mapper = Klaxon().converter(PersonConverter())
        val obj = listOf(Person("1", "John"))

        val jv = mapper.toJsonString(obj)
        Assert.assertThat(jv, CoreMatchers.equalTo("[1:John]")) // Passes
        Assert.assertThat(mapper.parse(jv), CoreMatchers.equalTo(obj)) // Fails
    }

    @Test
    fun deserializeStringToObject() {
        val mapper = Klaxon().converter(PersonConverter())
        val obj = Person("1", "John")

        val jv = mapper.toJsonString(obj)
        Assert.assertThat(jv, CoreMatchers.equalTo("1:John")) // Passes
        Assert.assertThat(mapper.parse(jv), CoreMatchers.equalTo(obj))  // Fails
    }

    @Test
    fun deserializeStringListToStringList() {
        val obj = listOf("foo", "bar", "baz")
        val mapper = Klaxon()

        val jv = mapper.toJsonString(obj)
        Assert.assertThat(jv, CoreMatchers.equalTo("[\"foo\", \"bar\", \"baz\"]")) // Passes
        Assert.assertThat(mapper.parse(jv), CoreMatchers.equalTo(obj)) // Fails
    }

Now I get:

java.lang.ClassCastException: java.lang.String cannot be cast to com.beust.klaxon.JsonObject

    at com.prontoforms.pcns.auth.authorizer.MethodArnUnitTest.deserializeStringToObject(MethodArnUnitTest.kt:181)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

or

java.lang.ClassCastException: com.beust.klaxon.JsonArray cannot be cast to com.beust.klaxon.JsonObject

    at com.prontoforms.pcns.auth.authorizer.MethodArnUnitTest.deserializeStringListToObjectList(MethodArnUnitTest.kt:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
oharaandrew314commented, Jan 16, 2018

Ok, it obviously is wrong to serialize an object to a plain string. I’ll close and re-open with the real problem.

0reactions
oharaandrew314commented, Jan 16, 2018

There was indeed a mistake in my serializer. I needed to surround the result in quotes. I now no longer get parsing exceptions, but I still get cast exceptions. It seems that your converter only allows json objects to be at the root of a document, when, in reality, a json array can be a valid start of the document.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Klaxon gives error when reading JSON File : r/Kotlin
A Files has only one String field, not a list, and a Dirs has only one Files . I would guess the error...
Read more >
How to parse JSON in Kotlin?
I'm receiving a quite deep JSON object string from a service which I must parse to a JSON object and ...
Read more >
Strings - C# Programming Guide
Learn about strings in C# programming. See information on declaring and initializing strings, the immutability of string objects, and string ...
Read more >
Advanced JSON parsing techniques using Moshi and Kotlin
Fully manual parsing. The most basic way of parsing JSON using Moshi is to use the streaming API, which is similar to the...
Read more >
Hi all How to make scalar YearMonth and Year classes of Kotl
(although at least one Creator exists): cannot deserialize from Object value (no ... toString() override fun deserialize(raw: String, valueNode: ValueNode?)
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