/
xqjson.xql
330 lines (306 loc) · 10.8 KB
/
xqjson.xql
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
xquery version "3.0";
(:
: Copyright (c) 2010-2011
: John Snelson. All rights reserved.
:
: Licensed under the Apache License, Version 2.0 (the "License");
: you may not use this file except in compliance with the License.
: You may obtain a copy of the License at
:
: http://www.apache.org/licenses/LICENSE-2.0
:
: Unless required by applicable law or agreed to in writing, software
: distributed under the License is distributed on an "AS IS" BASIS,
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
: See the License for the specific language governing permissions and
: limitations under the License.
:)
(:
20121227 Adam Retter changed namespace prefix from xqilla to xqjson
20121227 Adam Retter patched parseArray to support empty arrays
20121227 Adam Retter patched parseObject to support empty objects
20130210 Joe Wicentowski changed namespace to include 'xqjson'
20130612 Joe Wicentowski patched tokenize to handle long string arrays
20130613 Leif-Jöran Olsson patched decodeHexChar to correctly handle a-f chars.
20130614 Refactoring suggestion for performance of same function by Michael Westbay.
20140507 @Albicocca helped fix bug where commas were output when serializing JSON.
:)
module namespace xqjson="http://xqilla.sourceforge.net/lib/xqjson";
(:----------------------------------------------------------------------------------------------------:)
(: JSON parsing :)
declare function xqjson:parse-json($json as xs:string)
as element()?
{
let $res := xqjson:parseValue(xqjson:tokenize($json))
return
if(exists(remove($res,1))) then xqjson:parseError($res[2])
else element json {
$res[1]/@*,
$res[1]/node()
}
};
declare %private function xqjson:parseValue($tokens as element(token)*)
{
let $token := $tokens[1]
let $tokens := remove($tokens,1)
return
if($token/@t = "lbrace") then (
let $res := xqjson:parseObject($tokens)
let $tokens := remove($res,1)
return (
element res {
attribute type { "object" },
$res[1]/node()
},
$tokens
)
) else if ($token/@t = "lsquare") then (
let $res := xqjson:parseArray($tokens)
let $tokens := remove($res,1)
return (
element res {
attribute type { "array" },
$res[1]/node()
},
$tokens
)
) else if ($token/@t = "number") then (
element res {
attribute type { "number" },
text { $token }
},
$tokens
) else if ($token/@t = "string") then (
element res {
attribute type { "string" },
text { xqjson:unescape-json-string($token) }
},
$tokens
) else if ($token/@t = "true" or $token/@t = "false") then (
element res {
attribute type { "boolean" },
text { $token }
},
$tokens
) else if ($token/@t = "null") then (
element res {
attribute type { "null" }
},
$tokens
) else xqjson:parseError($token)
};
declare %private function xqjson:parseObject($tokens as element(token)*)
{
let $token1 := $tokens[1]
let $tokens := remove($tokens,1)
return
if($token1/@t eq "rbrace")then (
element res {
},
$tokens
) else if(not($token1/@t = "string")) then xqjson:parseError($token1) else
let $token2 := $tokens[1]
let $tokens := remove($tokens,1)
return
if(not($token2/@t = "colon")) then xqjson:parseError($token2) else
let $res := xqjson:parseValue($tokens)
let $tokens := remove($res,1)
let $pair := element pair {
attribute name { $token1 },
$res[1]/@*,
$res[1]/node()
}
let $token := $tokens[1]
let $tokens := remove($tokens,1)
return
if($token/@t = "comma") then (
let $res := xqjson:parseObject($tokens)
let $tokens := remove($res,1)
return (
element res {
$pair,
$res[1]/node()
},
$tokens
)
) else if($token/@t = "rbrace") then (
element res {
$pair
},
$tokens
) else xqjson:parseError($token)
};
declare %private function xqjson:parseArray($tokens as element(token)*)
{
if($tokens[1]/@t = "rsquare") then (
(: empty array! :)
element res {},
remove($tokens, 1)
) else
let $res := xqjson:parseValue($tokens)
let $tokens := remove($res,1)
let $item := element item {
$res[1]/@*,
$res[1]/node()
}
let $token := $tokens[1]
let $tokens := remove($tokens,1)
return
if($token/@t = "comma") then (
let $res := xqjson:parseArray($tokens)
let $tokens := remove($res,1)
return (
element res {
$item,
$res[1]/node()
},
$tokens
)
) else if($token/@t = "rsquare") then (
element res {
$item
},
$tokens
) else xqjson:parseError($token)
};
declare %private function xqjson:parseError($token as element(token))
as empty-sequence()
{
error(xs:QName("xqjson:PARSEJSON01"),
concat("Unexpected token: ", string($token/@t), " (""", string($token), """)"))
};
declare %private function xqjson:tokenize($json as xs:string)
as element(token)*
{
let $tokens := ("\{", "\}", "\[", "\]", ":", ",", "true", "false", "null", "\s+",
'"([^"\\]|\\"|\\\\|\\/|\\b|\\f|\\n|\\r|\\t|\\u[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])*"',
"-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+\-]?[0-9]+)?")
let $regex := string-join(for $t in $tokens return concat("(",$t,")"),"|")
for $match in analyze-string($json, $regex)/*
return
if($match/self::*:non-match) then xqjson:token("error", string($match))
else if($match/*:group/@nr = 1) then xqjson:token("lbrace", string($match))
else if($match/*:group/@nr = 2) then xqjson:token("rbrace", string($match))
else if($match/*:group/@nr = 3) then xqjson:token("lsquare", string($match))
else if($match/*:group/@nr = 4) then xqjson:token("rsquare", string($match))
else if($match/*:group/@nr = 5) then xqjson:token("colon", string($match))
else if($match/*:group/@nr = 6) then xqjson:token("comma", string($match))
else if($match/*:group/@nr = 7) then xqjson:token("true", string($match))
else if($match/*:group/@nr = 8) then xqjson:token("false", string($match))
else if($match/*:group/@nr = 9) then xqjson:token("null", string($match))
else if($match/*:group/@nr = 10) then () (:ignore whitespace:)
else if($match/*:group/@nr = 11) then
let $v := string($match)
let $len := string-length($v)
return xqjson:token("string", substring($v, 2, $len - 2))
(:
In the original version the string matching *inner* group was masked via '?>' as an atomic group (thus non-capturing).
This is not supported by Saxon so I removed it and incremented the group count by one.
group/@nr=12 is now the inner group of group/@nr=11 and will never appear as a direct child of <match>.
:)
else if($match/*:group/@nr = 13) then xqjson:token("number", string($match))
else xqjson:token("error", string($match))
};
declare %private function xqjson:token($t, $value)
{
<token t="{$t}">{ string($value) }</token>
};
(:----------------------------------------------------------------------------------------------------:)
(: JSON serializing :)
declare function xqjson:serialize-json($json-xml as element()?)
as xs:string?
{
if(empty($json-xml)) then () else
string-join(
xqjson:serializeJSONElement($json-xml)
,"")
};
declare %private function xqjson:serializeJSONElement($e as element())
as xs:string*
{
if($e/@type = "object") then xqjson:serializeJSONObject($e)
else if($e/@type = "array") then xqjson:serializeJSONArray($e)
else if($e/@type = "null") then "null"
else if($e/@type = "boolean") then string($e)
else if($e/@type = "number") then string($e)
else ('"', xqjson:escape-json-string($e), '"')
};
declare %private function xqjson:serializeJSONObject($e as element())
as xs:string*
{
"{",
for $el at $pos in $e/* return
(
if($pos = 1) then () else ",",
'"', xqjson:escape-json-string($el/@name), '":',
xqjson:serializeJSONElement($el)
),
"}"
};
declare %private function xqjson:serializeJSONArray($e as element())
as xs:string*
{
"[",
for $el at $pos in $e/* return
(
if($pos = 1) then () else ",",
xqjson:serializeJSONElement($el)
),
"]"
};
(:----------------------------------------------------------------------------------------------------:)
(: JSON unescaping :)
declare function xqjson:unescape-json-string($val as xs:string)
as xs:string
{
string-join(
let $regex := '[^\\]+|(\\")|(\\\\)|(\\/)|(\\b)|(\\f)|(\\n)|(\\r)|(\\t)|(\\u[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9])'
for $match in analyze-string($val, $regex)/*
return
if($match/*:group/@nr = 1) then """"
else if($match/*:group/@nr = 2) then "\"
else if($match/*:group/@nr = 3) then "/"
(: else if($match/*:group/@nr = 4) then "" :)
(: else if($match/*:group/@nr = 5) then "" :)
else if($match/*:group/@nr = 6) then "
"
else if($match/*:group/@nr = 7) then "
"
else if($match/*:group/@nr = 8) then "	"
else if($match/*:group/@nr = 9) then
codepoints-to-string(xqjson:decode-hex-string(substring($match, 3)))
else string($match)
,"")
};
declare function xqjson:escape-json-string($val as xs:string)
as xs:string
{
string-join(
let $regex := '(")|(\\)|(/)|(
)|(
)|(	)|[^"\\/

	]+'
for $match in analyze-string($val, $regex)/*
return
if($match/*:group/@nr = 1) then "\"""
else if($match/*:group/@nr = 2) then "\\"
else if($match/*:group/@nr = 3) then "\/"
else if($match/*:group/@nr = 4) then "\n"
else if($match/*:group/@nr = 5) then "\r"
else if($match/*:group/@nr = 6) then "\t"
else string($match)
,"")
};
declare function xqjson:decode-hex-string($val as xs:string)
as xs:integer
{
xqjson:decodeHexStringHelper(string-to-codepoints($val), 0)
};
declare %private function xqjson:decodeHexChar($val as xs:integer)
as xs:integer
{
if ($val le 57) then $val - 48 (: Handle '0' to '9' by subtracting '0' :)
else if ($val le 70) then $val - 55 (: Handle 'A' to 'F' by subtracting 'A' and adding 10 (-65 + 10) :)
else $val - 87 (: Handle 'a' to 'f' by subtracting 'a' and adding 10 (-97 + 10) :)
};
declare %private function xqjson:decodeHexStringHelper($chars as xs:integer*, $acc as xs:integer)
as xs:integer
{
if(empty($chars)) then $acc
else xqjson:decodeHexStringHelper(remove($chars,1), ($acc * 16) + xqjson:decodeHexChar($chars[1]))
};