Skip to content

Commit 0f155ae

Browse files
committed
added dataframe-json as excludable dependency to dataframe-csv and dataframe-excel
1 parent 341b7c8 commit 0f155ae

File tree

6 files changed

+44
-60
lines changed

6 files changed

+44
-60
lines changed

dataframe-csv/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ repositories {
2525
dependencies {
2626
api(projects.core)
2727

28+
// for reading/writing JSON <-> DataFrame/DataRow in CSV/TSV/Delim
29+
// can safely be excluded when working without JSON and only writing flat dataframes
30+
api(projects.dataframeJson)
31+
2832
// for csv reading
2933
api(libs.deephavenCsv)
3034
// for csv writing

dataframe-csv/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/jsonHelper.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

dataframe-csv/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/io/writeDelim.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.jetbrains.kotlinx.dataframe.documentation.DelimParams.CSV_DELIMITER
1212
import org.jetbrains.kotlinx.dataframe.documentation.DelimParams.WRITER_WRITE
1313
import org.jetbrains.kotlinx.dataframe.io.AdjustCSVFormat
1414
import org.jetbrains.kotlinx.dataframe.io.QuoteMode
15+
import org.jetbrains.kotlinx.dataframe.io.toJson
1516
import org.apache.commons.csv.QuoteMode as ApacheQuoteMode
1617

1718
/**
@@ -57,8 +58,22 @@ internal fun writeDelimImpl(
5758
df.forEach {
5859
val values = it.values().map {
5960
when (it) {
60-
is AnyRow -> it.toJson()
61-
is AnyFrame -> it.toJson()
61+
is AnyRow -> try {
62+
it.toJson()
63+
} catch (_: NoClassDefFoundError) {
64+
error(
65+
"Encountered a DataFrame when writing to csv/tsv/delim. This needs to be converted to JSON, so the dataframe-json dependency is required.",
66+
)
67+
}
68+
69+
is AnyFrame -> try {
70+
it.toJson()
71+
} catch (_: NoClassDefFoundError) {
72+
error(
73+
"Encountered a DataRow when writing to csv/tsv/delim. This needs to be converted to JSON, so the dataframe-json dependency is required.",
74+
)
75+
}
76+
6277
else -> it
6378
}
6479
}

dataframe-excel/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ repositories {
1717
dependencies {
1818
api(projects.core)
1919
api(libs.poi)
20+
21+
// for writing DataFrame/DataRow -> JSON in Excel cells
22+
// can safely be excluded when writing only flat dataframes
23+
api(projects.dataframeJson)
24+
2025
implementation(libs.poi.ooxml)
2126

2227
implementation(libs.kotlin.datetimeJvm)

dataframe-excel/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/jsonHelper.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

dataframe-excel/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/xlsx.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,25 @@ public fun <T> DataFrame<T>.writeExcel(
668668

669669
private fun Cell.setCellValueByGuessedType(any: Any) =
670670
when (any) {
671-
is AnyRow -> this.setCellValue(any.toJson())
671+
is AnyRow -> this.setCellValue(
672+
try {
673+
any.toJson()
674+
} catch (_: NoClassDefFoundError) {
675+
error(
676+
"Encountered a DataRow when writing to an Excel cell. This needs to be converted to JSON, so the dataframe-json dependency is required.",
677+
)
678+
},
679+
)
672680

673-
is AnyFrame -> this.setCellValue(any.toJson())
681+
is AnyFrame -> this.setCellValue(
682+
try {
683+
any.toJson()
684+
} catch (_: NoClassDefFoundError) {
685+
error(
686+
"Encountered a DataFrame when writing to an Excel cell. This needs to be converted to JSON, so the dataframe-json dependency is required.",
687+
)
688+
},
689+
)
674690

675691
is Number -> this.setCellValue(any.toDouble())
676692

0 commit comments

Comments
 (0)