… | |
… | |
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 |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
|
|
25 | |
|
|
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
27 | |
|
|
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
30 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
33 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
36 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
25 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
26 | |
38 | |
27 | #define SB do { |
39 | #define SB do { |
28 | #define SE } while (0) |
40 | #define SE } while (0) |
|
|
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
29 | |
52 | |
30 | static HV *json_stash; // JSON::XS:: |
53 | static HV *json_stash; // JSON::XS:: |
31 | |
54 | |
32 | ///////////////////////////////////////////////////////////////////////////// |
55 | ///////////////////////////////////////////////////////////////////////////// |
33 | // utility functions |
56 | // utility functions |
… | |
… | |
58 | // decode an utf-8 character and return it, or (UV)-1 in |
81 | // decode an utf-8 character and return it, or (UV)-1 in |
59 | // case of an error. |
82 | // case of an error. |
60 | // we special-case "safe" characters from U+80 .. U+7FF, |
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
61 | // but use the very good perl function to parse anything else. |
84 | // but use the very good perl function to parse anything else. |
62 | // note that we never call this function for a ascii codepoints |
85 | // note that we never call this function for a ascii codepoints |
63 | static UV |
86 | inline UV |
64 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
65 | { |
88 | { |
66 | if (s[0] > 0xdf || s[0] < 0xc2) |
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
67 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
68 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
69 | { |
92 | { |
70 | *clen = 2; |
93 | *clen = 2; |
71 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
72 | } |
95 | } |
73 | else |
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
74 | return (UV)-1; |
99 | return (UV)-1; |
|
|
100 | } |
75 | } |
101 | } |
76 | |
102 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
78 | // encoder |
104 | // encoder |
79 | |
105 | |
… | |
… | |
81 | typedef struct |
107 | typedef struct |
82 | { |
108 | { |
83 | char *cur; // SvPVX (sv) + current output position |
109 | char *cur; // SvPVX (sv) + current output position |
84 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
85 | SV *sv; // result scalar |
111 | SV *sv; // result scalar |
86 | UV flags; // F_* |
112 | U32 flags; // F_* |
87 | int indent; // indentation level |
113 | U32 indent; // indentation level |
88 | int max_depth; // max. recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
89 | } enc_t; |
115 | } enc_t; |
90 | |
116 | |
91 | static void |
117 | inline void |
92 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
93 | { |
119 | { |
94 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
95 | { |
121 | { |
96 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
97 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
98 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
99 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
100 | } |
126 | } |
101 | } |
127 | } |
102 | |
128 | |
103 | static void |
129 | inline void |
104 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
105 | { |
131 | { |
106 | need (enc, 1); |
132 | need (enc, 1); |
107 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
108 | } |
134 | } |
… | |
… | |
116 | |
142 | |
117 | while (str < end) |
143 | while (str < end) |
118 | { |
144 | { |
119 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
120 | |
146 | |
121 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
122 | { |
148 | { |
123 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
124 | { |
150 | { |
125 | need (enc, len += 1); |
151 | need (enc, len += 1); |
126 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
127 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
128 | } |
154 | } |
129 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
130 | { |
156 | { |
131 | need (enc, len += 1); |
157 | need (enc, len += 1); |
132 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
133 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
134 | } |
160 | } |
… | |
… | |
166 | } |
192 | } |
167 | |
193 | |
168 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
169 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
170 | |
196 | |
171 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
172 | { |
198 | { |
173 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
174 | { |
200 | { |
175 | need (enc, len += 11); |
201 | need (enc, len += 11); |
176 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
190 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
191 | } |
217 | } |
192 | |
218 | |
193 | str += clen; |
219 | str += clen; |
194 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
195 | else if (is_utf8) |
226 | else if (is_utf8) |
196 | { |
227 | { |
197 | need (enc, len += clen); |
228 | need (enc, len += clen); |
198 | do |
229 | do |
199 | { |
230 | { |
… | |
… | |
201 | } |
232 | } |
202 | while (--clen); |
233 | while (--clen); |
203 | } |
234 | } |
204 | else |
235 | else |
205 | { |
236 | { |
206 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
237 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
207 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
208 | ++str; |
239 | ++str; |
209 | } |
240 | } |
210 | } |
241 | } |
211 | } |
242 | } |
… | |
… | |
213 | |
244 | |
214 | --len; |
245 | --len; |
215 | } |
246 | } |
216 | } |
247 | } |
217 | |
248 | |
218 | static void |
249 | inline void |
219 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
220 | { |
251 | { |
221 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
222 | { |
253 | { |
223 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
226 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
227 | enc->cur += spaces; |
258 | enc->cur += spaces; |
228 | } |
259 | } |
229 | } |
260 | } |
230 | |
261 | |
231 | static void |
262 | inline void |
232 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
233 | { |
264 | { |
234 | need (enc, 1); |
265 | need (enc, 1); |
235 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
236 | } |
267 | } |
237 | |
268 | |
238 | static void |
269 | inline void |
239 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
240 | { |
271 | { |
241 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
242 | { |
273 | { |
243 | need (enc, 1); |
274 | need (enc, 1); |
244 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
245 | } |
276 | } |
246 | } |
277 | } |
247 | |
278 | |
248 | static void |
279 | inline void |
249 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
250 | { |
281 | { |
251 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
252 | |
283 | |
253 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
260 | |
291 | |
261 | static void |
292 | static void |
262 | encode_av (enc_t *enc, AV *av) |
293 | encode_av (enc_t *enc, AV *av) |
263 | { |
294 | { |
264 | int i, len = av_len (av); |
295 | int i, len = av_len (av); |
|
|
296 | |
|
|
297 | if (enc->indent >= enc->maxdepth) |
|
|
298 | croak ("data structure too deep (hit recursion limit)"); |
265 | |
299 | |
266 | encode_ch (enc, '['); encode_nl (enc); |
300 | encode_ch (enc, '['); encode_nl (enc); |
267 | ++enc->indent; |
301 | ++enc->indent; |
268 | |
302 | |
269 | for (i = 0; i <= len; ++i) |
303 | for (i = 0; i <= len; ++i) |
… | |
… | |
336 | static void |
370 | static void |
337 | encode_hv (enc_t *enc, HV *hv) |
371 | encode_hv (enc_t *enc, HV *hv) |
338 | { |
372 | { |
339 | int count, i; |
373 | int count, i; |
340 | |
374 | |
|
|
375 | if (enc->indent >= enc->maxdepth) |
|
|
376 | croak ("data structure too deep (hit recursion limit)"); |
|
|
377 | |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
378 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
342 | |
379 | |
343 | if ((count = hv_iterinit (hv))) |
380 | if ((count = hv_iterinit (hv))) |
344 | { |
381 | { |
345 | // for canonical output we have to sort by keys first |
382 | // for canonical output we have to sort by keys first |
346 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
347 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
348 | // that randomises hash orderings |
385 | // that randomises hash orderings |
349 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
350 | { |
387 | { |
351 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
352 | int fast = 1; |
388 | int fast = 1; |
|
|
389 | HE *he; |
|
|
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
391 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
392 | #else |
|
|
393 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
394 | #endif |
353 | |
395 | |
354 | i = 0; |
396 | i = 0; |
355 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
356 | { |
398 | { |
357 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
392 | |
434 | |
393 | encode_nl (enc); |
435 | encode_nl (enc); |
394 | } |
436 | } |
395 | else |
437 | else |
396 | { |
438 | { |
397 | SV *sv; |
|
|
398 | HE *he = hv_iternext (hv); |
439 | HE *he = hv_iternext (hv); |
399 | |
440 | |
400 | for (;;) |
441 | for (;;) |
401 | { |
442 | { |
402 | encode_indent (enc); |
443 | encode_indent (enc); |
… | |
… | |
411 | encode_nl (enc); |
452 | encode_nl (enc); |
412 | } |
453 | } |
413 | } |
454 | } |
414 | |
455 | |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
456 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
457 | } |
|
|
458 | |
|
|
459 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
460 | static void |
|
|
461 | encode_rv (enc_t *enc, SV *sv) |
|
|
462 | { |
|
|
463 | svtype svt; |
|
|
464 | |
|
|
465 | SvGETMAGIC (sv); |
|
|
466 | svt = SvTYPE (sv); |
|
|
467 | |
|
|
468 | if (svt == SVt_PVHV) |
|
|
469 | encode_hv (enc, (HV *)sv); |
|
|
470 | else if (svt == SVt_PVAV) |
|
|
471 | encode_av (enc, (AV *)sv); |
|
|
472 | else if (svt < SVt_PVAV) |
|
|
473 | { |
|
|
474 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
475 | encode_str (enc, "false", 5, 0); |
|
|
476 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
477 | encode_str (enc, "true", 4, 0); |
|
|
478 | else |
|
|
479 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
480 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
481 | } |
|
|
482 | else |
|
|
483 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
484 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
416 | } |
485 | } |
417 | |
486 | |
418 | static void |
487 | static void |
419 | encode_sv (enc_t *enc, SV *sv) |
488 | encode_sv (enc_t *enc, SV *sv) |
420 | { |
489 | { |
… | |
… | |
428 | encode_str (enc, str, len, SvUTF8 (sv)); |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
429 | encode_ch (enc, '"'); |
498 | encode_ch (enc, '"'); |
430 | } |
499 | } |
431 | else if (SvNOKp (sv)) |
500 | else if (SvNOKp (sv)) |
432 | { |
501 | { |
|
|
502 | // trust that perl will do the right thing w.r.t. JSON syntax. |
433 | need (enc, NV_DIG + 32); |
503 | need (enc, NV_DIG + 32); |
434 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
435 | enc->cur += strlen (enc->cur); |
505 | enc->cur += strlen (enc->cur); |
436 | } |
506 | } |
437 | else if (SvIOKp (sv)) |
507 | else if (SvIOKp (sv)) |
438 | { |
508 | { |
439 | need (enc, 64); |
509 | // we assume we can always read an IV as a UV |
|
|
510 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
511 | { |
|
|
512 | // large integer, use the (rather slow) snprintf way. |
|
|
513 | need (enc, sizeof (UV) * 3); |
440 | enc->cur += |
514 | enc->cur += |
441 | SvIsUV(sv) |
515 | SvIsUV(sv) |
442 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
516 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
443 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
517 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
518 | } |
|
|
519 | else |
|
|
520 | { |
|
|
521 | // optimise the "small number case" |
|
|
522 | // code will likely be branchless and use only a single multiplication |
|
|
523 | I32 i = SvIV (sv); |
|
|
524 | U32 u; |
|
|
525 | char digit, nz = 0; |
|
|
526 | |
|
|
527 | need (enc, 6); |
|
|
528 | |
|
|
529 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
530 | u = i < 0 ? -i : i; |
|
|
531 | |
|
|
532 | // convert to 4.28 fixed-point representation |
|
|
533 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
534 | |
|
|
535 | // now output digit by digit, each time masking out the integer part |
|
|
536 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
537 | // resulting in a net multiplication by 10. |
|
|
538 | // we always write the digit to memory but conditionally increment |
|
|
539 | // the pointer, to ease the usage of conditional move instructions. |
|
|
540 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
541 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
542 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
543 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
544 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
545 | } |
444 | } |
546 | } |
445 | else if (SvROK (sv)) |
547 | else if (SvROK (sv)) |
446 | { |
548 | encode_rv (enc, SvRV (sv)); |
447 | SV *rv = SvRV (sv); |
|
|
448 | |
|
|
449 | if (enc->indent >= enc->max_depth) |
|
|
450 | croak ("data structure too deep (hit recursion limit)"); |
|
|
451 | |
|
|
452 | switch (SvTYPE (rv)) |
|
|
453 | { |
|
|
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
455 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
456 | |
|
|
457 | default: |
|
|
458 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
459 | SvPV_nolen (sv)); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | else if (!SvOK (sv)) |
549 | else if (!SvOK (sv)) |
463 | encode_str (enc, "null", 4, 0); |
550 | encode_str (enc, "null", 4, 0); |
464 | else |
551 | else |
465 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
552 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
466 | SvPV_nolen (sv), SvFLAGS (sv)); |
553 | SvPV_nolen (sv), SvFLAGS (sv)); |
467 | } |
554 | } |
468 | |
555 | |
469 | static SV * |
556 | static SV * |
470 | encode_json (SV *scalar, UV flags) |
557 | encode_json (SV *scalar, U32 flags) |
471 | { |
558 | { |
|
|
559 | enc_t enc; |
|
|
560 | |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
561 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
473 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
562 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
474 | |
563 | |
475 | enc_t enc; |
|
|
476 | enc.flags = flags; |
564 | enc.flags = flags; |
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
565 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
478 | enc.cur = SvPVX (enc.sv); |
566 | enc.cur = SvPVX (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
567 | enc.end = SvEND (enc.sv); |
480 | enc.indent = 0; |
568 | enc.indent = 0; |
481 | enc.max_depth = 0x7fffffffUL; |
569 | enc.maxdepth = DEC_DEPTH (flags); |
482 | |
570 | |
483 | SvPOK_only (enc.sv); |
571 | SvPOK_only (enc.sv); |
484 | encode_sv (&enc, scalar); |
572 | encode_sv (&enc, scalar); |
485 | |
573 | |
|
|
574 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
575 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
576 | |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
577 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
487 | SvUTF8_on (enc.sv); |
578 | SvUTF8_on (enc.sv); |
488 | |
|
|
489 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
490 | |
579 | |
491 | if (enc.flags & F_SHRINK) |
580 | if (enc.flags & F_SHRINK) |
492 | shrink (enc.sv); |
581 | shrink (enc.sv); |
493 | |
582 | |
494 | return enc.sv; |
583 | return enc.sv; |
… | |
… | |
501 | typedef struct |
590 | typedef struct |
502 | { |
591 | { |
503 | char *cur; // current parser pointer |
592 | char *cur; // current parser pointer |
504 | char *end; // end of input string |
593 | char *end; // end of input string |
505 | const char *err; // parse error, if != 0 |
594 | const char *err; // parse error, if != 0 |
506 | UV flags; // F_* |
595 | U32 flags; // F_* |
|
|
596 | U32 depth; // recursion depth |
|
|
597 | U32 maxdepth; // recursion depth limit |
507 | } dec_t; |
598 | } dec_t; |
508 | |
599 | |
509 | static void |
600 | inline void |
510 | decode_ws (dec_t *dec) |
601 | decode_ws (dec_t *dec) |
511 | { |
602 | { |
512 | for (;;) |
603 | for (;;) |
513 | { |
604 | { |
514 | char ch = *dec->cur; |
605 | char ch = *dec->cur; |
… | |
… | |
520 | ++dec->cur; |
611 | ++dec->cur; |
521 | } |
612 | } |
522 | } |
613 | } |
523 | |
614 | |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
615 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
616 | |
525 | #define EXPECT_CH(ch) SB \ |
617 | #define EXPECT_CH(ch) SB \ |
526 | if (*dec->cur != ch) \ |
618 | if (*dec->cur != ch) \ |
527 | ERR (# ch " expected"); \ |
619 | ERR (# ch " expected"); \ |
528 | ++dec->cur; \ |
620 | ++dec->cur; \ |
529 | SE |
621 | SE |
530 | |
622 | |
|
|
623 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
624 | #define DEC_DEC_DEPTH --dec->depth |
|
|
625 | |
531 | static SV *decode_sv (dec_t *dec); |
626 | static SV *decode_sv (dec_t *dec); |
532 | |
627 | |
533 | static signed char decode_hexdigit[256]; |
628 | static signed char decode_hexdigit[256]; |
534 | |
629 | |
535 | static UV |
630 | static UV |
536 | decode_4hex (dec_t *dec) |
631 | decode_4hex (dec_t *dec) |
537 | { |
632 | { |
538 | signed char d1, d2, d3, d4; |
633 | signed char d1, d2, d3, d4; |
539 | unsigned char *cur = (unsigned char *)dec->cur; |
634 | unsigned char *cur = (unsigned char *)dec->cur; |
540 | |
635 | |
541 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
542 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
543 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
544 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
545 | |
640 | |
546 | dec->cur += 4; |
641 | dec->cur += 4; |
547 | |
642 | |
548 | return ((UV)d1) << 12 |
643 | return ((UV)d1) << 12 |
549 | | ((UV)d2) << 8 |
644 | | ((UV)d2) << 8 |
… | |
… | |
557 | static SV * |
652 | static SV * |
558 | decode_str (dec_t *dec) |
653 | decode_str (dec_t *dec) |
559 | { |
654 | { |
560 | SV *sv = 0; |
655 | SV *sv = 0; |
561 | int utf8 = 0; |
656 | int utf8 = 0; |
|
|
657 | char *dec_cur = dec->cur; |
562 | |
658 | |
563 | do |
659 | do |
564 | { |
660 | { |
565 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
661 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
566 | char *cur = buf; |
662 | char *cur = buf; |
567 | |
663 | |
568 | do |
664 | do |
569 | { |
665 | { |
570 | unsigned char ch = *(unsigned char *)dec->cur++; |
666 | unsigned char ch = *(unsigned char *)dec_cur++; |
571 | |
667 | |
572 | if (ch == '"') |
668 | if (expect_false (ch == '"')) |
573 | { |
669 | { |
574 | --dec->cur; |
670 | --dec_cur; |
575 | break; |
671 | break; |
576 | } |
672 | } |
577 | else if (ch == '\\') |
673 | else if (expect_false (ch == '\\')) |
578 | { |
674 | { |
579 | switch (*dec->cur) |
675 | switch (*dec_cur) |
580 | { |
676 | { |
581 | case '\\': |
677 | case '\\': |
582 | case '/': |
678 | case '/': |
583 | case '"': *cur++ = *dec->cur++; break; |
679 | case '"': *cur++ = *dec_cur++; break; |
584 | |
680 | |
585 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
681 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
586 | case 't': ++dec->cur; *cur++ = '\011'; break; |
682 | case 't': ++dec_cur; *cur++ = '\011'; break; |
587 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
683 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
588 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
684 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
589 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
685 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
590 | |
686 | |
591 | case 'u': |
687 | case 'u': |
592 | { |
688 | { |
593 | UV lo, hi; |
689 | UV lo, hi; |
594 | ++dec->cur; |
690 | ++dec_cur; |
595 | |
691 | |
|
|
692 | dec->cur = dec_cur; |
596 | hi = decode_4hex (dec); |
693 | hi = decode_4hex (dec); |
|
|
694 | dec_cur = dec->cur; |
597 | if (hi == (UV)-1) |
695 | if (hi == (UV)-1) |
598 | goto fail; |
696 | goto fail; |
599 | |
697 | |
600 | // possibly a surrogate pair |
698 | // possibly a surrogate pair |
601 | if (hi >= 0xd800) |
699 | if (hi >= 0xd800) |
602 | if (hi < 0xdc00) |
700 | if (hi < 0xdc00) |
603 | { |
701 | { |
604 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
702 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
605 | ERR ("missing low surrogate character in surrogate pair"); |
703 | ERR ("missing low surrogate character in surrogate pair"); |
606 | |
704 | |
607 | dec->cur += 2; |
705 | dec_cur += 2; |
608 | |
706 | |
|
|
707 | dec->cur = dec_cur; |
609 | lo = decode_4hex (dec); |
708 | lo = decode_4hex (dec); |
|
|
709 | dec_cur = dec->cur; |
610 | if (lo == (UV)-1) |
710 | if (lo == (UV)-1) |
611 | goto fail; |
711 | goto fail; |
612 | |
712 | |
613 | if (lo < 0xdc00 || lo >= 0xe000) |
713 | if (lo < 0xdc00 || lo >= 0xe000) |
614 | ERR ("surrogate pair expected"); |
714 | ERR ("surrogate pair expected"); |
… | |
… | |
628 | *cur++ = hi; |
728 | *cur++ = hi; |
629 | } |
729 | } |
630 | break; |
730 | break; |
631 | |
731 | |
632 | default: |
732 | default: |
633 | --dec->cur; |
733 | --dec_cur; |
634 | ERR ("illegal backslash escape sequence in string"); |
734 | ERR ("illegal backslash escape sequence in string"); |
635 | } |
735 | } |
636 | } |
736 | } |
637 | else if (ch >= 0x20 && ch <= 0x7f) |
737 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
638 | *cur++ = ch; |
738 | *cur++ = ch; |
639 | else if (ch >= 0x80) |
739 | else if (ch >= 0x80) |
640 | { |
740 | { |
641 | --dec->cur; |
|
|
642 | |
|
|
643 | STRLEN clen; |
741 | STRLEN clen; |
|
|
742 | UV uch; |
|
|
743 | |
|
|
744 | --dec_cur; |
|
|
745 | |
644 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
746 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
645 | if (clen == (STRLEN)-1) |
747 | if (clen == (STRLEN)-1) |
646 | ERR ("malformed UTF-8 character in JSON string"); |
748 | ERR ("malformed UTF-8 character in JSON string"); |
647 | |
749 | |
648 | do |
750 | do |
649 | { |
|
|
650 | *cur++ = *dec->cur++; |
751 | *cur++ = *dec_cur++; |
651 | } |
|
|
652 | while (--clen); |
752 | while (--clen); |
653 | |
753 | |
654 | utf8 = 1; |
754 | utf8 = 1; |
655 | } |
755 | } |
656 | else if (!ch) |
|
|
657 | ERR ("unexpected end of string while parsing json string"); |
|
|
658 | else |
756 | else |
|
|
757 | { |
|
|
758 | --dec_cur; |
|
|
759 | |
|
|
760 | if (!ch) |
|
|
761 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
762 | else |
659 | ERR ("invalid character encountered"); |
763 | ERR ("invalid character encountered while parsing JSON string"); |
660 | |
764 | } |
661 | } |
765 | } |
662 | while (cur < buf + SHORT_STRING_LEN); |
766 | while (cur < buf + SHORT_STRING_LEN); |
663 | |
767 | |
|
|
768 | { |
664 | STRLEN len = cur - buf; |
769 | STRLEN len = cur - buf; |
665 | |
770 | |
666 | if (sv) |
771 | if (sv) |
667 | { |
772 | { |
668 | SvGROW (sv, SvCUR (sv) + len + 1); |
773 | SvGROW (sv, SvCUR (sv) + len + 1); |
669 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
774 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
670 | SvCUR_set (sv, SvCUR (sv) + len); |
775 | SvCUR_set (sv, SvCUR (sv) + len); |
671 | } |
776 | } |
672 | else |
777 | else |
673 | sv = newSVpvn (buf, len); |
778 | sv = newSVpvn (buf, len); |
674 | } |
779 | } |
|
|
780 | } |
675 | while (*dec->cur != '"'); |
781 | while (*dec_cur != '"'); |
676 | |
782 | |
677 | ++dec->cur; |
783 | ++dec_cur; |
678 | |
784 | |
679 | if (sv) |
785 | if (sv) |
680 | { |
786 | { |
681 | SvPOK_only (sv); |
787 | SvPOK_only (sv); |
682 | *SvEND (sv) = 0; |
788 | *SvEND (sv) = 0; |
… | |
… | |
685 | SvUTF8_on (sv); |
791 | SvUTF8_on (sv); |
686 | } |
792 | } |
687 | else |
793 | else |
688 | sv = newSVpvn ("", 0); |
794 | sv = newSVpvn ("", 0); |
689 | |
795 | |
|
|
796 | dec->cur = dec_cur; |
690 | return sv; |
797 | return sv; |
691 | |
798 | |
692 | fail: |
799 | fail: |
|
|
800 | dec->cur = dec_cur; |
693 | return 0; |
801 | return 0; |
694 | } |
802 | } |
695 | |
803 | |
696 | static SV * |
804 | static SV * |
697 | decode_num (dec_t *dec) |
805 | decode_num (dec_t *dec) |
… | |
… | |
755 | is_nv = 1; |
863 | is_nv = 1; |
756 | } |
864 | } |
757 | |
865 | |
758 | if (!is_nv) |
866 | if (!is_nv) |
759 | { |
867 | { |
760 | UV uv; |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
761 | int numtype = grok_number (start, dec->cur - start, &uv); |
869 | if (*start == '-') |
762 | if (numtype & IS_NUMBER_IN_UV) |
870 | switch (dec->cur - start) |
763 | if (numtype & IS_NUMBER_NEG) |
|
|
764 | { |
871 | { |
765 | if (uv < (UV)IV_MIN) |
872 | case 2: return newSViv (-( start [1] - '0' )); |
766 | return newSViv (-(IV)uv); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
767 | } |
876 | } |
|
|
877 | else |
|
|
878 | switch (dec->cur - start) |
|
|
879 | { |
|
|
880 | case 1: return newSViv ( start [0] - '0' ); |
|
|
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
884 | } |
|
|
885 | |
|
|
886 | { |
|
|
887 | UV uv; |
|
|
888 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
889 | if (numtype & IS_NUMBER_IN_UV) |
|
|
890 | if (numtype & IS_NUMBER_NEG) |
|
|
891 | { |
|
|
892 | if (uv < (UV)IV_MIN) |
|
|
893 | return newSViv (-(IV)uv); |
|
|
894 | } |
768 | else |
895 | else |
769 | return newSVuv (uv); |
896 | return newSVuv (uv); |
|
|
897 | } |
770 | } |
898 | } |
771 | |
899 | |
772 | return newSVnv (Atof (start)); |
900 | return newSVnv (Atof (start)); |
773 | |
901 | |
774 | fail: |
902 | fail: |
… | |
… | |
778 | static SV * |
906 | static SV * |
779 | decode_av (dec_t *dec) |
907 | decode_av (dec_t *dec) |
780 | { |
908 | { |
781 | AV *av = newAV (); |
909 | AV *av = newAV (); |
782 | |
910 | |
|
|
911 | DEC_INC_DEPTH; |
783 | decode_ws (dec); |
912 | decode_ws (dec); |
|
|
913 | |
784 | if (*dec->cur == ']') |
914 | if (*dec->cur == ']') |
785 | ++dec->cur; |
915 | ++dec->cur; |
786 | else |
916 | else |
787 | for (;;) |
917 | for (;;) |
788 | { |
918 | { |
… | |
… | |
806 | ERR (", or ] expected while parsing array"); |
936 | ERR (", or ] expected while parsing array"); |
807 | |
937 | |
808 | ++dec->cur; |
938 | ++dec->cur; |
809 | } |
939 | } |
810 | |
940 | |
|
|
941 | DEC_DEC_DEPTH; |
811 | return newRV_noinc ((SV *)av); |
942 | return newRV_noinc ((SV *)av); |
812 | |
943 | |
813 | fail: |
944 | fail: |
814 | SvREFCNT_dec (av); |
945 | SvREFCNT_dec (av); |
|
|
946 | DEC_DEC_DEPTH; |
815 | return 0; |
947 | return 0; |
816 | } |
948 | } |
817 | |
949 | |
818 | static SV * |
950 | static SV * |
819 | decode_hv (dec_t *dec) |
951 | decode_hv (dec_t *dec) |
820 | { |
952 | { |
821 | HV *hv = newHV (); |
953 | HV *hv = newHV (); |
822 | |
954 | |
|
|
955 | DEC_INC_DEPTH; |
823 | decode_ws (dec); |
956 | decode_ws (dec); |
|
|
957 | |
824 | if (*dec->cur == '}') |
958 | if (*dec->cur == '}') |
825 | ++dec->cur; |
959 | ++dec->cur; |
826 | else |
960 | else |
827 | for (;;) |
961 | for (;;) |
828 | { |
962 | { |
… | |
… | |
841 | { |
975 | { |
842 | SvREFCNT_dec (key); |
976 | SvREFCNT_dec (key); |
843 | goto fail; |
977 | goto fail; |
844 | } |
978 | } |
845 | |
979 | |
846 | //TODO: optimise |
|
|
847 | hv_store_ent (hv, key, value, 0); |
980 | hv_store_ent (hv, key, value, 0); |
|
|
981 | SvREFCNT_dec (key); |
848 | |
982 | |
849 | decode_ws (dec); |
983 | decode_ws (dec); |
850 | |
984 | |
851 | if (*dec->cur == '}') |
985 | if (*dec->cur == '}') |
852 | { |
986 | { |
… | |
… | |
858 | ERR (", or } expected while parsing object/hash"); |
992 | ERR (", or } expected while parsing object/hash"); |
859 | |
993 | |
860 | ++dec->cur; |
994 | ++dec->cur; |
861 | } |
995 | } |
862 | |
996 | |
|
|
997 | DEC_DEC_DEPTH; |
863 | return newRV_noinc ((SV *)hv); |
998 | return newRV_noinc ((SV *)hv); |
864 | |
999 | |
865 | fail: |
1000 | fail: |
866 | SvREFCNT_dec (hv); |
1001 | SvREFCNT_dec (hv); |
|
|
1002 | DEC_DEC_DEPTH; |
867 | return 0; |
1003 | return 0; |
868 | } |
1004 | } |
869 | |
1005 | |
870 | static SV * |
1006 | static SV * |
871 | decode_sv (dec_t *dec) |
1007 | decode_sv (dec_t *dec) |
… | |
… | |
914 | ERR ("'null' expected"); |
1050 | ERR ("'null' expected"); |
915 | |
1051 | |
916 | break; |
1052 | break; |
917 | |
1053 | |
918 | default: |
1054 | default: |
919 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1055 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
920 | break; |
1056 | break; |
921 | } |
1057 | } |
922 | |
1058 | |
923 | fail: |
1059 | fail: |
924 | return 0; |
1060 | return 0; |
925 | } |
1061 | } |
926 | |
1062 | |
927 | static SV * |
1063 | static SV * |
928 | decode_json (SV *string, UV flags) |
1064 | decode_json (SV *string, U32 flags, UV *offset_return) |
929 | { |
1065 | { |
|
|
1066 | dec_t dec; |
|
|
1067 | UV offset; |
930 | SV *sv; |
1068 | SV *sv; |
|
|
1069 | |
|
|
1070 | SvGETMAGIC (string); |
|
|
1071 | SvUPGRADE (string, SVt_PV); |
931 | |
1072 | |
932 | if (flags & F_UTF8) |
1073 | if (flags & F_UTF8) |
933 | sv_utf8_downgrade (string, 0); |
1074 | sv_utf8_downgrade (string, 0); |
934 | else |
1075 | else |
935 | sv_utf8_upgrade (string); |
1076 | sv_utf8_upgrade (string); |
936 | |
1077 | |
937 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1078 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
938 | |
1079 | |
939 | dec_t dec; |
|
|
940 | dec.flags = flags; |
1080 | dec.flags = flags; |
941 | dec.cur = SvPVX (string); |
1081 | dec.cur = SvPVX (string); |
942 | dec.end = SvEND (string); |
1082 | dec.end = SvEND (string); |
943 | dec.err = 0; |
1083 | dec.err = 0; |
|
|
1084 | dec.depth = 0; |
|
|
1085 | dec.maxdepth = DEC_DEPTH (dec.flags); |
944 | |
1086 | |
|
|
1087 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
945 | sv = decode_sv (&dec); |
1088 | sv = decode_sv (&dec); |
946 | |
1089 | |
|
|
1090 | if (!(offset_return || !sv)) |
|
|
1091 | { |
|
|
1092 | // check for trailing garbage |
|
|
1093 | decode_ws (&dec); |
|
|
1094 | |
|
|
1095 | if (*dec.cur) |
|
|
1096 | { |
|
|
1097 | dec.err = "garbage after JSON object"; |
|
|
1098 | SvREFCNT_dec (sv); |
|
|
1099 | sv = 0; |
|
|
1100 | } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | if (offset_return || !sv) |
|
|
1104 | { |
|
|
1105 | offset = dec.flags & F_UTF8 |
|
|
1106 | ? dec.cur - SvPVX (string) |
|
|
1107 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1108 | |
|
|
1109 | if (offset_return) |
|
|
1110 | *offset_return = offset; |
|
|
1111 | } |
|
|
1112 | |
947 | if (!sv) |
1113 | if (!sv) |
948 | { |
1114 | { |
949 | IV offset = dec.flags & F_UTF8 |
|
|
950 | ? dec.cur - SvPVX (string) |
|
|
951 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
952 | SV *uni = sv_newmortal (); |
1115 | SV *uni = sv_newmortal (); |
953 | |
1116 | |
954 | // horrible hack to silence warning inside pv_uni_display |
1117 | // horrible hack to silence warning inside pv_uni_display |
955 | COP cop = *PL_curcop; |
1118 | COP cop = *PL_curcop; |
956 | cop.cop_warnings = pWARN_NONE; |
1119 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
958 | SAVEVPTR (PL_curcop); |
1121 | SAVEVPTR (PL_curcop); |
959 | PL_curcop = &cop; |
1122 | PL_curcop = &cop; |
960 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1123 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
961 | LEAVE; |
1124 | LEAVE; |
962 | |
1125 | |
963 | croak ("%s, at character offset %d (%s)", |
1126 | croak ("%s, at character offset %d [\"%s\"]", |
964 | dec.err, |
1127 | dec.err, |
965 | (int)offset, |
1128 | (int)offset, |
966 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1129 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
967 | } |
1130 | } |
968 | |
1131 | |
… | |
… | |
982 | BOOT: |
1145 | BOOT: |
983 | { |
1146 | { |
984 | int i; |
1147 | int i; |
985 | |
1148 | |
986 | memset (decode_hexdigit, 0xff, 256); |
1149 | memset (decode_hexdigit, 0xff, 256); |
|
|
1150 | |
987 | for (i = 10; i--; ) |
1151 | for (i = 0; i < 256; ++i) |
988 | decode_hexdigit ['0' + i] = i; |
1152 | decode_hexdigit [i] = |
989 | |
1153 | i >= '0' && i <= '9' ? i - '0' |
990 | for (i = 7; i--; ) |
1154 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
991 | { |
1155 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
992 | decode_hexdigit ['a' + i] = 10 + i; |
1156 | : -1; |
993 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
994 | } |
|
|
995 | |
1157 | |
996 | json_stash = gv_stashpv ("JSON::XS", 1); |
1158 | json_stash = gv_stashpv ("JSON::XS", 1); |
997 | } |
1159 | } |
998 | |
1160 | |
999 | PROTOTYPES: DISABLE |
1161 | PROTOTYPES: DISABLE |
… | |
… | |
1005 | RETVAL |
1167 | RETVAL |
1006 | |
1168 | |
1007 | SV *ascii (SV *self, int enable = 1) |
1169 | SV *ascii (SV *self, int enable = 1) |
1008 | ALIAS: |
1170 | ALIAS: |
1009 | ascii = F_ASCII |
1171 | ascii = F_ASCII |
|
|
1172 | latin1 = F_LATIN1 |
1010 | utf8 = F_UTF8 |
1173 | utf8 = F_UTF8 |
1011 | indent = F_INDENT |
1174 | indent = F_INDENT |
1012 | canonical = F_CANONICAL |
1175 | canonical = F_CANONICAL |
1013 | space_before = F_SPACE_BEFORE |
1176 | space_before = F_SPACE_BEFORE |
1014 | space_after = F_SPACE_AFTER |
1177 | space_after = F_SPACE_AFTER |
… | |
… | |
1026 | RETVAL = newSVsv (self); |
1189 | RETVAL = newSVsv (self); |
1027 | } |
1190 | } |
1028 | OUTPUT: |
1191 | OUTPUT: |
1029 | RETVAL |
1192 | RETVAL |
1030 | |
1193 | |
|
|
1194 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1195 | CODE: |
|
|
1196 | { |
|
|
1197 | UV *uv = SvJSON (self); |
|
|
1198 | UV log2 = 0; |
|
|
1199 | |
|
|
1200 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1201 | |
|
|
1202 | while ((1UL << log2) < max_depth) |
|
|
1203 | ++log2; |
|
|
1204 | |
|
|
1205 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1206 | |
|
|
1207 | RETVAL = newSVsv (self); |
|
|
1208 | } |
|
|
1209 | OUTPUT: |
|
|
1210 | RETVAL |
|
|
1211 | |
1031 | void encode (SV *self, SV *scalar) |
1212 | void encode (SV *self, SV *scalar) |
1032 | PPCODE: |
1213 | PPCODE: |
1033 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1214 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1034 | |
1215 | |
1035 | void decode (SV *self, SV *jsonstr) |
1216 | void decode (SV *self, SV *jsonstr) |
1036 | PPCODE: |
1217 | PPCODE: |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1218 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1219 | |
|
|
1220 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1221 | PPCODE: |
|
|
1222 | { |
|
|
1223 | UV offset; |
|
|
1224 | EXTEND (SP, 2); |
|
|
1225 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1226 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1227 | } |
1038 | |
1228 | |
1039 | PROTOTYPES: ENABLE |
1229 | PROTOTYPES: ENABLE |
1040 | |
1230 | |
1041 | void to_json (SV *scalar) |
1231 | void to_json (SV *scalar) |
|
|
1232 | ALIAS: |
|
|
1233 | objToJson = 0 |
1042 | PPCODE: |
1234 | PPCODE: |
1043 | XPUSHs (encode_json (scalar, F_UTF8)); |
1235 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1044 | |
1236 | |
1045 | void from_json (SV *jsonstr) |
1237 | void from_json (SV *jsonstr) |
|
|
1238 | ALIAS: |
|
|
1239 | jsonToObj = 0 |
1046 | PPCODE: |
1240 | PPCODE: |
1047 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1241 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1048 | |
1242 | |