-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJson.js
444 lines (368 loc) · 9.44 KB
/
Json.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
import Array exposing (initialize)
import Elm.Kernel.List exposing (Cons, Nil, fromArray)
import Elm.Kernel.Utils exposing (Tuple2)
import Json.Decode as Json exposing (Field, Index, OneOf, Failure, errorToString)
import List exposing (reverse)
import Maybe exposing (Just, Nothing)
import Result exposing (Ok, Err, isOk)
*/
/**__DEBUG/
function _Json_errorToString(error)
{
return __Json_errorToString(error);
}
//*/
// CORE DECODERS
function _Json_succeed(msg)
{
return {
$: __1_SUCCEED,
__msg: msg
};
}
function _Json_fail(msg)
{
return {
$: __1_FAIL,
__msg: msg
};
}
function _Json_decodePrim(decoder)
{
return { $: __1_PRIM, __decoder: decoder };
}
var _Json_decodeInt = _Json_decodePrim(function(value) {
return (typeof value !== 'number')
? _Json_expecting('an INT', value)
:
(-2147483647 < value && value < 2147483647 && (value | 0) === value)
? __Result_Ok(value)
:
(isFinite(value) && !(value % 1))
? __Result_Ok(value)
: _Json_expecting('an INT', value);
});
var _Json_decodeBool = _Json_decodePrim(function(value) {
return (typeof value === 'boolean')
? __Result_Ok(value)
: _Json_expecting('a BOOL', value);
});
var _Json_decodeFloat = _Json_decodePrim(function(value) {
return (typeof value === 'number')
? __Result_Ok(value)
: _Json_expecting('a FLOAT', value);
});
var _Json_decodeValue = _Json_decodePrim(function(value) {
return __Result_Ok(_Json_wrap(value));
});
var _Json_decodeString = _Json_decodePrim(function(value) {
return (typeof value === 'string')
? __Result_Ok(value)
: (value instanceof String)
? __Result_Ok(value + '')
: _Json_expecting('a STRING', value);
});
function _Json_decodeList(decoder) { return { $: __1_LIST, __decoder: decoder }; }
function _Json_decodeArray(decoder) { return { $: __1_ARRAY, __decoder: decoder }; }
function _Json_decodeNull(value) { return { $: __1_NULL, __value: value }; }
var _Json_decodeField = F2(function(field, decoder)
{
return {
$: __1_FIELD,
__field: field,
__decoder: decoder
};
});
var _Json_decodeIndex = F2(function(index, decoder)
{
return {
$: __1_INDEX,
__index: index,
__decoder: decoder
};
});
function _Json_decodeKeyValuePairs(decoder)
{
return {
$: __1_KEY_VALUE,
__decoder: decoder
};
}
function _Json_mapMany(f, decoders)
{
return {
$: __1_MAP,
__func: f,
__decoders: decoders
};
}
var _Json_andThen = F2(function(callback, decoder)
{
return {
$: __1_AND_THEN,
__decoder: decoder,
__callback: callback
};
});
function _Json_oneOf(decoders)
{
return {
$: __1_ONE_OF,
__decoders: decoders
};
}
// DECODING OBJECTS
var _Json_map1 = F2(function(f, d1)
{
return _Json_mapMany(f, [d1]);
});
var _Json_map2 = F3(function(f, d1, d2)
{
return _Json_mapMany(f, [d1, d2]);
});
var _Json_map3 = F4(function(f, d1, d2, d3)
{
return _Json_mapMany(f, [d1, d2, d3]);
});
var _Json_map4 = F5(function(f, d1, d2, d3, d4)
{
return _Json_mapMany(f, [d1, d2, d3, d4]);
});
var _Json_map5 = F6(function(f, d1, d2, d3, d4, d5)
{
return _Json_mapMany(f, [d1, d2, d3, d4, d5]);
});
var _Json_map6 = F7(function(f, d1, d2, d3, d4, d5, d6)
{
return _Json_mapMany(f, [d1, d2, d3, d4, d5, d6]);
});
var _Json_map7 = F8(function(f, d1, d2, d3, d4, d5, d6, d7)
{
return _Json_mapMany(f, [d1, d2, d3, d4, d5, d6, d7]);
});
var _Json_map8 = F9(function(f, d1, d2, d3, d4, d5, d6, d7, d8)
{
return _Json_mapMany(f, [d1, d2, d3, d4, d5, d6, d7, d8]);
});
// DECODE
var _Json_runOnString = F2(function(decoder, string)
{
try
{
var value = JSON.parse(string);
return _Json_runHelp(decoder, value);
}
catch (e)
{
return __Result_Err(A2(__Json_Failure, 'This is not valid JSON! ' + e.message, _Json_wrap(string)));
}
});
var _Json_run = F2(function(decoder, value)
{
return _Json_runHelp(decoder, _Json_unwrap(value));
});
function _Json_runHelp(decoder, value)
{
switch (decoder.$)
{
case __1_PRIM:
return decoder.__decoder(value);
case __1_NULL:
return (value === null)
? __Result_Ok(decoder.__value)
: _Json_expecting('null', value);
case __1_LIST:
if (!_Json_isArray(value))
{
return _Json_expecting('a LIST', value);
}
return _Json_runArrayDecoder(decoder.__decoder, value, __List_fromArray);
case __1_ARRAY:
if (!_Json_isArray(value))
{
return _Json_expecting('an ARRAY', value);
}
return _Json_runArrayDecoder(decoder.__decoder, value, _Json_toElmArray);
case __1_FIELD:
var field = decoder.__field;
if (typeof value !== 'object' || value === null || !(field in value))
{
return _Json_expecting('an OBJECT with a field named `' + field + '`', value);
}
var result = _Json_runHelp(decoder.__decoder, value[field]);
return (__Result_isOk(result)) ? result : __Result_Err(A2(__Json_Field, field, result.a));
case __1_INDEX:
var index = decoder.__index;
if (!_Json_isArray(value))
{
return _Json_expecting('an ARRAY', value);
}
if (index >= value.length)
{
return _Json_expecting('a LONGER array. Need index ' + index + ' but only see ' + value.length + ' entries', value);
}
var result = _Json_runHelp(decoder.__decoder, value[index]);
return (__Result_isOk(result)) ? result : __Result_Err(A2(__Json_Index, index, result.a));
case __1_KEY_VALUE:
if (typeof value !== 'object' || value === null || _Json_isArray(value))
{
return _Json_expecting('an OBJECT', value);
}
var keyValuePairs = __List_Nil;
// TODO test perf of Object.keys and switch when support is good enough
for (var key in value)
{
if (value.hasOwnProperty(key))
{
var result = _Json_runHelp(decoder.__decoder, value[key]);
if (!__Result_isOk(result))
{
return __Result_Err(A2(__Json_Field, key, result.a));
}
keyValuePairs = __List_Cons(__Utils_Tuple2(key, result.a), keyValuePairs);
}
}
return __Result_Ok(__List_reverse(keyValuePairs));
case __1_MAP:
var answer = decoder.__func;
var decoders = decoder.__decoders;
for (var i = 0; i < decoders.length; i++)
{
var result = _Json_runHelp(decoders[i], value);
if (!__Result_isOk(result))
{
return result;
}
answer = answer(result.a);
}
return __Result_Ok(answer);
case __1_AND_THEN:
var result = _Json_runHelp(decoder.__decoder, value);
return (!__Result_isOk(result))
? result
: _Json_runHelp(decoder.__callback(result.a), value);
case __1_ONE_OF:
var errors = __List_Nil;
for (var temp = decoder.__decoders; temp.b; temp = temp.b) // WHILE_CONS
{
var result = _Json_runHelp(temp.a, value);
if (__Result_isOk(result))
{
return result;
}
errors = __List_Cons(result.a, errors);
}
return __Result_Err(__Json_OneOf(__List_reverse(errors)));
case __1_FAIL:
return __Result_Err(A2(__Json_Failure, decoder.__msg, _Json_wrap(value)));
case __1_SUCCEED:
return __Result_Ok(decoder.__msg);
}
}
function _Json_runArrayDecoder(decoder, value, toElmValue)
{
var len = value.length;
var array = new Array(len);
for (var i = 0; i < len; i++)
{
var result = _Json_runHelp(decoder, value[i]);
if (!__Result_isOk(result))
{
return __Result_Err(A2(__Json_Index, i, result.a));
}
array[i] = result.a;
}
return __Result_Ok(toElmValue(array));
}
function _Json_isArray(value)
{
return Array.isArray(value)
|| (typeof FileList !== 'undefined' && value instanceof FileList)
|| (typeof TouchList !== 'undefined' && value instanceof TouchList);
}
function _Json_toElmArray(array)
{
return A2(__Array_initialize, array.length, function(i) { return array[i]; });
}
function _Json_expecting(type, value)
{
return __Result_Err(A2(__Json_Failure, 'Expecting ' + type, _Json_wrap(value)));
}
// EQUALITY
function _Json_equality(x, y)
{
if (x === y)
{
return true;
}
if (x.$ !== y.$)
{
return false;
}
switch (x.$)
{
case __1_SUCCEED:
case __1_FAIL:
return x.__msg === y.__msg;
case __1_PRIM:
return x.__decoder === y.__decoder;
case __1_NULL:
return x.__value === y.__value;
case __1_LIST:
case __1_ARRAY:
case __1_KEY_VALUE:
return _Json_equality(x.__decoder, y.__decoder);
case __1_FIELD:
return x.__field === y.__field && _Json_equality(x.__decoder, y.__decoder);
case __1_INDEX:
return x.__index === y.__index && _Json_equality(x.__decoder, y.__decoder);
case __1_MAP:
return x.__func === y.__func && _Json_listEquality(x.__decoders, y.__decoders);
case __1_AND_THEN:
return x.__callback === y.__callback && _Json_equality(x.__decoder, y.__decoder);
case __1_ONE_OF:
return _Json_listEquality(x.__decoders, y.__decoders);
}
}
function _Json_listEquality(aDecoders, bDecoders)
{
var len = aDecoders.length;
if (len !== bDecoders.length)
{
return false;
}
for (var i = 0; i < len; i++)
{
if (!_Json_equality(aDecoders[i], bDecoders[i]))
{
return false;
}
}
return true;
}
// ENCODE
var _Json_encode = F2(function(indentLevel, value)
{
return JSON.stringify(_Json_unwrap(value), null, indentLevel) + '';
});
function _Json_wrap__DEBUG(value) { return { $: __0_JSON, a: value }; }
function _Json_unwrap__DEBUG(value) { return value.a; }
function _Json_wrap__PROD(value) { return value; }
function _Json_unwrap__PROD(value) { return value; }
function _Json_emptyArray() { return []; }
function _Json_emptyObject() { return {}; }
var _Json_addField = F3(function(key, value, object)
{
object[key] = _Json_unwrap(value);
return object;
});
function _Json_addEntry(func)
{
return F2(function(entry, array)
{
array.push(_Json_unwrap(func(entry)));
return array;
});
}
var _Json_encodeNull = _Json_wrap(null);