Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent Klaxon from using default reflective serializer #621

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ private val valueTypes =
setOf(Boolean::class, Double::class, Int::class, Float::class, Long::class, Short::class, Byte::class)

internal fun KlaxonJson.encodeValue(col: AnyCol, index: Int): Any? = when {
col.isList() -> col[index]?.let { array(it as List<*>) } ?: array()
col.isList() -> col[index]?.let { list ->
val values = (list as List<*>).map {
when (it) {
null, is Int, is Double, is Float, is Long, is Boolean, is Short, is Byte -> it
// Klaxon default serializers will try to use reflection and can sometimes fail.
// We can't have exceptions in Notebook DataFrame renderer
else -> it.toString()
}
}
array(values)
} ?: array()
col.typeClass in valueTypes -> {
val v = col[index]
if ((v is Double && v.isNaN()) || (v is Float && v.isNaN())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package org.jetbrains.kotlinx.dataframe.io
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.matchers.collections.shouldBeIn
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotContain
import io.kotest.matchers.types.instanceOf
import io.kotest.matchers.types.shouldBeInstanceOf
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlinx.dataframe.AnyFrame
import org.jetbrains.kotlinx.dataframe.DataFrame
Expand Down Expand Up @@ -1060,4 +1062,19 @@ class JsonTests {
val decodedData = contributors[DATA] as JsonArray<*>
decodedData.size shouldBe nestedFrameRowLimit
}

@Test
fun `serialize column with list of objects`() {
val df = dataFrameOf("col")(Regex(".+").findAll("abc").toList())
val json = shouldNotThrowAny { df.toJson() }
val list = DataFrame.readJsonStr(json)["col"][0].shouldBeInstanceOf<List<*>>()
list[0].shouldBeInstanceOf<String>()
}

@Test
fun `serialize column with list of primitives`() {
val df = dataFrameOf("col")(listOf(1, 2, 3))
val json = df.toJson()
DataFrame.readJsonStr(json) shouldBe df
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ private val valueTypes =
setOf(Boolean::class, Double::class, Int::class, Float::class, Long::class, Short::class, Byte::class)

internal fun KlaxonJson.encodeValue(col: AnyCol, index: Int): Any? = when {
col.isList() -> col[index]?.let { array(it as List<*>) } ?: array()
col.isList() -> col[index]?.let { list ->
val values = (list as List<*>).map {
when (it) {
null, is Int, is Double, is Float, is Long, is Boolean, is Short, is Byte -> it
// Klaxon default serializers will try to use reflection and can sometimes fail.
// We can't have exceptions in Notebook DataFrame renderer
else -> it.toString()
}
}
array(values)
} ?: array()
col.typeClass in valueTypes -> {
val v = col[index]
if ((v is Double && v.isNaN()) || (v is Float && v.isNaN())) {
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package org.jetbrains.kotlinx.dataframe.io
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.matchers.collections.shouldBeIn
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotContain
import io.kotest.matchers.types.instanceOf
import io.kotest.matchers.types.shouldBeInstanceOf
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlinx.dataframe.AnyFrame
import org.jetbrains.kotlinx.dataframe.DataFrame
Expand Down Expand Up @@ -1060,4 +1062,19 @@ class JsonTests {
val decodedData = contributors[DATA] as JsonArray<*>
decodedData.size shouldBe nestedFrameRowLimit
}

@Test
fun `serialize column with list of objects`() {
val df = dataFrameOf("col")(Regex(".+").findAll("abc").toList())
val json = shouldNotThrowAny { df.toJson() }
val list = DataFrame.readJsonStr(json)["col"][0].shouldBeInstanceOf<List<*>>()
list[0].shouldBeInstanceOf<String>()
}

@Test
fun `serialize column with list of primitives`() {
val df = dataFrameOf("col")(listOf(1, 2, 3))
val json = df.toJson()
DataFrame.readJsonStr(json) shouldBe df
}
}