Skip to content

Commit cf43401

Browse files
committed
Refactor json element matching and little refactor encodeFrame
1 parent 79c294e commit cf43401

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/readJson.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import kotlinx.serialization.json.floatOrNull
1313
import kotlinx.serialization.json.int
1414
import kotlinx.serialization.json.intOrNull
1515
import kotlinx.serialization.json.jsonArray
16-
import kotlinx.serialization.json.jsonPrimitive
1716
import kotlinx.serialization.json.long
1817
import kotlinx.serialization.json.longOrNull
1918
import org.jetbrains.kotlinx.dataframe.AnyCol
@@ -201,6 +200,8 @@ internal fun fromJsonListAnyColumns(
201200
)
202201
}
203202

203+
is JsonNull -> collector.add(null)
204+
204205
is JsonPrimitive -> {
205206
when {
206207
v.content == "NaN" -> {
@@ -214,7 +215,6 @@ internal fun fromJsonListAnyColumns(
214215
v.longOrNull != null -> collector.add(v.long)
215216
v.doubleOrNull != null -> collector.add(v.double)
216217
v.floatOrNull != null -> collector.add(v.float)
217-
v.jsonPrimitive is JsonNull -> collector.add(null)
218218
}
219219
}
220220

@@ -513,6 +513,7 @@ internal fun fromJsonListArrayAndValueColumns(
513513
when (v) {
514514
is JsonObject -> collector.add(null)
515515
is JsonArray -> collector.add(null)
516+
is JsonNull -> collector.add(null)
516517
is JsonPrimitive -> {
517518
when {
518519
v.content == "NaN" -> {
@@ -526,8 +527,6 @@ internal fun fromJsonListArrayAndValueColumns(
526527
v.longOrNull != null -> collector.add(v.long)
527528
v.doubleOrNull != null -> collector.add(v.double)
528529
v.floatOrNull != null -> collector.add(v.float)
529-
v is JsonNull -> collector.add(null)
530-
else -> collector.add(v)
531530
}
532531
}
533532

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/writeJson.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,11 @@ internal fun encodeFrame(frame: AnyFrame): JsonArray {
299299
val arraysAreFrames = arrayColumn?.kind() == ColumnKind.Frame
300300

301301
val data = frame.indices().map { rowIndex ->
302-
valueColumn?.get(rowIndex) ?: arrayColumn?.get(rowIndex)?.let {
303-
if (arraysAreFrames) encodeFrame(it as AnyFrame) else null
304-
} ?: encodeRow(frame, rowIndex)
302+
when {
303+
valueColumn != null -> valueColumn[rowIndex]
304+
arrayColumn != null -> arrayColumn[rowIndex]?.let { if (arraysAreFrames) encodeFrame(it as AnyFrame) else null }
305+
else -> encodeRow(frame, rowIndex)
306+
}
305307
}
306308

307309
return buildJsonArray { addAll(data.map { convert(it) }) }

0 commit comments

Comments
 (0)