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.

JVM Json Serialization Broken with Array<Array<Primitive> types

See original GitHub issue

Describe the bug

Given the following code

@Serializable
    data class Matrix2(val data: Array<Array<Int>>) {
        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (other == null || this::class != other::class) return false

            other as Matrix2

            if (!data.contentDeepEquals(other.data)) return false

            return true
        }

        override fun hashCode(): Int {
            return data.contentDeepHashCode()
        }
    }

    @Test
    fun test() {
        val m = Matrix2(
            arrayOf(
                arrayOf(1, 0, 0),
                arrayOf(0, 1, 0),
                arrayOf(0, 0, 1)
            )
        )
        val json = Json.encodeToString(m)
        val result: Matrix2 = Json.decodeFromString(json)

        assertEquals(m, result)
    }

The test should pass, but on the JVM it fails with the following error.

java.lang.ClassCastException: class [[Ljava.lang.Object; cannot be cast to class [[Ljava.lang.Integer; ([[Ljava.lang.Object; and [[Ljava.lang.Integer; are in module java.base of loader 'bootstrap')
	at serialization.JsonSerializationTest$Matrix2$$serializer.deserialize(JsonSerializationTest.kt:68)
	at serialization.JsonSerializationTest$Matrix2$$serializer.deserialize(JsonSerializationTest.kt:68)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:63)
	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:32)
	at kotlinx.serialization.json.Json.decodeFromString(Json.kt:85)
	at raytracer.serialization.JsonSerializationTest.test(JsonSerializationTest.kt:111)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)

The generic type of Array<Array<Int> can be changed to any other primitive and the same behavior occurs. On js targets and native targets the code works as expected.

Expected behavior

Expected behavior would be that the Matrix2 class would be able to be serialized and deserialized on all target platforms.

Environment kotlin = 1.4.32 kotlinx-serialization = 1.1.0 gradle = 7.0 jvm = 11.0.2 (Oracle Corporation 11.0.2+9)

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
qwwdfsadcommented, Oct 6, 2021

Fixed in Kotlin 1.5.0+

1reaction
sandwwraithcommented, Oct 14, 2021

@judos That was a bug in the compiler plugin, that has the same versioning as the Kotlin compiler itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing Java arrays with Jackson - json - Stack Overflow
The best solution I've found for now is checking before serialization if the object is an array (Class.isArray) and then doing the ...
Read more >
Problems deserializing primitive `long` field while using ...
I am trying to serialize a bunch of basic plain old java objects from libraries we are using (so we cannot modify the...
Read more >
Working With JSON Data in Python - GeeksforGeeks
The process of encoding JSON is usually called serialization. This term refers to the transformation of data into a series of bytes (hence ......
Read more >
Serialization Guide - Json.NET
The Json.NET serializer can serialize a wide variety of .NET objects. This guide looks at how it works, first at a high level...
Read more >
Working with JSON data in Google Standard SQL | BigQuery
JSON can contain JSON arrays, which are not directly equivalent to an ARRAY<JSON> type in BigQuery. You can use the following functions to...
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