@@ -9,8 +9,8 @@ import org.jetbrains.kotlinx.dataframe.ColumnsContainer
9
9
import org.jetbrains.kotlinx.dataframe.DataColumn
10
10
import org.jetbrains.kotlinx.dataframe.api.indices
11
11
import org.jetbrains.kotlinx.dataframe.api.isList
12
- import org.jetbrains.kotlinx.dataframe.api.name
13
12
import org.jetbrains.kotlinx.dataframe.api.rows
13
+ import org.jetbrains.kotlinx.dataframe.api.schema
14
14
import org.jetbrains.kotlinx.dataframe.api.take
15
15
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
16
16
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
@@ -22,12 +22,16 @@ import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.KOTLIN_DATAFRAM
22
22
import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.METADATA
23
23
import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.NCOL
24
24
import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.NROW
25
+ import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.TYPE
26
+ import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.TYPES
25
27
import org.jetbrains.kotlinx.dataframe.impl.io.SerializationKeys.VERSION
26
28
import org.jetbrains.kotlinx.dataframe.io.Base64ImageEncodingOptions
27
29
import org.jetbrains.kotlinx.dataframe.io.arrayColumnName
28
30
import org.jetbrains.kotlinx.dataframe.io.valueColumnName
31
+ import org.jetbrains.kotlinx.dataframe.name
29
32
import org.jetbrains.kotlinx.dataframe.ncol
30
33
import org.jetbrains.kotlinx.dataframe.nrow
34
+ import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
31
35
import org.jetbrains.kotlinx.dataframe.typeClass
32
36
import java.awt.image.BufferedImage
33
37
import java.io.IOException
@@ -53,9 +57,13 @@ internal object SerializationKeys {
53
57
const val VERSION = " \$ version"
54
58
const val COLUMNS = " columns"
55
59
const val KOTLIN_DATAFRAME = " kotlin_dataframe"
60
+ const val TYPE = " type"
61
+ const val TYPES = " types"
56
62
}
57
63
58
- internal const val SERIALIZATION_VERSION = " 2.0.0"
64
+ // See docs/serialization_format.md for a description of
65
+ // serialization versions and format.
66
+ internal const val SERIALIZATION_VERSION = " 2.1.0"
59
67
60
68
internal fun KlaxonJson.encodeRowWithMetadata (
61
69
frame : ColumnsContainer <* >,
@@ -65,24 +73,39 @@ internal fun KlaxonJson.encodeRowWithMetadata(
65
73
): JsonObject ? {
66
74
val values = frame.columns().map { col ->
67
75
when (col) {
68
- is ColumnGroup <* > -> obj(
69
- DATA to encodeRowWithMetadata(col, index, rowLimit, imageEncodingOptions),
70
- METADATA to obj(KIND to ColumnKind .Group .toString())
71
- )
72
-
76
+ is ColumnGroup <* > -> {
77
+ val schema = col.schema()
78
+ obj(
79
+ DATA to encodeRowWithMetadata(col, index, rowLimit, imageEncodingOptions),
80
+ METADATA to obj(
81
+ KIND to ColumnKind .Group .toString(),
82
+ COLUMNS to schema.columns.keys,
83
+ TYPES to schema.columns.values.map { columnSchema ->
84
+ createJsonTypeDescriptor(columnSchema)
85
+ }
86
+ ),
87
+ )
88
+ }
73
89
is FrameColumn <* > -> {
74
- val data = if (rowLimit == null ) encodeFrameWithMetadata(col[index], null , imageEncodingOptions)
75
- else encodeFrameWithMetadata(col[index].take(rowLimit), rowLimit, imageEncodingOptions)
90
+ val data = if (rowLimit == null ) {
91
+ encodeFrameWithMetadata(col[index], null , imageEncodingOptions)
92
+ } else {
93
+ encodeFrameWithMetadata(col[index].take(rowLimit), rowLimit, imageEncodingOptions)
94
+ }
95
+ val schema = col.schema.value
76
96
obj(
77
97
DATA to data,
78
98
METADATA to obj(
79
99
KIND to ColumnKind .Frame .toString(),
100
+ COLUMNS to schema.columns.keys,
101
+ TYPES to schema.columns.values.map { columnSchema ->
102
+ createJsonTypeDescriptor(columnSchema)
103
+ },
80
104
NCOL to col[index].ncol,
81
105
NROW to col[index].nrow
82
106
)
83
107
)
84
108
}
85
-
86
109
else -> encodeValue(col, index, imageEncodingOptions)
87
110
}.let { col.name to it }
88
111
}
@@ -148,6 +171,16 @@ private fun encodeBufferedImageAsBase64(
148
171
}
149
172
}
150
173
174
+ private fun createJsonTypeDescriptor (columnSchema : ColumnSchema ): JsonObject {
175
+ return JsonObject (
176
+ mutableMapOf (KIND to columnSchema.kind.toString()).also {
177
+ if (columnSchema.kind == ColumnKind .Value ) {
178
+ it.put(TYPE , columnSchema.type.toString())
179
+ }
180
+ }
181
+ )
182
+ }
183
+
151
184
internal fun KlaxonJson.encodeFrameWithMetadata (
152
185
frame : AnyFrame ,
153
186
rowLimit : Int? = null,
@@ -257,6 +290,9 @@ internal fun KlaxonJson.encodeDataFrameWithMetadata(
257
290
VERSION to SERIALIZATION_VERSION ,
258
291
METADATA to obj(
259
292
COLUMNS to frame.columnNames(),
293
+ TYPES to frame.schema().columns.values.map { colSchema ->
294
+ createJsonTypeDescriptor(colSchema)
295
+ },
260
296
NROW to frame.rowsCount(),
261
297
NCOL to frame.columnsCount()
262
298
),
0 commit comments