• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PolyMathOrg / DataFrame / 13409391746

19 Feb 2025 09:30AM UTC coverage: 94.756%. Remained the same
13409391746

push

github

web-flow
Enable Pharo 12 and 13 for the CI

13571 of 14322 relevant lines covered (94.76%)

4.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.91
/src/DataFrame-IO/DataFrameJsonWriter.class.st
1
Class {
2
        #name : #DataFrameJsonWriter,
3
        #superclass : #DataFrameWriter,
4
        #instVars : [
5
                'orient',
6
                'pretty',
7
                'writeMap'
8
        ],
9
        #category : #'DataFrame-IO-Core'
10
}
11

12
{ #category : #converting }
13
DataFrameJsonWriter class >> convertToColumns: aDataFrame [
×
14
        "Converts aDataFrame into following format:
×
15
                {
×
16
                 column->{
×
17
                                         rowName->data
×
18
                                        }
×
19
                }
×
20
                No need to actually convert, since dataframe gets iterated
×
21
                in the above format itself.
×
22
                Note: column is converted to string.
×
23
        "
×
24
        | output |
×
25

×
26
        output := Dictionary new.
×
27
        aDataFrame asArrayOfColumns withIndexDo: [ :col :index |
×
28
                output add: (aDataFrame columnNames at: index) asString -> col
×
29
                ].
×
30
        ^ output
×
31
]
×
32

33
{ #category : #converting }
34
DataFrameJsonWriter class >> convertToRecords: aDataFrame [
5✔
35
        "Converts aDataFrame into following format:
5✔
36
                [
5✔
37
                        {column->data},
5✔
38
                ]
5✔
39
                Note: It ignores rowNames
5✔
40
                No need to actually convert, since dataframe gets iterated
5✔
41
                in the above format itself.
5✔
42
        "
5✔
43

5✔
44
        ^ aDataFrame
5✔
45
]
5✔
46

47
{ #category : #converting }
48
DataFrameJsonWriter class >> convertToRowNames: aDataFrame [
×
49
        "Converts aDataFrame into following format:
×
50
                {
×
51
                 rowName->{
×
52
                                         column->data
×
53
                                        }
×
54
                }
×
55
                No need to actually convert, since dataframe gets iterated
×
56
                in the above format itself.
×
57
                Note: rowName is converted to string.
×
58
        "
×
59
        | output |
×
60

×
61
        output := Dictionary new.
×
62
        aDataFrame asArrayOfRows withIndexDo: [ :row :index |
×
63
                output add: (aDataFrame rowNames at: index) asString -> row
×
64
                ].
×
65
        ^ output
×
66
]
×
67

68
{ #category : #converting }
69
DataFrameJsonWriter class >> convertToSplit: aDataFrame [
×
70
        "Converts aDataFrame into following format:
×
71
                {
×
72
                        index->[rowNames],
×
73
                        columns->[columnNames],
×
74
                        data->[data]
×
75
                }
×
76
                Note: It ignores rowNames
×
77
                No need to actually convert, since dataframe gets iterated
×
78
                in the above format itself.
×
79
        "
×
80
        | output rows |
×
81

×
82
        output := Dictionary new.
×
83
        output add: 'index'->(aDataFrame rowNames).
×
84
        output add: 'columns'->(aDataFrame columnNames).
×
85

×
86
        rows := OrderedCollection new: aDataFrame size.
×
87
        aDataFrame do: [ :row | rows add: row asArray ].
×
88

×
89
        output add: 'data'->rows.
×
90
        ^ output
×
91
]
×
92

93
{ #category : #converting }
94
DataFrameJsonWriter class >> convertToValues: aDataFrame [
5✔
95
        "Converts aDataFrame into an array of arrays
5✔
96
                Note: It ignores rowNames and columnNames
5✔
97
                No need to actually convert, since dataframe gets iterated
5✔
98
                in the above format itself.
5✔
99
        "
5✔
100
        | rows |
5✔
101

5✔
102
        rows := OrderedCollection new: aDataFrame size.
5✔
103
        aDataFrame do: [ :row | rows add: row asArray ].
5✔
104
        ^ rows
5✔
105
]
5✔
106

107
{ #category : #accessing }
108
DataFrameJsonWriter >> defaultOrient [
5✔
109
        ^ 'records'
5✔
110
]
5✔
111

112
{ #category : #accessing }
113
DataFrameJsonWriter >> defaultPretty [
5✔
114
        ^ false
5✔
115
]
5✔
116

117
{ #category : #initialization }
118
DataFrameJsonWriter >> initialize [
5✔
119
        super initialize.
5✔
120
        orient := self defaultOrient.
5✔
121
        pretty := self defaultPretty.
5✔
122
        writeMap := Dictionary newFrom: {
5✔
123
                'records'->[ :aDataFrame :writeStream  | self class convertToRecords: aDataFrame].
5✔
124
                'split'->[ :aDataFrame :writeStream    | self class convertToSplit: aDataFrame].
5✔
125
                'columns'->[ :aDataFrame :writeStream  | self class convertToColumns: aDataFrame].
5✔
126
                'rowNames'->[ :aDataFrame :writeStream | self class convertToRowNames: aDataFrame].
5✔
127
                'values'->[ :aDataFrame :writeStream   | self class convertToValues: aDataFrame].
5✔
128
                }
5✔
129
]
5✔
130

131
{ #category : #accessing }
132
DataFrameJsonWriter >> orient [
×
133
        ^ orient
×
134
]
×
135

136
{ #category : #accessing }
137
DataFrameJsonWriter >> orient: aString [
5✔
138
        orient := aString
5✔
139
]
5✔
140

141
{ #category : #accessing }
142
DataFrameJsonWriter >> pretty [
×
143
        ^ pretty
×
144
]
×
145

146
{ #category : #accessing }
147
DataFrameJsonWriter >> pretty: aBoolean [
×
148
        pretty := aBoolean
×
149
]
×
150

151
{ #category : #writing }
152
DataFrameJsonWriter >> write: aDataFrame to: aFileReference [
5✔
153
        | writeStream writer jsonObj |
5✔
154

5✔
155
        writeStream := aFileReference writeStream.
5✔
156
        writer := NeoJSONWriter new on: writeStream.
5✔
157
        writer prettyPrint: pretty.
5✔
158
        jsonObj := (writeMap at: orient) value: aDataFrame value: writeStream.
5✔
159
        writer nextPut: jsonObj.
5✔
160
        writer close
5✔
161
]
5✔
162

163
{ #category : #writing }
164
DataFrameJsonWriter >> writeAsString: aDataFrame [
5✔
165
        "Returns JSON representation of aDataFrame according to orient"
5✔
166
        | writeStream writer jsonObj |
5✔
167

5✔
168
        writeStream := WriteStream on: (String new: 100).
5✔
169
        writer := NeoJSONWriter new on: writeStream.
5✔
170
        writer prettyPrint: pretty.
5✔
171
        jsonObj := (writeMap at: orient) value: aDataFrame value: writeStream.
5✔
172
        writer nextPut: jsonObj.
5✔
173
        writer close.
5✔
174
        ^ writeStream contents
5✔
175
]
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc