-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDataFrameJsonWriter.class.st
More file actions
175 lines (152 loc) · 4.24 KB
/
DataFrameJsonWriter.class.st
File metadata and controls
175 lines (152 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Class {
#name : #DataFrameJsonWriter,
#superclass : #DataFrameWriter,
#instVars : [
'orient',
'pretty',
'writeMap'
],
#category : #'DataFrame-IO-Core'
}
{ #category : #converting }
DataFrameJsonWriter class >> convertToColumns: aDataFrame [
"Converts aDataFrame into following format:
{
column->{
rowName->data
}
}
No need to actually convert, since dataframe gets iterated
in the above format itself.
Note: column is converted to string.
"
| output |
output := Dictionary new.
aDataFrame asArrayOfColumns withIndexDo: [ :col :index |
output add: (aDataFrame columnNames at: index) asString -> col
].
^ output
]
{ #category : #converting }
DataFrameJsonWriter class >> convertToRecords: aDataFrame [
"Converts aDataFrame into following format:
[
{column->data},
]
Note: It ignores rowNames
No need to actually convert, since dataframe gets iterated
in the above format itself.
"
^ aDataFrame
]
{ #category : #converting }
DataFrameJsonWriter class >> convertToRowNames: aDataFrame [
"Converts aDataFrame into following format:
{
rowName->{
column->data
}
}
No need to actually convert, since dataframe gets iterated
in the above format itself.
Note: rowName is converted to string.
"
| output |
output := Dictionary new.
aDataFrame asArrayOfRows withIndexDo: [ :row :index |
output add: (aDataFrame rowNames at: index) asString -> row
].
^ output
]
{ #category : #converting }
DataFrameJsonWriter class >> convertToSplit: aDataFrame [
"Converts aDataFrame into following format:
{
index->[rowNames],
columns->[columnNames],
data->[data]
}
Note: It ignores rowNames
No need to actually convert, since dataframe gets iterated
in the above format itself.
"
| output rows |
output := Dictionary new.
output add: 'index'->(aDataFrame rowNames).
output add: 'columns'->(aDataFrame columnNames).
rows := OrderedCollection new: aDataFrame size.
aDataFrame do: [ :row | rows add: row asArray ].
output add: 'data'->rows.
^ output
]
{ #category : #converting }
DataFrameJsonWriter class >> convertToValues: aDataFrame [
"Converts aDataFrame into an array of arrays
Note: It ignores rowNames and columnNames
No need to actually convert, since dataframe gets iterated
in the above format itself.
"
| rows |
rows := OrderedCollection new: aDataFrame size.
aDataFrame do: [ :row | rows add: row asArray ].
^ rows
]
{ #category : #accessing }
DataFrameJsonWriter >> defaultOrient [
^ 'records'
]
{ #category : #accessing }
DataFrameJsonWriter >> defaultPretty [
^ false
]
{ #category : #initialization }
DataFrameJsonWriter >> initialize [
super initialize.
orient := self defaultOrient.
pretty := self defaultPretty.
writeMap := Dictionary newFrom: {
'records'->[ :aDataFrame :writeStream | self class convertToRecords: aDataFrame].
'split'->[ :aDataFrame :writeStream | self class convertToSplit: aDataFrame].
'columns'->[ :aDataFrame :writeStream | self class convertToColumns: aDataFrame].
'rowNames'->[ :aDataFrame :writeStream | self class convertToRowNames: aDataFrame].
'values'->[ :aDataFrame :writeStream | self class convertToValues: aDataFrame].
}
]
{ #category : #accessing }
DataFrameJsonWriter >> orient [
^ orient
]
{ #category : #accessing }
DataFrameJsonWriter >> orient: aString [
orient := aString
]
{ #category : #accessing }
DataFrameJsonWriter >> pretty [
^ pretty
]
{ #category : #accessing }
DataFrameJsonWriter >> pretty: aBoolean [
pretty := aBoolean
]
{ #category : #writing }
DataFrameJsonWriter >> write: aDataFrame to: aFileReference [
| writeStream writer jsonObj |
writeStream := aFileReference writeStream.
writer := NeoJSONWriter new on: writeStream.
writer prettyPrint: pretty.
jsonObj := (writeMap at: orient) value: aDataFrame value: writeStream.
writer nextPut: jsonObj.
writer close
]
{ #category : #writing }
DataFrameJsonWriter >> writeAsString: aDataFrame [
"Returns JSON representation of aDataFrame according to orient"
| writeStream writer jsonObj |
writeStream := WriteStream on: (String new: 100).
writer := NeoJSONWriter new on: writeStream.
writer prettyPrint: pretty.
jsonObj := (writeMap at: orient) value: aDataFrame value: writeStream.
writer nextPut: jsonObj.
writer close.
^ writeStream contents
]