-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserialize.js
More file actions
202 lines (161 loc) · 4.71 KB
/
serialize.js
File metadata and controls
202 lines (161 loc) · 4.71 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const immutable = require('immutable')
const JSONStreamStringify = require('json-stream-stringify')
const nativeTypeHelpers = require('./helpers/native-type-helpers')
function serialize(data, options = {}) {
if (immutable.Iterable.isIterable(data) ||
data instanceof immutable.Record ||
nativeTypeHelpers.isSupportedNativeType(data)
) {
const patchedData = Object.create(data)
if (nativeTypeHelpers.isSupportedNativeType(data)) {
// NOTE: When native type (such as Date or RegExp) methods are called
// on an `Object.create()`'d objects, invalid usage errors are thrown
// in many cases. We need to patch the used methods to work
// on originals.
nativeTypeHelpers.patchNativeTypeMethods(patchedData, data)
}
// NOTE: JSON.stringify() calls the #toJSON() method of the root object.
// Immutable.JS provides its own #toJSON() implementation which does not
// preserve map key types.
patchedData.toJSON = function () {
return this
}
data = patchedData
}
const indentation = options.pretty ? 2 : 0
return JSON.stringify(data, replace, indentation)
}
function createSerializationStream(data, options = {}) {
const indentation = options.pretty ? 2 : 0
const replacer = options.bigChunks ? replace : replaceAsync
const stream = JSONStreamStringify(data, replacer, indentation)
return stream
}
function replace(key, value) {
let result = value
if (value instanceof immutable.Record) {
result = replaceRecord(value, replace)
}
else if (immutable.Iterable.isIterable(value)) {
result = replaceIterable(value, replace)
}
else if (Array.isArray(value)) {
result = replaceArray(value, replace)
}
else if (nativeTypeHelpers.isDate(value)) {
result = { '__date': value.toISOString() }
}
else if (nativeTypeHelpers.isRegExp(value)) {
result = { '__regexp': value.toString() }
}
else if (typeof value === 'object' && value !== null) {
result = replacePlainObject(value, replace)
}
return result
}
function replaceAsync(key, value) {
let result = value
if (!(value instanceof Promise)) {
if (value instanceof immutable.Record) {
result = new Promise((resolve) => {
setImmediate(() => {
resolve(replaceRecord(value, replaceAsync))
})
})
}
else if (immutable.Iterable.isIterable(value)) {
result = new Promise((resolve) => {
setImmediate(() => {
resolve(replaceIterable(value, replaceAsync))
})
})
}
else if (Array.isArray(value)) {
result = new Promise((resolve) => {
setImmediate(() => {
resolve(replaceArray(value, replaceAsync))
})
})
}
else if (typeof value === 'object' && value !== null) {
result = new Promise((resolve) => {
setImmediate(() => {
resolve(replacePlainObject(value, replaceAsync))
})
})
}
}
return result
}
function replaceRecord(rec, replaceChild) {
const recordDataMap = rec.toSeq()
const recordData = {}
recordDataMap.forEach((value, key) => {
recordData[key] = replaceChild(key, value)
})
if (!rec._name) {
return recordData
}
return { "__record": rec._name, "data": recordData }
}
function getIterableType(iterable) {
if (immutable.List.isList(iterable)) {
return 'List'
}
if (immutable.Stack.isStack(iterable)) {
return 'Stack'
}
if (immutable.Set.isSet(iterable)) {
if (immutable.OrderedSet.isOrderedSet(iterable)) {
return 'OrderedSet'
}
return 'Set'
}
if (immutable.Map.isMap(iterable)) {
if (immutable.OrderedMap.isOrderedMap(iterable)) {
return 'OrderedMap'
}
return 'Map'
}
return undefined;
}
function replaceIterable(iter, replaceChild) {
const iterableType = getIterableType(iter)
if (!iterableType) {
throw new Error(`Cannot find type of iterable: ${iter}`)
}
switch (iterableType) {
case 'List':
case 'Set':
case 'OrderedSet':
case 'Stack':
const listData = []
iter.forEach((value, key) => {
listData.push(replaceChild(key, value))
})
return { "__iterable": iterableType, "data": listData }
case 'Map':
case 'OrderedMap':
const mapData = []
iter.forEach((value, key) => {
mapData.push([ key, replaceChild(key, value) ])
})
return { "__iterable": iterableType, "data": mapData }
}
}
function replaceArray(arr, replaceChild) {
return arr.map((value, index) => {
return replaceChild(index, value)
})
}
function replacePlainObject(obj, replaceChild) {
const objData = {}
Object.keys(obj).forEach((key) => {
objData[key] = replaceChild(key, obj[key])
})
return objData
}
module.exports = {
createSerializationStream,
serialize,
}