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