/
bslJson.js
243 lines (205 loc) · 6.75 KB
/
bslJson.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
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
##extern-type RPC.Json.json
/** This part concerns translation of a OPA Json object to/from a native javascript object */
##extern-type RPC.Json.private.native
##extern-type ll_json_list_repr
##extern-type ll_json_record_repr
function is_float(n){
return /[e\.]/.test(n);
}
##module Json \ bsl_json
/** Get a "special json string" and return corresponding javascript
* object. This string is a json cut to be not too deep. For more
* information on this "special json special" see json.opa.
*
* @param {!string} str A string that may be parsed into a {e cut} JSON object.
* @return {Object} JS object represented by the given string
* @raise SyntaxError if given string doesn't respect "special" JSON
* format
*/
##register string_to_native : string -> 'a
##args(str)
{
/*This auxiliar function route the tree of given object. And get
all object with at least one field equals null. It's used by
string_to_native.
@param obj Object to route
@return An array of object with field(s) equals null
*/
var get_all_pre_null = function(obj){
var tab = new Array();
var aux = function(obj){
if (typeof obj != 'object') return;
for (var field in obj){
if (obj[field] == null){
tab.push(obj);
break;
}
}
for (var field in obj){
if (obj[field] != null){
aux(obj[field])
}
}
}
aux(obj);
return tab;
}
/* Main string_to_native */
var tab = new Array();
if (str.charAt(0) == '$'){
str = str.substr(1);
while (str.length > 0){
var start_json = str.search(/[^0-9]/);
// Numbers are parsed as decimal strings.
var json_length = parseInt(str.substr(0, start_json), 10);
var json = str.substr(start_json, json_length);
tab.push(JSON.parse(json));
str = str.substr(start_json + json_length);
}
} else {
tab.push(JSON.parse(str));
}
var iroot = 0;
for (var i = 1; i < tab.length;i=i){
var pre_null = get_all_pre_null(tab[iroot]);
for (var j = 0; j < pre_null.length; j++){
for (var field in pre_null[j]){
if(pre_null[j][field] == null){
pre_null[j][field] = tab[i];
i++;
}
}
}
iroot++;
}
return tab[0];
}
var json_void_result = make_onefield_record(static_field_of_name("Record"), make_simple_record(static_field_of_name("nil")));
/** This function create a Json object from a native javascript object.
*
* Note : order of record and/or list is preserved
*
* @param {!Object} obj A native JSON object (i.e. an object without methods, recursively)
* @return {!Object} A json value, if possible, or none
*/
##register native_to_json:'a -> option(RPC.Json.json)
##args(obj)
{
function aux(obj){
switch (typeof obj){
case "number":
return is_float(obj) ? make_onefield_record("Float", obj) : make_onefield_record("Int", obj);
case "string":
return make_onefield_record("String", obj);
case "boolean":
return make_onefield_record("Bool", obj);
case "object":
if (obj instanceof Array) {
var i;
var len = obj.length;
var tab = new Array(len);
for(i = 0; i < len; ++i)
tab[i] = aux(obj[i])
return make_onefield_record("List", js2list(tab));
} else {
var i;
var tab = [];
var empty = true;
for(i in obj)
{
//Construct the pair
var pair_cons = empty_constructor();
pair_cons = add_field(pair_cons, "f1", i);
pair_cons = add_field(pair_cons, "f2", aux(obj[i]));
var pair = make_record(pair_cons);
tab.push(pair);
empty = false;
}
if (empty) return json_void_result;
return make_onefield_record("Record", js2list(tab));
}
default:
throw ({message:"This is not a valid object to transform to json", obj:obj });
}
}
try {
return js_some(aux(obj));
} catch(e) {
jlog(e);
return js_none;
}
}
/** This function create a Json object from a string.
* @param str
* @return A json value
*/
##register of_string : string -> option(RPC.Json.json)
##args(str)
{
/* Construct a json value, from a native js object */
try {
var obj = %%Bsljson.Json.string_to_native%%(str);
if (obj == null) return js_none;
return %%Bsljson.Json.native_to_json%%(obj);
} catch(e) {
return js_none;
}
}
##register of_json_repr :RPC.Json.private.native -> option(RPC.Json.json)
##args(obj)
{
return %%BslJson.Json.native_to_json%%(obj);
}
##register json_list_empty : -> ll_json_list_repr
##args()
{ return new Array(); }
##register json_list_cons : RPC.Json.private.native, ll_json_list_repr -> ll_json_list_repr
##args(a,l)
{
l.unshift(a);
return l;
}
##register json_record_empty : -> ll_json_record_repr
##args()
{ return new Object(); }
##register json_record_cons : string, RPC.Json.private.native, ll_json_record_repr -> ll_json_record_repr
##args(a,b,r)
{
r[a] = b;
return r;
}
##register json_repr_int : int -> RPC.Json.private.native
##args(x)
{ return x; }
##register json_repr_float : float -> RPC.Json.private.native
##args(x)
{ return x; }
##register json_repr_string : string -> RPC.Json.private.native
##args(x)
{ return x; }
##register json_repr_bool : bool -> RPC.Json.private.native
##args(x)
{ return x; }
##register json_repr_void : -> RPC.Json.private.native
##args()
{ return {}; }
##register json_repr_array : ll_json_list_repr -> RPC.Json.private.native
##args(x)
{ return x; }
##register json_repr_record : ll_json_record_repr -> RPC.Json.private.native
##args(x)
{ return x; }
##endmodule