… | |
… | |
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 256 // 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 |
… | |
… | |
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
76 | SvPV_renew (sv, SvCUR (sv) + 1); |
54 | #endif |
77 | #endif |
55 | } |
78 | } |
56 | } |
79 | } |
57 | |
80 | |
|
|
81 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
82 | // case of an error. |
|
|
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
84 | // but use the very good perl function to parse anything else. |
|
|
85 | // note that we never call this function for a ascii codepoints |
|
|
86 | inline UV |
|
|
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
88 | { |
|
|
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
92 | { |
|
|
93 | *clen = 2; |
|
|
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
95 | } |
|
|
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
|
|
99 | return (UV)-1; |
|
|
100 | } |
|
|
101 | } |
|
|
102 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
59 | // encoder |
104 | // encoder |
60 | |
105 | |
61 | // structure used for encoding JSON |
106 | // structure used for encoding JSON |
62 | typedef struct |
107 | typedef struct |
63 | { |
108 | { |
64 | char *cur; // SvPVX (sv) + current output position |
109 | char *cur; // SvPVX (sv) + current output position |
65 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
66 | SV *sv; // result scalar |
111 | SV *sv; // result scalar |
67 | UV flags; // F_* |
112 | U32 flags; // F_* |
68 | int indent; // indentation level |
113 | U32 indent; // indentation level |
69 | int max_depth; // max. recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
70 | } enc_t; |
115 | } enc_t; |
71 | |
116 | |
72 | static void |
117 | inline void |
73 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
74 | { |
119 | { |
75 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
76 | { |
121 | { |
77 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
78 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
79 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
80 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
81 | } |
126 | } |
82 | } |
127 | } |
83 | |
128 | |
84 | static void |
129 | inline void |
85 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
86 | { |
131 | { |
87 | need (enc, 1); |
132 | need (enc, 1); |
88 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
89 | } |
134 | } |
… | |
… | |
97 | |
142 | |
98 | while (str < end) |
143 | while (str < end) |
99 | { |
144 | { |
100 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
101 | |
146 | |
102 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
103 | { |
148 | { |
104 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
105 | { |
150 | { |
106 | need (enc, len += 1); |
151 | need (enc, len += 1); |
107 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
108 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
109 | } |
154 | } |
110 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
111 | { |
156 | { |
112 | need (enc, len += 1); |
157 | need (enc, len += 1); |
113 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
114 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
115 | } |
160 | } |
… | |
… | |
133 | STRLEN clen; |
178 | STRLEN clen; |
134 | UV uch; |
179 | UV uch; |
135 | |
180 | |
136 | if (is_utf8) |
181 | if (is_utf8) |
137 | { |
182 | { |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
139 | if (clen == (STRLEN)-1) |
185 | if (clen == (STRLEN)-1) |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
141 | } |
187 | } |
142 | else |
188 | else |
143 | { |
189 | { |
… | |
… | |
146 | } |
192 | } |
147 | |
193 | |
148 | if (uch > 0x10FFFFUL) |
194 | if (uch > 0x10FFFFUL) |
149 | 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); |
150 | |
196 | |
151 | if (uch < 0x80 || enc->flags & F_ASCII) |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
152 | { |
198 | { |
153 | if (uch > 0xFFFFUL) |
199 | if (uch > 0xFFFFUL) |
154 | { |
200 | { |
155 | need (enc, len += 11); |
201 | need (enc, len += 11); |
156 | sprintf (enc->cur, "\\u%04x\\u%04x", |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
170 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
171 | } |
217 | } |
172 | |
218 | |
173 | str += clen; |
219 | str += clen; |
174 | } |
220 | } |
|
|
221 | else if (enc->flags & F_LATIN1) |
|
|
222 | { |
|
|
223 | *enc->cur++ = uch; |
|
|
224 | str += clen; |
|
|
225 | } |
175 | else if (is_utf8) |
226 | else if (is_utf8) |
176 | { |
227 | { |
177 | need (enc, len += clen); |
228 | need (enc, len += clen); |
178 | do |
229 | do |
179 | { |
230 | { |
… | |
… | |
181 | } |
232 | } |
182 | while (--clen); |
233 | while (--clen); |
183 | } |
234 | } |
184 | else |
235 | else |
185 | { |
236 | { |
186 | 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 |
187 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
238 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
188 | ++str; |
239 | ++str; |
189 | } |
240 | } |
190 | } |
241 | } |
191 | } |
242 | } |
… | |
… | |
193 | |
244 | |
194 | --len; |
245 | --len; |
195 | } |
246 | } |
196 | } |
247 | } |
197 | |
248 | |
198 | static void |
249 | inline void |
199 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
200 | { |
251 | { |
201 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
202 | { |
253 | { |
203 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
206 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
207 | enc->cur += spaces; |
258 | enc->cur += spaces; |
208 | } |
259 | } |
209 | } |
260 | } |
210 | |
261 | |
211 | static void |
262 | inline void |
212 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
213 | { |
264 | { |
214 | need (enc, 1); |
265 | need (enc, 1); |
215 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
216 | } |
267 | } |
217 | |
268 | |
218 | static void |
269 | inline void |
219 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
220 | { |
271 | { |
221 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
222 | { |
273 | { |
223 | need (enc, 1); |
274 | need (enc, 1); |
224 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
225 | } |
276 | } |
226 | } |
277 | } |
227 | |
278 | |
228 | static void |
279 | inline void |
229 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
230 | { |
281 | { |
231 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
232 | |
283 | |
233 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
240 | |
291 | |
241 | static void |
292 | static void |
242 | encode_av (enc_t *enc, AV *av) |
293 | encode_av (enc_t *enc, AV *av) |
243 | { |
294 | { |
244 | 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)"); |
245 | |
299 | |
246 | encode_ch (enc, '['); encode_nl (enc); |
300 | encode_ch (enc, '['); encode_nl (enc); |
247 | ++enc->indent; |
301 | ++enc->indent; |
248 | |
302 | |
249 | for (i = 0; i <= len; ++i) |
303 | for (i = 0; i <= len; ++i) |
… | |
… | |
316 | static void |
370 | static void |
317 | encode_hv (enc_t *enc, HV *hv) |
371 | encode_hv (enc_t *enc, HV *hv) |
318 | { |
372 | { |
319 | int count, i; |
373 | int count, i; |
320 | |
374 | |
|
|
375 | if (enc->indent >= enc->maxdepth) |
|
|
376 | croak ("data structure too deep (hit recursion limit)"); |
|
|
377 | |
321 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
378 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
322 | |
379 | |
323 | if ((count = hv_iterinit (hv))) |
380 | if ((count = hv_iterinit (hv))) |
324 | { |
381 | { |
325 | // for canonical output we have to sort by keys first |
382 | // for canonical output we have to sort by keys first |
326 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
327 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
328 | // that randomises hash orderings |
385 | // that randomises hash orderings |
329 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
330 | { |
387 | { |
331 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
332 | 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 |
333 | |
395 | |
334 | i = 0; |
396 | i = 0; |
335 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
336 | { |
398 | { |
337 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
372 | |
434 | |
373 | encode_nl (enc); |
435 | encode_nl (enc); |
374 | } |
436 | } |
375 | else |
437 | else |
376 | { |
438 | { |
377 | SV *sv; |
|
|
378 | HE *he = hv_iternext (hv); |
439 | HE *he = hv_iternext (hv); |
379 | |
440 | |
380 | for (;;) |
441 | for (;;) |
381 | { |
442 | { |
382 | encode_indent (enc); |
443 | encode_indent (enc); |
… | |
… | |
391 | encode_nl (enc); |
452 | encode_nl (enc); |
392 | } |
453 | } |
393 | } |
454 | } |
394 | |
455 | |
395 | --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)))); |
396 | } |
485 | } |
397 | |
486 | |
398 | static void |
487 | static void |
399 | encode_sv (enc_t *enc, SV *sv) |
488 | encode_sv (enc_t *enc, SV *sv) |
400 | { |
489 | { |
… | |
… | |
414 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
415 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
416 | } |
505 | } |
417 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
418 | { |
507 | { |
419 | need (enc, 64); |
508 | // we assume we can always read an IV as a UV |
|
|
509 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
510 | { |
|
|
511 | need (enc, sizeof (UV) * 3); |
420 | enc->cur += |
512 | enc->cur += |
421 | SvIsUV(sv) |
513 | SvIsUV(sv) |
422 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
423 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
516 | } |
|
|
517 | else |
|
|
518 | { |
|
|
519 | // optimise the "small number case" |
|
|
520 | // code will likely be branchless and use only a single multiplication |
|
|
521 | I32 i = SvIV (sv); |
|
|
522 | U32 u; |
|
|
523 | |
|
|
524 | need (enc, 6); |
|
|
525 | |
|
|
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
527 | u = i < 0 ? -i : i; |
|
|
528 | |
|
|
529 | // convert to 4.28 fixed-point representation |
|
|
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
531 | |
|
|
532 | char digit, nz = 0; |
|
|
533 | |
|
|
534 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
535 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
536 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
537 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
538 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; |
|
|
539 | } |
424 | } |
540 | } |
425 | else if (SvROK (sv)) |
541 | else if (SvROK (sv)) |
426 | { |
542 | encode_rv (enc, SvRV (sv)); |
427 | SV *rv = SvRV (sv); |
|
|
428 | |
|
|
429 | if (enc->indent >= enc->max_depth) |
|
|
430 | croak ("data structure too deep (hit recursion limit)"); |
|
|
431 | |
|
|
432 | switch (SvTYPE (rv)) |
|
|
433 | { |
|
|
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
435 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
436 | |
|
|
437 | default: |
|
|
438 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
439 | SvPV_nolen (sv)); |
|
|
440 | } |
|
|
441 | } |
|
|
442 | else if (!SvOK (sv)) |
543 | else if (!SvOK (sv)) |
443 | encode_str (enc, "null", 4, 0); |
544 | encode_str (enc, "null", 4, 0); |
444 | else |
545 | else |
445 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
546 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
446 | SvPV_nolen (sv), SvFLAGS (sv)); |
547 | SvPV_nolen (sv), SvFLAGS (sv)); |
447 | } |
548 | } |
448 | |
549 | |
449 | static SV * |
550 | static SV * |
450 | encode_json (SV *scalar, UV flags) |
551 | encode_json (SV *scalar, U32 flags) |
451 | { |
552 | { |
|
|
553 | enc_t enc; |
|
|
554 | |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
555 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
556 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
454 | |
557 | |
455 | enc_t enc; |
|
|
456 | enc.flags = flags; |
558 | enc.flags = flags; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
559 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
560 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
561 | enc.end = SvEND (enc.sv); |
460 | enc.indent = 0; |
562 | enc.indent = 0; |
461 | enc.max_depth = 0x7fffffffUL; |
563 | enc.maxdepth = DEC_DEPTH (flags); |
462 | |
564 | |
463 | SvPOK_only (enc.sv); |
565 | SvPOK_only (enc.sv); |
464 | encode_sv (&enc, scalar); |
566 | encode_sv (&enc, scalar); |
465 | |
567 | |
|
|
568 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
569 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
570 | |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
571 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
467 | SvUTF8_on (enc.sv); |
572 | SvUTF8_on (enc.sv); |
468 | |
|
|
469 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
470 | |
573 | |
471 | if (enc.flags & F_SHRINK) |
574 | if (enc.flags & F_SHRINK) |
472 | shrink (enc.sv); |
575 | shrink (enc.sv); |
473 | |
576 | |
474 | return enc.sv; |
577 | return enc.sv; |
… | |
… | |
481 | typedef struct |
584 | typedef struct |
482 | { |
585 | { |
483 | char *cur; // current parser pointer |
586 | char *cur; // current parser pointer |
484 | char *end; // end of input string |
587 | char *end; // end of input string |
485 | const char *err; // parse error, if != 0 |
588 | const char *err; // parse error, if != 0 |
486 | UV flags; // F_* |
589 | U32 flags; // F_* |
|
|
590 | U32 depth; // recursion depth |
|
|
591 | U32 maxdepth; // recursion depth limit |
487 | } dec_t; |
592 | } dec_t; |
488 | |
593 | |
489 | static void |
594 | inline void |
490 | decode_ws (dec_t *dec) |
595 | decode_ws (dec_t *dec) |
491 | { |
596 | { |
492 | for (;;) |
597 | for (;;) |
493 | { |
598 | { |
494 | char ch = *dec->cur; |
599 | char ch = *dec->cur; |
… | |
… | |
500 | ++dec->cur; |
605 | ++dec->cur; |
501 | } |
606 | } |
502 | } |
607 | } |
503 | |
608 | |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
609 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
610 | |
505 | #define EXPECT_CH(ch) SB \ |
611 | #define EXPECT_CH(ch) SB \ |
506 | if (*dec->cur != ch) \ |
612 | if (*dec->cur != ch) \ |
507 | ERR (# ch " expected"); \ |
613 | ERR (# ch " expected"); \ |
508 | ++dec->cur; \ |
614 | ++dec->cur; \ |
509 | SE |
615 | SE |
510 | |
616 | |
|
|
617 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
618 | #define DEC_DEC_DEPTH --dec->depth |
|
|
619 | |
511 | static SV *decode_sv (dec_t *dec); |
620 | static SV *decode_sv (dec_t *dec); |
512 | |
621 | |
513 | static signed char decode_hexdigit[256]; |
622 | static signed char decode_hexdigit[256]; |
514 | |
623 | |
515 | static UV |
624 | static UV |
516 | decode_4hex (dec_t *dec) |
625 | decode_4hex (dec_t *dec) |
517 | { |
626 | { |
518 | signed char d1, d2, d3, d4; |
627 | signed char d1, d2, d3, d4; |
519 | unsigned char *cur = (unsigned char *)dec->cur; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
520 | |
629 | |
521 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
522 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
523 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
524 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
525 | |
634 | |
526 | dec->cur += 4; |
635 | dec->cur += 4; |
527 | |
636 | |
528 | return ((UV)d1) << 12 |
637 | return ((UV)d1) << 12 |
529 | | ((UV)d2) << 8 |
638 | | ((UV)d2) << 8 |
… | |
… | |
537 | static SV * |
646 | static SV * |
538 | decode_str (dec_t *dec) |
647 | decode_str (dec_t *dec) |
539 | { |
648 | { |
540 | SV *sv = 0; |
649 | SV *sv = 0; |
541 | int utf8 = 0; |
650 | int utf8 = 0; |
|
|
651 | char *dec_cur = dec->cur; |
542 | |
652 | |
543 | do |
653 | do |
544 | { |
654 | { |
545 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
655 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
546 | char *cur = buf; |
656 | char *cur = buf; |
547 | |
657 | |
548 | do |
658 | do |
549 | { |
659 | { |
550 | unsigned char ch = *(unsigned char *)dec->cur++; |
660 | unsigned char ch = *(unsigned char *)dec_cur++; |
551 | |
661 | |
552 | if (ch == '"') |
662 | if (expect_false (ch == '"')) |
553 | { |
663 | { |
554 | --dec->cur; |
664 | --dec_cur; |
555 | break; |
665 | break; |
556 | } |
666 | } |
557 | else if (ch == '\\') |
667 | else if (expect_false (ch == '\\')) |
558 | { |
668 | { |
559 | switch (*dec->cur) |
669 | switch (*dec_cur) |
560 | { |
670 | { |
561 | case '\\': |
671 | case '\\': |
562 | case '/': |
672 | case '/': |
563 | case '"': *cur++ = *dec->cur++; break; |
673 | case '"': *cur++ = *dec_cur++; break; |
564 | |
674 | |
565 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
675 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
566 | case 't': ++dec->cur; *cur++ = '\011'; break; |
676 | case 't': ++dec_cur; *cur++ = '\011'; break; |
567 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
677 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
568 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
678 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
569 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
679 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
570 | |
680 | |
571 | case 'u': |
681 | case 'u': |
572 | { |
682 | { |
573 | UV lo, hi; |
683 | UV lo, hi; |
574 | ++dec->cur; |
684 | ++dec_cur; |
575 | |
685 | |
|
|
686 | dec->cur = dec_cur; |
576 | hi = decode_4hex (dec); |
687 | hi = decode_4hex (dec); |
|
|
688 | dec_cur = dec->cur; |
577 | if (hi == (UV)-1) |
689 | if (hi == (UV)-1) |
578 | goto fail; |
690 | goto fail; |
579 | |
691 | |
580 | // possibly a surrogate pair |
692 | // possibly a surrogate pair |
581 | if (hi >= 0xd800) |
693 | if (hi >= 0xd800) |
582 | if (hi < 0xdc00) |
694 | if (hi < 0xdc00) |
583 | { |
695 | { |
584 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
696 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
585 | ERR ("missing low surrogate character in surrogate pair"); |
697 | ERR ("missing low surrogate character in surrogate pair"); |
586 | |
698 | |
587 | dec->cur += 2; |
699 | dec_cur += 2; |
588 | |
700 | |
|
|
701 | dec->cur = dec_cur; |
589 | lo = decode_4hex (dec); |
702 | lo = decode_4hex (dec); |
|
|
703 | dec_cur = dec->cur; |
590 | if (lo == (UV)-1) |
704 | if (lo == (UV)-1) |
591 | goto fail; |
705 | goto fail; |
592 | |
706 | |
593 | if (lo < 0xdc00 || lo >= 0xe000) |
707 | if (lo < 0xdc00 || lo >= 0xe000) |
594 | ERR ("surrogate pair expected"); |
708 | ERR ("surrogate pair expected"); |
… | |
… | |
608 | *cur++ = hi; |
722 | *cur++ = hi; |
609 | } |
723 | } |
610 | break; |
724 | break; |
611 | |
725 | |
612 | default: |
726 | default: |
613 | --dec->cur; |
727 | --dec_cur; |
614 | ERR ("illegal backslash escape sequence in string"); |
728 | ERR ("illegal backslash escape sequence in string"); |
615 | } |
729 | } |
616 | } |
730 | } |
617 | else if (ch >= 0x20 && ch <= 0x7f) |
731 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
618 | *cur++ = ch; |
732 | *cur++ = ch; |
619 | else if (ch >= 0x80) |
733 | else if (ch >= 0x80) |
620 | { |
734 | { |
621 | --dec->cur; |
|
|
622 | |
|
|
623 | STRLEN clen; |
735 | STRLEN clen; |
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
736 | UV uch; |
|
|
737 | |
|
|
738 | --dec_cur; |
|
|
739 | |
|
|
740 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
625 | if (clen == (STRLEN)-1) |
741 | if (clen == (STRLEN)-1) |
626 | ERR ("malformed UTF-8 character in JSON string"); |
742 | ERR ("malformed UTF-8 character in JSON string"); |
627 | |
743 | |
628 | do |
744 | do |
629 | { |
|
|
630 | *cur++ = *dec->cur++; |
745 | *cur++ = *dec_cur++; |
631 | } |
|
|
632 | while (--clen); |
746 | while (--clen); |
633 | |
747 | |
634 | utf8 = 1; |
748 | utf8 = 1; |
635 | } |
749 | } |
636 | else if (!ch) |
|
|
637 | ERR ("unexpected end of string while parsing json string"); |
|
|
638 | else |
750 | else |
|
|
751 | { |
|
|
752 | --dec_cur; |
|
|
753 | |
|
|
754 | if (!ch) |
|
|
755 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
756 | else |
639 | ERR ("invalid character encountered"); |
757 | ERR ("invalid character encountered while parsing JSON string"); |
640 | |
758 | } |
641 | } |
759 | } |
642 | while (cur < buf + SHORT_STRING_LEN); |
760 | while (cur < buf + SHORT_STRING_LEN); |
643 | |
761 | |
|
|
762 | { |
644 | STRLEN len = cur - buf; |
763 | STRLEN len = cur - buf; |
645 | |
764 | |
646 | if (sv) |
765 | if (sv) |
647 | { |
766 | { |
648 | SvGROW (sv, SvCUR (sv) + len + 1); |
767 | SvGROW (sv, SvCUR (sv) + len + 1); |
649 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
768 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
650 | SvCUR_set (sv, SvCUR (sv) + len); |
769 | SvCUR_set (sv, SvCUR (sv) + len); |
651 | } |
770 | } |
652 | else |
771 | else |
653 | sv = newSVpvn (buf, len); |
772 | sv = newSVpvn (buf, len); |
654 | } |
773 | } |
|
|
774 | } |
655 | while (*dec->cur != '"'); |
775 | while (*dec_cur != '"'); |
656 | |
776 | |
657 | ++dec->cur; |
777 | ++dec_cur; |
658 | |
778 | |
659 | if (sv) |
779 | if (sv) |
660 | { |
780 | { |
661 | SvPOK_only (sv); |
781 | SvPOK_only (sv); |
662 | *SvEND (sv) = 0; |
782 | *SvEND (sv) = 0; |
… | |
… | |
665 | SvUTF8_on (sv); |
785 | SvUTF8_on (sv); |
666 | } |
786 | } |
667 | else |
787 | else |
668 | sv = newSVpvn ("", 0); |
788 | sv = newSVpvn ("", 0); |
669 | |
789 | |
|
|
790 | dec->cur = dec_cur; |
670 | return sv; |
791 | return sv; |
671 | |
792 | |
672 | fail: |
793 | fail: |
|
|
794 | dec->cur = dec_cur; |
673 | return 0; |
795 | return 0; |
674 | } |
796 | } |
675 | |
797 | |
676 | static SV * |
798 | static SV * |
677 | decode_num (dec_t *dec) |
799 | decode_num (dec_t *dec) |
… | |
… | |
735 | is_nv = 1; |
857 | is_nv = 1; |
736 | } |
858 | } |
737 | |
859 | |
738 | if (!is_nv) |
860 | if (!is_nv) |
739 | { |
861 | { |
740 | UV uv; |
862 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
741 | int numtype = grok_number (start, dec->cur - start, &uv); |
863 | if (*start == '-') |
742 | if (numtype & IS_NUMBER_IN_UV) |
864 | switch (dec->cur - start) |
743 | if (numtype & IS_NUMBER_NEG) |
|
|
744 | { |
865 | { |
745 | if (uv < (UV)IV_MIN) |
866 | case 2: return newSViv (-( start [1] - '0' )); |
746 | return newSViv (-(IV)uv); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
868 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
869 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
747 | } |
870 | } |
|
|
871 | else |
|
|
872 | switch (dec->cur - start) |
|
|
873 | { |
|
|
874 | case 1: return newSViv ( start [0] - '0' ); |
|
|
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
876 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
877 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
878 | } |
|
|
879 | |
|
|
880 | { |
|
|
881 | UV uv; |
|
|
882 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
883 | if (numtype & IS_NUMBER_IN_UV) |
|
|
884 | if (numtype & IS_NUMBER_NEG) |
|
|
885 | { |
|
|
886 | if (uv < (UV)IV_MIN) |
|
|
887 | return newSViv (-(IV)uv); |
|
|
888 | } |
748 | else |
889 | else |
749 | return newSVuv (uv); |
890 | return newSVuv (uv); |
|
|
891 | } |
750 | } |
892 | } |
751 | |
893 | |
752 | return newSVnv (Atof (start)); |
894 | return newSVnv (Atof (start)); |
753 | |
895 | |
754 | fail: |
896 | fail: |
… | |
… | |
758 | static SV * |
900 | static SV * |
759 | decode_av (dec_t *dec) |
901 | decode_av (dec_t *dec) |
760 | { |
902 | { |
761 | AV *av = newAV (); |
903 | AV *av = newAV (); |
762 | |
904 | |
|
|
905 | DEC_INC_DEPTH; |
763 | decode_ws (dec); |
906 | decode_ws (dec); |
|
|
907 | |
764 | if (*dec->cur == ']') |
908 | if (*dec->cur == ']') |
765 | ++dec->cur; |
909 | ++dec->cur; |
766 | else |
910 | else |
767 | for (;;) |
911 | for (;;) |
768 | { |
912 | { |
… | |
… | |
786 | ERR (", or ] expected while parsing array"); |
930 | ERR (", or ] expected while parsing array"); |
787 | |
931 | |
788 | ++dec->cur; |
932 | ++dec->cur; |
789 | } |
933 | } |
790 | |
934 | |
|
|
935 | DEC_DEC_DEPTH; |
791 | return newRV_noinc ((SV *)av); |
936 | return newRV_noinc ((SV *)av); |
792 | |
937 | |
793 | fail: |
938 | fail: |
794 | SvREFCNT_dec (av); |
939 | SvREFCNT_dec (av); |
|
|
940 | DEC_DEC_DEPTH; |
795 | return 0; |
941 | return 0; |
796 | } |
942 | } |
797 | |
943 | |
798 | static SV * |
944 | static SV * |
799 | decode_hv (dec_t *dec) |
945 | decode_hv (dec_t *dec) |
800 | { |
946 | { |
801 | HV *hv = newHV (); |
947 | HV *hv = newHV (); |
802 | |
948 | |
|
|
949 | DEC_INC_DEPTH; |
803 | decode_ws (dec); |
950 | decode_ws (dec); |
|
|
951 | |
804 | if (*dec->cur == '}') |
952 | if (*dec->cur == '}') |
805 | ++dec->cur; |
953 | ++dec->cur; |
806 | else |
954 | else |
807 | for (;;) |
955 | for (;;) |
808 | { |
956 | { |
… | |
… | |
821 | { |
969 | { |
822 | SvREFCNT_dec (key); |
970 | SvREFCNT_dec (key); |
823 | goto fail; |
971 | goto fail; |
824 | } |
972 | } |
825 | |
973 | |
826 | //TODO: optimise |
|
|
827 | hv_store_ent (hv, key, value, 0); |
974 | hv_store_ent (hv, key, value, 0); |
|
|
975 | SvREFCNT_dec (key); |
828 | |
976 | |
829 | decode_ws (dec); |
977 | decode_ws (dec); |
830 | |
978 | |
831 | if (*dec->cur == '}') |
979 | if (*dec->cur == '}') |
832 | { |
980 | { |
… | |
… | |
838 | ERR (", or } expected while parsing object/hash"); |
986 | ERR (", or } expected while parsing object/hash"); |
839 | |
987 | |
840 | ++dec->cur; |
988 | ++dec->cur; |
841 | } |
989 | } |
842 | |
990 | |
|
|
991 | DEC_DEC_DEPTH; |
843 | return newRV_noinc ((SV *)hv); |
992 | return newRV_noinc ((SV *)hv); |
844 | |
993 | |
845 | fail: |
994 | fail: |
846 | SvREFCNT_dec (hv); |
995 | SvREFCNT_dec (hv); |
|
|
996 | DEC_DEC_DEPTH; |
847 | return 0; |
997 | return 0; |
848 | } |
998 | } |
849 | |
999 | |
850 | static SV * |
1000 | static SV * |
851 | decode_sv (dec_t *dec) |
1001 | decode_sv (dec_t *dec) |
… | |
… | |
894 | ERR ("'null' expected"); |
1044 | ERR ("'null' expected"); |
895 | |
1045 | |
896 | break; |
1046 | break; |
897 | |
1047 | |
898 | default: |
1048 | default: |
899 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1049 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
900 | break; |
1050 | break; |
901 | } |
1051 | } |
902 | |
1052 | |
903 | fail: |
1053 | fail: |
904 | return 0; |
1054 | return 0; |
905 | } |
1055 | } |
906 | |
1056 | |
907 | static SV * |
1057 | static SV * |
908 | decode_json (SV *string, UV flags) |
1058 | decode_json (SV *string, U32 flags, UV *offset_return) |
909 | { |
1059 | { |
|
|
1060 | dec_t dec; |
|
|
1061 | UV offset; |
910 | SV *sv; |
1062 | SV *sv; |
|
|
1063 | |
|
|
1064 | SvGETMAGIC (string); |
|
|
1065 | SvUPGRADE (string, SVt_PV); |
911 | |
1066 | |
912 | if (flags & F_UTF8) |
1067 | if (flags & F_UTF8) |
913 | sv_utf8_downgrade (string, 0); |
1068 | sv_utf8_downgrade (string, 0); |
914 | else |
1069 | else |
915 | sv_utf8_upgrade (string); |
1070 | sv_utf8_upgrade (string); |
916 | |
1071 | |
917 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1072 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
918 | |
1073 | |
919 | dec_t dec; |
|
|
920 | dec.flags = flags; |
1074 | dec.flags = flags; |
921 | dec.cur = SvPVX (string); |
1075 | dec.cur = SvPVX (string); |
922 | dec.end = SvEND (string); |
1076 | dec.end = SvEND (string); |
923 | dec.err = 0; |
1077 | dec.err = 0; |
|
|
1078 | dec.depth = 0; |
|
|
1079 | dec.maxdepth = DEC_DEPTH (dec.flags); |
924 | |
1080 | |
|
|
1081 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
925 | sv = decode_sv (&dec); |
1082 | sv = decode_sv (&dec); |
926 | |
1083 | |
|
|
1084 | if (!(offset_return || !sv)) |
|
|
1085 | { |
|
|
1086 | // check for trailing garbage |
|
|
1087 | decode_ws (&dec); |
|
|
1088 | |
|
|
1089 | if (*dec.cur) |
|
|
1090 | { |
|
|
1091 | dec.err = "garbage after JSON object"; |
|
|
1092 | SvREFCNT_dec (sv); |
|
|
1093 | sv = 0; |
|
|
1094 | } |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | if (offset_return || !sv) |
|
|
1098 | { |
|
|
1099 | offset = dec.flags & F_UTF8 |
|
|
1100 | ? dec.cur - SvPVX (string) |
|
|
1101 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1102 | |
|
|
1103 | if (offset_return) |
|
|
1104 | *offset_return = offset; |
|
|
1105 | } |
|
|
1106 | |
927 | if (!sv) |
1107 | if (!sv) |
928 | { |
1108 | { |
929 | IV offset = dec.flags & F_UTF8 |
|
|
930 | ? dec.cur - SvPVX (string) |
|
|
931 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
932 | SV *uni = sv_newmortal (); |
1109 | SV *uni = sv_newmortal (); |
933 | |
1110 | |
934 | // horrible hack to silence warning inside pv_uni_display |
1111 | // horrible hack to silence warning inside pv_uni_display |
935 | COP cop = *PL_curcop; |
1112 | COP cop = *PL_curcop; |
936 | cop.cop_warnings = pWARN_NONE; |
1113 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
938 | SAVEVPTR (PL_curcop); |
1115 | SAVEVPTR (PL_curcop); |
939 | PL_curcop = &cop; |
1116 | PL_curcop = &cop; |
940 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1117 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
941 | LEAVE; |
1118 | LEAVE; |
942 | |
1119 | |
943 | croak ("%s, at character offset %d (%s)", |
1120 | croak ("%s, at character offset %d [\"%s\"]", |
944 | dec.err, |
1121 | dec.err, |
945 | (int)offset, |
1122 | (int)offset, |
946 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1123 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
947 | } |
1124 | } |
948 | |
1125 | |
… | |
… | |
962 | BOOT: |
1139 | BOOT: |
963 | { |
1140 | { |
964 | int i; |
1141 | int i; |
965 | |
1142 | |
966 | memset (decode_hexdigit, 0xff, 256); |
1143 | memset (decode_hexdigit, 0xff, 256); |
|
|
1144 | |
967 | for (i = 10; i--; ) |
1145 | for (i = 0; i < 256; ++i) |
968 | decode_hexdigit ['0' + i] = i; |
1146 | decode_hexdigit [i] = |
969 | |
1147 | i >= '0' && i <= '9' ? i - '0' |
970 | for (i = 7; i--; ) |
1148 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
971 | { |
1149 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
972 | decode_hexdigit ['a' + i] = 10 + i; |
1150 | : -1; |
973 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
974 | } |
|
|
975 | |
1151 | |
976 | json_stash = gv_stashpv ("JSON::XS", 1); |
1152 | json_stash = gv_stashpv ("JSON::XS", 1); |
977 | } |
1153 | } |
978 | |
1154 | |
979 | PROTOTYPES: DISABLE |
1155 | PROTOTYPES: DISABLE |
… | |
… | |
985 | RETVAL |
1161 | RETVAL |
986 | |
1162 | |
987 | SV *ascii (SV *self, int enable = 1) |
1163 | SV *ascii (SV *self, int enable = 1) |
988 | ALIAS: |
1164 | ALIAS: |
989 | ascii = F_ASCII |
1165 | ascii = F_ASCII |
|
|
1166 | latin1 = F_LATIN1 |
990 | utf8 = F_UTF8 |
1167 | utf8 = F_UTF8 |
991 | indent = F_INDENT |
1168 | indent = F_INDENT |
992 | canonical = F_CANONICAL |
1169 | canonical = F_CANONICAL |
993 | space_before = F_SPACE_BEFORE |
1170 | space_before = F_SPACE_BEFORE |
994 | space_after = F_SPACE_AFTER |
1171 | space_after = F_SPACE_AFTER |
… | |
… | |
1006 | RETVAL = newSVsv (self); |
1183 | RETVAL = newSVsv (self); |
1007 | } |
1184 | } |
1008 | OUTPUT: |
1185 | OUTPUT: |
1009 | RETVAL |
1186 | RETVAL |
1010 | |
1187 | |
|
|
1188 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1189 | CODE: |
|
|
1190 | { |
|
|
1191 | UV *uv = SvJSON (self); |
|
|
1192 | UV log2 = 0; |
|
|
1193 | |
|
|
1194 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1195 | |
|
|
1196 | while ((1UL << log2) < max_depth) |
|
|
1197 | ++log2; |
|
|
1198 | |
|
|
1199 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1200 | |
|
|
1201 | RETVAL = newSVsv (self); |
|
|
1202 | } |
|
|
1203 | OUTPUT: |
|
|
1204 | RETVAL |
|
|
1205 | |
1011 | void encode (SV *self, SV *scalar) |
1206 | void encode (SV *self, SV *scalar) |
1012 | PPCODE: |
1207 | PPCODE: |
1013 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1208 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1014 | |
1209 | |
1015 | void decode (SV *self, SV *jsonstr) |
1210 | void decode (SV *self, SV *jsonstr) |
1016 | PPCODE: |
1211 | PPCODE: |
1017 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1212 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1213 | |
|
|
1214 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1215 | PPCODE: |
|
|
1216 | { |
|
|
1217 | UV offset; |
|
|
1218 | EXTEND (SP, 2); |
|
|
1219 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1220 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1221 | } |
1018 | |
1222 | |
1019 | PROTOTYPES: ENABLE |
1223 | PROTOTYPES: ENABLE |
1020 | |
1224 | |
1021 | void to_json (SV *scalar) |
1225 | void to_json (SV *scalar) |
|
|
1226 | ALIAS: |
|
|
1227 | objToJson = 0 |
1022 | PPCODE: |
1228 | PPCODE: |
1023 | XPUSHs (encode_json (scalar, F_UTF8)); |
1229 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1024 | |
1230 | |
1025 | void from_json (SV *jsonstr) |
1231 | void from_json (SV *jsonstr) |
|
|
1232 | ALIAS: |
|
|
1233 | jsonToObj = 0 |
1026 | PPCODE: |
1234 | PPCODE: |
1027 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1235 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1028 | |
1236 | |