… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
|
|
24 | |
|
|
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
26 | |
|
|
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
29 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
32 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
34 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
35 | |
|
|
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
22 | |
38 | |
23 | #define SB do { |
39 | #define SB do { |
24 | #define SE } while (0) |
40 | #define SE } while (0) |
25 | |
41 | |
26 | static HV *json_stash; |
42 | static HV *json_stash; // JSON::XS:: |
|
|
43 | |
|
|
44 | ///////////////////////////////////////////////////////////////////////////// |
|
|
45 | // utility functions |
|
|
46 | |
|
|
47 | static UV * |
|
|
48 | SvJSON (SV *sv) |
|
|
49 | { |
|
|
50 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
51 | croak ("object is not of type JSON::XS"); |
|
|
52 | |
|
|
53 | return &SvUVX (SvRV (sv)); |
|
|
54 | } |
|
|
55 | |
|
|
56 | static void |
|
|
57 | shrink (SV *sv) |
|
|
58 | { |
|
|
59 | sv_utf8_downgrade (sv, 1); |
|
|
60 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
61 | { |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #elif defined (SvPV_renew) |
|
|
65 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
66 | #endif |
|
|
67 | } |
|
|
68 | } |
|
|
69 | |
|
|
70 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
71 | // case of an error. |
|
|
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
73 | // but use the very good perl function to parse anything else. |
|
|
74 | // note that we never call this function for a ascii codepoints |
|
|
75 | static UV |
|
|
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
77 | { |
|
|
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
81 | { |
|
|
82 | *clen = 2; |
|
|
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
84 | } |
|
|
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
|
|
88 | return (UV)-1; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
|
|
92 | ///////////////////////////////////////////////////////////////////////////// |
|
|
93 | // encoder |
27 | |
94 | |
28 | // structure used for encoding JSON |
95 | // structure used for encoding JSON |
29 | typedef struct |
96 | typedef struct |
30 | { |
97 | { |
31 | char *cur; |
98 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
34 | SV *sv; |
100 | SV *sv; // result scalar |
35 | UV flags; |
101 | U32 flags; // F_* |
36 | int max_recurse; |
102 | U32 indent; // indentation level |
37 | int indent; |
103 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
104 | } enc_t; |
39 | |
|
|
40 | // structure used for decoding JSON |
|
|
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | static void |
|
|
59 | shrink (SV *sv) |
|
|
60 | { |
|
|
61 | sv_utf8_downgrade (sv, 1); |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #endif |
|
|
65 | } |
|
|
66 | |
|
|
67 | ///////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
105 | |
69 | static void |
106 | static void |
70 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
71 | { |
108 | { |
72 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
… | |
… | |
130 | STRLEN clen; |
167 | STRLEN clen; |
131 | UV uch; |
168 | UV uch; |
132 | |
169 | |
133 | if (is_utf8) |
170 | if (is_utf8) |
134 | { |
171 | { |
135 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
136 | if (clen == (STRLEN)-1) |
174 | if (clen == (STRLEN)-1) |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
138 | } |
176 | } |
139 | else |
177 | else |
140 | { |
178 | { |
… | |
… | |
178 | } |
216 | } |
179 | while (--clen); |
217 | while (--clen); |
180 | } |
218 | } |
181 | else |
219 | else |
182 | { |
220 | { |
183 | need (enc, len += 10); // never more than 11 bytes needed |
221 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
184 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
222 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
185 | ++str; |
223 | ++str; |
186 | } |
224 | } |
187 | } |
225 | } |
188 | } |
226 | } |
… | |
… | |
190 | |
228 | |
191 | --len; |
229 | --len; |
192 | } |
230 | } |
193 | } |
231 | } |
194 | |
232 | |
195 | #define INDENT SB \ |
233 | static void |
|
|
234 | encode_indent (enc_t *enc) |
|
|
235 | { |
196 | if (enc->flags & F_INDENT) \ |
236 | if (enc->flags & F_INDENT) |
197 | { \ |
237 | { |
198 | int i_; \ |
238 | int spaces = enc->indent * INDENT_STEP; |
199 | need (enc, enc->indent); \ |
|
|
200 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
201 | encode_ch (enc, ' '); \ |
|
|
202 | } \ |
|
|
203 | SE |
|
|
204 | |
239 | |
205 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
240 | need (enc, spaces); |
206 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
241 | memset (enc->cur, ' ', spaces); |
207 | #define COMMA SB \ |
242 | enc->cur += spaces; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
|
|
246 | static void |
|
|
247 | encode_space (enc_t *enc) |
|
|
248 | { |
|
|
249 | need (enc, 1); |
208 | encode_ch (enc, ','); \ |
250 | encode_ch (enc, ' '); |
|
|
251 | } |
|
|
252 | |
|
|
253 | static void |
|
|
254 | encode_nl (enc_t *enc) |
|
|
255 | { |
209 | if (enc->flags & F_INDENT) \ |
256 | if (enc->flags & F_INDENT) |
210 | NL; \ |
257 | { |
|
|
258 | need (enc, 1); |
|
|
259 | encode_ch (enc, '\n'); |
|
|
260 | } |
|
|
261 | } |
|
|
262 | |
|
|
263 | static void |
|
|
264 | encode_comma (enc_t *enc) |
|
|
265 | { |
|
|
266 | encode_ch (enc, ','); |
|
|
267 | |
|
|
268 | if (enc->flags & F_INDENT) |
|
|
269 | encode_nl (enc); |
211 | else if (enc->flags & F_SPACE_AFTER) \ |
270 | else if (enc->flags & F_SPACE_AFTER) |
212 | SPACE; \ |
271 | encode_space (enc); |
213 | SE |
272 | } |
214 | |
273 | |
215 | static void encode_sv (enc_t *enc, SV *sv); |
274 | static void encode_sv (enc_t *enc, SV *sv); |
216 | |
275 | |
217 | static void |
276 | static void |
218 | encode_av (enc_t *enc, AV *av) |
277 | encode_av (enc_t *enc, AV *av) |
219 | { |
278 | { |
220 | int i, len = av_len (av); |
279 | int i, len = av_len (av); |
221 | |
280 | |
222 | encode_ch (enc, '['); NL; |
281 | if (enc->indent >= enc->maxdepth) |
|
|
282 | croak ("data structure too deep (hit recursion limit)"); |
|
|
283 | |
|
|
284 | encode_ch (enc, '['); encode_nl (enc); |
223 | ++enc->indent; |
285 | ++enc->indent; |
224 | |
286 | |
225 | for (i = 0; i <= len; ++i) |
287 | for (i = 0; i <= len; ++i) |
226 | { |
288 | { |
227 | INDENT; |
289 | encode_indent (enc); |
228 | encode_sv (enc, *av_fetch (av, i, 0)); |
290 | encode_sv (enc, *av_fetch (av, i, 0)); |
229 | |
291 | |
230 | if (i < len) |
292 | if (i < len) |
231 | COMMA; |
293 | encode_comma (enc); |
232 | } |
294 | } |
233 | |
295 | |
234 | NL; |
296 | encode_nl (enc); |
235 | |
297 | |
236 | --enc->indent; |
298 | --enc->indent; |
237 | INDENT; encode_ch (enc, ']'); |
299 | encode_indent (enc); encode_ch (enc, ']'); |
238 | } |
300 | } |
239 | |
301 | |
240 | static void |
302 | static void |
241 | encode_he (enc_t *enc, HE *he) |
303 | encode_he (enc_t *enc, HE *he) |
242 | { |
304 | { |
… | |
… | |
256 | else |
318 | else |
257 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
319 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
258 | |
320 | |
259 | encode_ch (enc, '"'); |
321 | encode_ch (enc, '"'); |
260 | |
322 | |
261 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
323 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
262 | encode_ch (enc, ':'); |
324 | encode_ch (enc, ':'); |
263 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
325 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
264 | encode_sv (enc, HeVAL (he)); |
326 | encode_sv (enc, HeVAL (he)); |
265 | } |
327 | } |
266 | |
328 | |
267 | // compare hash entries, used when all keys are bytestrings |
329 | // compare hash entries, used when all keys are bytestrings |
268 | static int |
330 | static int |
… | |
… | |
274 | HE *b = *(HE **)b_; |
336 | HE *b = *(HE **)b_; |
275 | |
337 | |
276 | STRLEN la = HeKLEN (a); |
338 | STRLEN la = HeKLEN (a); |
277 | STRLEN lb = HeKLEN (b); |
339 | STRLEN lb = HeKLEN (b); |
278 | |
340 | |
279 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
341 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
280 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
342 | cmp = la - lb; |
281 | |
343 | |
282 | return cmp; |
344 | return cmp; |
283 | } |
345 | } |
284 | |
346 | |
285 | // compare hash entries, used when some keys are sv's or utf-x |
347 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
292 | static void |
354 | static void |
293 | encode_hv (enc_t *enc, HV *hv) |
355 | encode_hv (enc_t *enc, HV *hv) |
294 | { |
356 | { |
295 | int count, i; |
357 | int count, i; |
296 | |
358 | |
|
|
359 | if (enc->indent >= enc->maxdepth) |
|
|
360 | croak ("data structure too deep (hit recursion limit)"); |
|
|
361 | |
297 | encode_ch (enc, '{'); NL; ++enc->indent; |
362 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
298 | |
363 | |
299 | if ((count = hv_iterinit (hv))) |
364 | if ((count = hv_iterinit (hv))) |
300 | { |
365 | { |
301 | // for canonical output we have to sort by keys first |
366 | // for canonical output we have to sort by keys first |
302 | // actually, this is mostly due to the stupid so-called |
367 | // actually, this is mostly due to the stupid so-called |
303 | // security workaround added somewhere in 5.8.x. |
368 | // security workaround added somewhere in 5.8.x. |
304 | // that randomises hash orderings |
369 | // that randomises hash orderings |
305 | if (enc->flags & F_CANONICAL) |
370 | if (enc->flags & F_CANONICAL) |
306 | { |
371 | { |
307 | HE *he, *hes [count]; |
372 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
308 | int fast = 1; |
373 | int fast = 1; |
309 | |
374 | |
310 | i = 0; |
375 | i = 0; |
311 | while ((he = hv_iternext (hv))) |
376 | while ((he = hv_iternext (hv))) |
312 | { |
377 | { |
… | |
… | |
337 | LEAVE; |
402 | LEAVE; |
338 | } |
403 | } |
339 | |
404 | |
340 | for (i = 0; i < count; ++i) |
405 | for (i = 0; i < count; ++i) |
341 | { |
406 | { |
342 | INDENT; |
407 | encode_indent (enc); |
343 | encode_he (enc, hes [i]); |
408 | encode_he (enc, hes [i]); |
344 | |
409 | |
345 | if (i < count - 1) |
410 | if (i < count - 1) |
346 | COMMA; |
411 | encode_comma (enc); |
347 | } |
412 | } |
348 | |
413 | |
349 | NL; |
414 | encode_nl (enc); |
350 | } |
415 | } |
351 | else |
416 | else |
352 | { |
417 | { |
353 | SV *sv; |
|
|
354 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
355 | |
419 | |
356 | for (;;) |
420 | for (;;) |
357 | { |
421 | { |
358 | INDENT; |
422 | encode_indent (enc); |
359 | encode_he (enc, he); |
423 | encode_he (enc, he); |
360 | |
424 | |
361 | if (!(he = hv_iternext (hv))) |
425 | if (!(he = hv_iternext (hv))) |
362 | break; |
426 | break; |
363 | |
427 | |
364 | COMMA; |
428 | encode_comma (enc); |
365 | } |
429 | } |
366 | |
430 | |
367 | NL; |
431 | encode_nl (enc); |
368 | } |
432 | } |
369 | } |
433 | } |
370 | |
434 | |
371 | --enc->indent; INDENT; encode_ch (enc, '}'); |
435 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
439 | static void |
|
|
440 | encode_rv (enc_t *enc, SV *sv) |
|
|
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
|
|
444 | SvGETMAGIC (sv); |
|
|
445 | svt = SvTYPE (sv); |
|
|
446 | |
|
|
447 | if (svt == SVt_PVHV) |
|
|
448 | encode_hv (enc, (HV *)sv); |
|
|
449 | else if (svt == SVt_PVAV) |
|
|
450 | encode_av (enc, (AV *)sv); |
|
|
451 | else if (svt < SVt_PVAV) |
|
|
452 | { |
|
|
453 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
454 | encode_str (enc, "false", 5, 0); |
|
|
455 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
456 | encode_str (enc, "true", 4, 0); |
|
|
457 | else |
|
|
458 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
459 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
460 | } |
|
|
461 | else |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
372 | } |
464 | } |
373 | |
465 | |
374 | static void |
466 | static void |
375 | encode_sv (enc_t *enc, SV *sv) |
467 | encode_sv (enc_t *enc, SV *sv) |
376 | { |
468 | { |
… | |
… | |
397 | SvIsUV(sv) |
489 | SvIsUV(sv) |
398 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
490 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
491 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | } |
492 | } |
401 | else if (SvROK (sv)) |
493 | else if (SvROK (sv)) |
402 | { |
494 | encode_rv (enc, SvRV (sv)); |
403 | SV *rv = SvRV (sv); |
|
|
404 | |
|
|
405 | if (!--enc->max_recurse) |
|
|
406 | croak ("data structure too deep (hit recursion limit)"); |
|
|
407 | |
|
|
408 | switch (SvTYPE (rv)) |
|
|
409 | { |
|
|
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
411 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
412 | |
|
|
413 | default: |
|
|
414 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
415 | SvPV_nolen (sv)); |
|
|
416 | } |
|
|
417 | } |
|
|
418 | else if (!SvOK (sv)) |
495 | else if (!SvOK (sv)) |
419 | encode_str (enc, "null", 4, 0); |
496 | encode_str (enc, "null", 4, 0); |
420 | else |
497 | else |
421 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
498 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
422 | SvPV_nolen (sv), SvFLAGS (sv)); |
499 | SvPV_nolen (sv), SvFLAGS (sv)); |
423 | } |
500 | } |
424 | |
501 | |
425 | static SV * |
502 | static SV * |
426 | encode_json (SV *scalar, UV flags) |
503 | encode_json (SV *scalar, U32 flags) |
427 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
430 | |
509 | |
431 | enc_t enc; |
|
|
432 | enc.flags = flags; |
510 | enc.flags = flags; |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
435 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
436 | enc.max_recurse = 0; |
|
|
437 | enc.indent = 0; |
514 | enc.indent = 0; |
|
|
515 | enc.maxdepth = DEC_DEPTH (flags); |
438 | |
516 | |
439 | SvPOK_only (enc.sv); |
517 | SvPOK_only (enc.sv); |
440 | encode_sv (&enc, scalar); |
518 | encode_sv (&enc, scalar); |
441 | |
519 | |
442 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
449 | |
527 | |
450 | return enc.sv; |
528 | return enc.sv; |
451 | } |
529 | } |
452 | |
530 | |
453 | ///////////////////////////////////////////////////////////////////////////// |
531 | ///////////////////////////////////////////////////////////////////////////// |
|
|
532 | // decoder |
454 | |
533 | |
455 | #define WS \ |
534 | // structure used for decoding JSON |
|
|
535 | typedef struct |
|
|
536 | { |
|
|
537 | char *cur; // current parser pointer |
|
|
538 | char *end; // end of input string |
|
|
539 | const char *err; // parse error, if != 0 |
|
|
540 | U32 flags; // F_* |
|
|
541 | U32 depth; // recursion depth |
|
|
542 | U32 maxdepth; // recursion depth limit |
|
|
543 | } dec_t; |
|
|
544 | |
|
|
545 | static void |
|
|
546 | decode_ws (dec_t *dec) |
|
|
547 | { |
456 | for (;;) \ |
548 | for (;;) |
457 | { \ |
549 | { |
458 | char ch = *dec->cur; \ |
550 | char ch = *dec->cur; |
|
|
551 | |
459 | if (ch > 0x20 \ |
552 | if (ch > 0x20 |
460 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
553 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
461 | break; \ |
554 | break; |
|
|
555 | |
462 | ++dec->cur; \ |
556 | ++dec->cur; |
463 | } |
557 | } |
|
|
558 | } |
464 | |
559 | |
465 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
561 | |
466 | #define EXPECT_CH(ch) SB \ |
562 | #define EXPECT_CH(ch) SB \ |
467 | if (*dec->cur != ch) \ |
563 | if (*dec->cur != ch) \ |
468 | ERR (# ch " expected"); \ |
564 | ERR (# ch " expected"); \ |
469 | ++dec->cur; \ |
565 | ++dec->cur; \ |
470 | SE |
566 | SE |
471 | |
567 | |
|
|
568 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
569 | #define DEC_DEC_DEPTH --dec->depth |
|
|
570 | |
472 | static SV *decode_sv (dec_t *dec); |
571 | static SV *decode_sv (dec_t *dec); |
473 | |
572 | |
474 | static signed char decode_hexdigit[256]; |
573 | static signed char decode_hexdigit[256]; |
475 | |
574 | |
476 | static UV |
575 | static UV |
477 | decode_4hex (dec_t *dec) |
576 | decode_4hex (dec_t *dec) |
478 | { |
577 | { |
479 | signed char d1, d2, d3, d4; |
578 | signed char d1, d2, d3, d4; |
|
|
579 | unsigned char *cur = (unsigned char *)dec->cur; |
480 | |
580 | |
481 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
482 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
581 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
483 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
484 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
582 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
485 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
486 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
583 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
487 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
488 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
584 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
489 | |
585 | |
490 | dec->cur += 4; |
586 | dec->cur += 4; |
491 | |
587 | |
492 | return ((UV)d1) << 12 |
588 | return ((UV)d1) << 12 |
493 | | ((UV)d2) << 8 |
589 | | ((UV)d2) << 8 |
… | |
… | |
496 | |
592 | |
497 | fail: |
593 | fail: |
498 | return (UV)-1; |
594 | return (UV)-1; |
499 | } |
595 | } |
500 | |
596 | |
501 | #define APPEND_GROW(n) SB \ |
|
|
502 | if (cur + (n) >= end) \ |
|
|
503 | { \ |
|
|
504 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
505 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
506 | cur = SvPVX (sv) + ofs; \ |
|
|
507 | end = SvEND (sv); \ |
|
|
508 | } \ |
|
|
509 | SE |
|
|
510 | |
|
|
511 | #define APPEND_CH(ch) SB \ |
|
|
512 | APPEND_GROW (1); \ |
|
|
513 | *cur++ = (ch); \ |
|
|
514 | SE |
|
|
515 | |
|
|
516 | static SV * |
597 | static SV * |
517 | decode_str (dec_t *dec) |
598 | decode_str (dec_t *dec) |
518 | { |
599 | { |
519 | SV *sv = NEWSV (0,2); |
600 | SV *sv = 0; |
520 | int utf8 = 0; |
601 | int utf8 = 0; |
521 | char *cur = SvPVX (sv); |
|
|
522 | char *end = SvEND (sv); |
|
|
523 | |
602 | |
524 | for (;;) |
603 | do |
525 | { |
604 | { |
526 | unsigned char ch = *(unsigned char *)dec->cur; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
606 | char *cur = buf; |
527 | |
607 | |
528 | if (ch == '"') |
608 | do |
529 | break; |
|
|
530 | else if (ch == '\\') |
|
|
531 | { |
609 | { |
532 | switch (*++dec->cur) |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
611 | |
|
|
612 | if (ch == '"') |
533 | { |
613 | { |
534 | case '\\': |
614 | --dec->cur; |
535 | case '/': |
615 | break; |
536 | case '"': APPEND_CH (*dec->cur++); break; |
616 | } |
537 | |
617 | else if (ch == '\\') |
538 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
618 | { |
539 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
619 | switch (*dec->cur) |
540 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
541 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
542 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
543 | |
|
|
544 | case 'u': |
|
|
545 | { |
620 | { |
546 | UV lo, hi; |
621 | case '\\': |
547 | ++dec->cur; |
622 | case '/': |
|
|
623 | case '"': *cur++ = *dec->cur++; break; |
548 | |
624 | |
549 | hi = decode_4hex (dec); |
625 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
550 | if (hi == (UV)-1) |
626 | case 't': ++dec->cur; *cur++ = '\011'; break; |
551 | goto fail; |
627 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
628 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
629 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
552 | |
630 | |
553 | // possibly a surrogate pair |
631 | case 'u': |
554 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
555 | { |
632 | { |
556 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
633 | UV lo, hi; |
557 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
558 | |
|
|
559 | dec->cur += 2; |
634 | ++dec->cur; |
560 | |
635 | |
561 | lo = decode_4hex (dec); |
636 | hi = decode_4hex (dec); |
562 | if (lo == (UV)-1) |
637 | if (hi == (UV)-1) |
563 | goto fail; |
638 | goto fail; |
564 | |
639 | |
|
|
640 | // possibly a surrogate pair |
|
|
641 | if (hi >= 0xd800) |
|
|
642 | if (hi < 0xdc00) |
|
|
643 | { |
|
|
644 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
645 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
646 | |
|
|
647 | dec->cur += 2; |
|
|
648 | |
|
|
649 | lo = decode_4hex (dec); |
|
|
650 | if (lo == (UV)-1) |
|
|
651 | goto fail; |
|
|
652 | |
565 | if (lo < 0xdc00 || lo >= 0xe000) |
653 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | ERR ("surrogate pair expected"); |
654 | ERR ("surrogate pair expected"); |
567 | |
655 | |
568 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
656 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
657 | } |
|
|
658 | else if (hi < 0xe000) |
|
|
659 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
660 | |
|
|
661 | if (hi >= 0x80) |
|
|
662 | { |
|
|
663 | utf8 = 1; |
|
|
664 | |
|
|
665 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
666 | } |
|
|
667 | else |
|
|
668 | *cur++ = hi; |
569 | } |
669 | } |
570 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
571 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
572 | |
|
|
573 | if (hi >= 0x80) |
|
|
574 | { |
670 | break; |
575 | utf8 = 1; |
|
|
576 | |
671 | |
577 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
578 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
579 | } |
|
|
580 | else |
672 | default: |
581 | APPEND_CH (hi); |
673 | --dec->cur; |
|
|
674 | ERR ("illegal backslash escape sequence in string"); |
582 | } |
675 | } |
583 | break; |
676 | } |
|
|
677 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
678 | *cur++ = ch; |
|
|
679 | else if (ch >= 0x80) |
|
|
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
584 | |
683 | |
585 | default: |
|
|
586 | --dec->cur; |
684 | --dec->cur; |
587 | ERR ("illegal backslash escape sequence in string"); |
685 | |
|
|
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
687 | if (clen == (STRLEN)-1) |
|
|
688 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
689 | |
|
|
690 | do |
|
|
691 | *cur++ = *dec->cur++; |
|
|
692 | while (--clen); |
|
|
693 | |
|
|
694 | utf8 = 1; |
|
|
695 | } |
|
|
696 | else |
|
|
697 | { |
|
|
698 | --dec->cur; |
|
|
699 | |
|
|
700 | if (!ch) |
|
|
701 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
702 | else |
|
|
703 | ERR ("invalid character encountered while parsing JSON string"); |
588 | } |
704 | } |
589 | } |
705 | } |
590 | else if (ch >= 0x20 && ch <= 0x7f) |
706 | while (cur < buf + SHORT_STRING_LEN); |
591 | APPEND_CH (*dec->cur++); |
707 | |
592 | else if (ch >= 0x80) |
|
|
593 | { |
708 | { |
594 | STRLEN clen; |
709 | STRLEN len = cur - buf; |
595 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
596 | if (clen == (STRLEN)-1) |
|
|
597 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
598 | |
710 | |
599 | APPEND_GROW (clen); |
711 | if (sv) |
600 | do |
|
|
601 | { |
712 | { |
602 | *cur++ = *dec->cur++; |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
|
|
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
|
|
715 | SvCUR_set (sv, SvCUR (sv) + len); |
603 | } |
716 | } |
604 | while (--clen); |
|
|
605 | |
|
|
606 | utf8 = 1; |
|
|
607 | } |
|
|
608 | else if (dec->cur == dec->end) |
|
|
609 | ERR ("unexpected end of string while parsing json string"); |
|
|
610 | else |
717 | else |
611 | ERR ("invalid character encountered"); |
718 | sv = newSVpvn (buf, len); |
612 | } |
719 | } |
|
|
720 | } |
|
|
721 | while (*dec->cur != '"'); |
613 | |
722 | |
614 | ++dec->cur; |
723 | ++dec->cur; |
615 | |
724 | |
616 | SvCUR_set (sv, cur - SvPVX (sv)); |
725 | if (sv) |
617 | |
726 | { |
618 | SvPOK_only (sv); |
727 | SvPOK_only (sv); |
619 | *SvEND (sv) = 0; |
728 | *SvEND (sv) = 0; |
620 | |
729 | |
621 | if (utf8) |
730 | if (utf8) |
622 | SvUTF8_on (sv); |
731 | SvUTF8_on (sv); |
623 | |
732 | } |
624 | if (dec->flags & F_SHRINK) |
733 | else |
625 | shrink (sv); |
734 | sv = newSVpvn ("", 0); |
626 | |
735 | |
627 | return sv; |
736 | return sv; |
628 | |
737 | |
629 | fail: |
738 | fail: |
630 | SvREFCNT_dec (sv); |
|
|
631 | return 0; |
739 | return 0; |
632 | } |
740 | } |
633 | |
741 | |
634 | static SV * |
742 | static SV * |
635 | decode_num (dec_t *dec) |
743 | decode_num (dec_t *dec) |
… | |
… | |
716 | static SV * |
824 | static SV * |
717 | decode_av (dec_t *dec) |
825 | decode_av (dec_t *dec) |
718 | { |
826 | { |
719 | AV *av = newAV (); |
827 | AV *av = newAV (); |
720 | |
828 | |
721 | WS; |
829 | DEC_INC_DEPTH; |
|
|
830 | decode_ws (dec); |
|
|
831 | |
722 | if (*dec->cur == ']') |
832 | if (*dec->cur == ']') |
723 | ++dec->cur; |
833 | ++dec->cur; |
724 | else |
834 | else |
725 | for (;;) |
835 | for (;;) |
726 | { |
836 | { |
… | |
… | |
730 | if (!value) |
840 | if (!value) |
731 | goto fail; |
841 | goto fail; |
732 | |
842 | |
733 | av_push (av, value); |
843 | av_push (av, value); |
734 | |
844 | |
735 | WS; |
845 | decode_ws (dec); |
736 | |
846 | |
737 | if (*dec->cur == ']') |
847 | if (*dec->cur == ']') |
738 | { |
848 | { |
739 | ++dec->cur; |
849 | ++dec->cur; |
740 | break; |
850 | break; |
… | |
… | |
744 | ERR (", or ] expected while parsing array"); |
854 | ERR (", or ] expected while parsing array"); |
745 | |
855 | |
746 | ++dec->cur; |
856 | ++dec->cur; |
747 | } |
857 | } |
748 | |
858 | |
|
|
859 | DEC_DEC_DEPTH; |
749 | return newRV_noinc ((SV *)av); |
860 | return newRV_noinc ((SV *)av); |
750 | |
861 | |
751 | fail: |
862 | fail: |
752 | SvREFCNT_dec (av); |
863 | SvREFCNT_dec (av); |
|
|
864 | DEC_DEC_DEPTH; |
753 | return 0; |
865 | return 0; |
754 | } |
866 | } |
755 | |
867 | |
756 | static SV * |
868 | static SV * |
757 | decode_hv (dec_t *dec) |
869 | decode_hv (dec_t *dec) |
758 | { |
870 | { |
759 | HV *hv = newHV (); |
871 | HV *hv = newHV (); |
760 | |
872 | |
761 | WS; |
873 | DEC_INC_DEPTH; |
|
|
874 | decode_ws (dec); |
|
|
875 | |
762 | if (*dec->cur == '}') |
876 | if (*dec->cur == '}') |
763 | ++dec->cur; |
877 | ++dec->cur; |
764 | else |
878 | else |
765 | for (;;) |
879 | for (;;) |
766 | { |
880 | { |
767 | SV *key, *value; |
881 | SV *key, *value; |
768 | |
882 | |
769 | WS; EXPECT_CH ('"'); |
883 | decode_ws (dec); EXPECT_CH ('"'); |
770 | |
884 | |
771 | key = decode_str (dec); |
885 | key = decode_str (dec); |
772 | if (!key) |
886 | if (!key) |
773 | goto fail; |
887 | goto fail; |
774 | |
888 | |
775 | WS; EXPECT_CH (':'); |
889 | decode_ws (dec); EXPECT_CH (':'); |
776 | |
890 | |
777 | value = decode_sv (dec); |
891 | value = decode_sv (dec); |
778 | if (!value) |
892 | if (!value) |
779 | { |
893 | { |
780 | SvREFCNT_dec (key); |
894 | SvREFCNT_dec (key); |
781 | goto fail; |
895 | goto fail; |
782 | } |
896 | } |
783 | |
897 | |
784 | //TODO: optimise |
|
|
785 | hv_store_ent (hv, key, value, 0); |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
786 | |
900 | |
787 | WS; |
901 | decode_ws (dec); |
788 | |
902 | |
789 | if (*dec->cur == '}') |
903 | if (*dec->cur == '}') |
790 | { |
904 | { |
791 | ++dec->cur; |
905 | ++dec->cur; |
792 | break; |
906 | break; |
… | |
… | |
796 | ERR (", or } expected while parsing object/hash"); |
910 | ERR (", or } expected while parsing object/hash"); |
797 | |
911 | |
798 | ++dec->cur; |
912 | ++dec->cur; |
799 | } |
913 | } |
800 | |
914 | |
|
|
915 | DEC_DEC_DEPTH; |
801 | return newRV_noinc ((SV *)hv); |
916 | return newRV_noinc ((SV *)hv); |
802 | |
917 | |
803 | fail: |
918 | fail: |
804 | SvREFCNT_dec (hv); |
919 | SvREFCNT_dec (hv); |
|
|
920 | DEC_DEC_DEPTH; |
805 | return 0; |
921 | return 0; |
806 | } |
922 | } |
807 | |
923 | |
808 | static SV * |
924 | static SV * |
809 | decode_sv (dec_t *dec) |
925 | decode_sv (dec_t *dec) |
810 | { |
926 | { |
811 | WS; |
927 | decode_ws (dec); |
812 | switch (*dec->cur) |
928 | switch (*dec->cur) |
813 | { |
929 | { |
814 | case '"': ++dec->cur; return decode_str (dec); |
930 | case '"': ++dec->cur; return decode_str (dec); |
815 | case '[': ++dec->cur; return decode_av (dec); |
931 | case '[': ++dec->cur; return decode_av (dec); |
816 | case '{': ++dec->cur; return decode_hv (dec); |
932 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
852 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
853 | |
969 | |
854 | break; |
970 | break; |
855 | |
971 | |
856 | default: |
972 | default: |
857 | ERR ("malformed json string, neither array, object, number, string or atom"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
858 | break; |
974 | break; |
859 | } |
975 | } |
860 | |
976 | |
861 | fail: |
977 | fail: |
862 | return 0; |
978 | return 0; |
863 | } |
979 | } |
864 | |
980 | |
865 | static SV * |
981 | static SV * |
866 | decode_json (SV *string, UV flags) |
982 | decode_json (SV *string, U32 flags) |
867 | { |
983 | { |
|
|
984 | dec_t dec; |
868 | SV *sv; |
985 | SV *sv; |
|
|
986 | |
|
|
987 | SvUPGRADE (string, SVt_PV); |
869 | |
988 | |
870 | if (flags & F_UTF8) |
989 | if (flags & F_UTF8) |
871 | sv_utf8_downgrade (string, 0); |
990 | sv_utf8_downgrade (string, 0); |
872 | else |
991 | else |
873 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
874 | |
993 | |
875 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
876 | |
995 | |
877 | dec_t dec; |
|
|
878 | dec.flags = flags; |
996 | dec.flags = flags; |
879 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
880 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
881 | dec.err = 0; |
999 | dec.err = 0; |
|
|
1000 | dec.depth = 0; |
|
|
1001 | dec.maxdepth = DEC_DEPTH (dec.flags); |
882 | |
1002 | |
|
|
1003 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
883 | sv = decode_sv (&dec); |
1004 | sv = decode_sv (&dec); |
884 | |
1005 | |
885 | if (!sv) |
1006 | if (!sv) |
886 | { |
1007 | { |
887 | IV offset = dec.flags & F_UTF8 |
1008 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
896 | SAVEVPTR (PL_curcop); |
1017 | SAVEVPTR (PL_curcop); |
897 | PL_curcop = &cop; |
1018 | PL_curcop = &cop; |
898 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1019 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
899 | LEAVE; |
1020 | LEAVE; |
900 | |
1021 | |
901 | croak ("%s, at character offset %d (%s)", |
1022 | croak ("%s, at character offset %d [\"%s\"]", |
902 | dec.err, |
1023 | dec.err, |
903 | (int)offset, |
1024 | (int)offset, |
904 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1025 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | } |
1026 | } |
906 | |
1027 | |
… | |
… | |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1031 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
911 | |
1032 | |
912 | return sv; |
1033 | return sv; |
913 | } |
1034 | } |
914 | |
1035 | |
|
|
1036 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1037 | // XS interface functions |
|
|
1038 | |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
1039 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | |
1040 | |
917 | BOOT: |
1041 | BOOT: |
918 | { |
1042 | { |
919 | int i; |
1043 | int i; |
920 | |
1044 | |
921 | memset (decode_hexdigit, 0xff, 256); |
1045 | memset (decode_hexdigit, 0xff, 256); |
|
|
1046 | |
922 | for (i = 10; i--; ) |
1047 | for (i = 0; i < 256; ++i) |
923 | decode_hexdigit ['0' + i] = i; |
1048 | decode_hexdigit [i] = |
924 | |
1049 | i >= '0' && i <= '9' ? i - '0' |
925 | for (i = 7; i--; ) |
1050 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
926 | { |
1051 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
927 | decode_hexdigit ['a' + i] = 10 + i; |
1052 | : -1; |
928 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
929 | } |
|
|
930 | |
1053 | |
931 | json_stash = gv_stashpv ("JSON::XS", 1); |
1054 | json_stash = gv_stashpv ("JSON::XS", 1); |
932 | } |
1055 | } |
933 | |
1056 | |
934 | PROTOTYPES: DISABLE |
1057 | PROTOTYPES: DISABLE |
… | |
… | |
961 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
962 | } |
1085 | } |
963 | OUTPUT: |
1086 | OUTPUT: |
964 | RETVAL |
1087 | RETVAL |
965 | |
1088 | |
|
|
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1090 | CODE: |
|
|
1091 | { |
|
|
1092 | UV *uv = SvJSON (self); |
|
|
1093 | UV log2 = 0; |
|
|
1094 | |
|
|
1095 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1096 | |
|
|
1097 | while ((1UL << log2) < max_depth) |
|
|
1098 | ++log2; |
|
|
1099 | |
|
|
1100 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1101 | |
|
|
1102 | RETVAL = newSVsv (self); |
|
|
1103 | } |
|
|
1104 | OUTPUT: |
|
|
1105 | RETVAL |
|
|
1106 | |
966 | void encode (SV *self, SV *scalar) |
1107 | void encode (SV *self, SV *scalar) |
967 | PPCODE: |
1108 | PPCODE: |
968 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1109 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
969 | |
1110 | |
970 | void decode (SV *self, SV *jsonstr) |
1111 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
972 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1113 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
973 | |
1114 | |
974 | PROTOTYPES: ENABLE |
1115 | PROTOTYPES: ENABLE |
975 | |
1116 | |
976 | void to_json (SV *scalar) |
1117 | void to_json (SV *scalar) |
|
|
1118 | ALIAS: |
|
|
1119 | objToJson = 0 |
977 | PPCODE: |
1120 | PPCODE: |
978 | XPUSHs (encode_json (scalar, F_UTF8)); |
1121 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
979 | |
1122 | |
980 | void from_json (SV *jsonstr) |
1123 | void from_json (SV *jsonstr) |
|
|
1124 | ALIAS: |
|
|
1125 | jsonToObj = 0 |
981 | PPCODE: |
1126 | PPCODE: |
982 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1127 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
983 | |
1128 | |