forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_json.js
170 lines (149 loc) · 3.46 KB
/
js_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
'use strict';
var Caml_option = require("./caml_option.js");
function classify(x) {
var ty = typeof x;
if (ty === "string") {
return {
TAG: "JSONString",
_0: x
};
} else if (ty === "number") {
return {
TAG: "JSONNumber",
_0: x
};
} else if (ty === "boolean") {
if (x === true) {
return "JSONTrue";
} else {
return "JSONFalse";
}
} else if (x === null) {
return "JSONNull";
} else if (Array.isArray(x)) {
return {
TAG: "JSONArray",
_0: x
};
} else {
return {
TAG: "JSONObject",
_0: x
};
}
}
function test(x, v) {
switch (v) {
case "String" :
return typeof x === "string";
case "Number" :
return typeof x === "number";
case "Object" :
if (x !== null && typeof x === "object") {
return !Array.isArray(x);
} else {
return false;
}
case "Array" :
return Array.isArray(x);
case "Boolean" :
return typeof x === "boolean";
case "Null" :
return x === null;
}
}
function decodeString(json) {
if (typeof json === "string") {
return json;
}
}
function decodeNumber(json) {
if (typeof json === "number") {
return json;
}
}
function decodeObject(json) {
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
return Caml_option.some(json);
}
}
function decodeArray(json) {
if (Array.isArray(json)) {
return json;
}
}
function decodeBoolean(json) {
if (typeof json === "boolean") {
return json;
}
}
function decodeNull(json) {
if (json === null) {
return null;
}
}
var patch = (function (json) {
var x = [json];
var q = [{ kind: 0, i: 0, parent: x }];
while (q.length !== 0) {
// begin pop the stack
var cur = q[q.length - 1];
if (cur.kind === 0) {
cur.val = cur.parent[cur.i]; // patch the undefined value for array
if (++cur.i === cur.parent.length) {
q.pop();
}
} else {
q.pop();
}
// finish
var task = cur.val;
if (typeof task === "object") {
if (Array.isArray(task) && task.length !== 0) {
q.push({ kind: 0, i: 0, parent: task, val: undefined });
} else {
for (var k in task) {
if (k === "RE_PRIVATE_NONE") {
if (cur.kind === 0) {
cur.parent[cur.i - 1] = undefined;
} else {
cur.parent[cur.i] = undefined;
}
continue;
}
q.push({ kind: 1, i: k, parent: task, val: task[k] });
}
}
}
}
return x[0];
});
function serializeExn(x) {
return (function(obj){
var output= JSON.stringify(obj,function(_,value){
if(value===undefined){
return {RE_PRIVATE_NONE : true}
}
return value
});
if(output === undefined){
// JSON.stringify will raise TypeError when it detects cylic objects
throw new TypeError("output is undefined")
}
return output
})(x);
}
function deserializeUnsafe(s) {
return patch(JSON.parse(s));
}
exports.classify = classify;
exports.test = test;
exports.decodeString = decodeString;
exports.decodeNumber = decodeNumber;
exports.decodeObject = decodeObject;
exports.decodeArray = decodeArray;
exports.decodeBoolean = decodeBoolean;
exports.decodeNull = decodeNull;
exports.deserializeUnsafe = deserializeUnsafe;
exports.serializeExn = serializeExn;
/* No side effect */