JVM Json Serialization Broken with Array<Array<Primitive> types
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in Kotlin 1.5.0+
@judos That was a bug in the compiler plugin, that has the same versioning as the Kotlin compiler itself.