… | |
… | |
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 |
17 | |
25 | |
18 | // F_SKIPINVALID? |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
19 | // F_EXECCODEREF? |
27 | |
20 | // F_SELFCONVERT? <=> { &__class__ => } |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
21 | |
30 | |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
23 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
24 | |
33 | |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
26 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
27 | |
36 | |
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
29 | #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 |
30 | |
38 | |
31 | #define SB do { |
39 | #define SB do { |
32 | #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) |
33 | |
52 | |
34 | static HV *json_stash; // JSON::XS:: |
53 | static HV *json_stash; // JSON::XS:: |
35 | |
54 | |
36 | ///////////////////////////////////////////////////////////////////////////// |
55 | ///////////////////////////////////////////////////////////////////////////// |
37 | // utility functions |
56 | // utility functions |
… | |
… | |
62 | // 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 |
63 | // case of an error. |
82 | // case of an error. |
64 | // we special-case "safe" characters from U+80 .. U+7FF, |
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
65 | // but use the very good perl function to parse anything else. |
84 | // but use the very good perl function to parse anything else. |
66 | // note that we never call this function for a ascii codepoints |
85 | // note that we never call this function for a ascii codepoints |
67 | static UV |
86 | inline UV |
68 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
69 | { |
88 | { |
70 | if (s[0] > 0xdf || s[0] < 0xc2) |
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
71 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
72 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
73 | { |
92 | { |
74 | *clen = 2; |
93 | *clen = 2; |
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
76 | } |
95 | } |
77 | else |
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
78 | return (UV)-1; |
99 | return (UV)-1; |
|
|
100 | } |
79 | } |
101 | } |
80 | |
102 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
103 | ///////////////////////////////////////////////////////////////////////////// |
82 | // encoder |
104 | // encoder |
83 | |
105 | |
… | |
… | |
85 | typedef struct |
107 | typedef struct |
86 | { |
108 | { |
87 | char *cur; // SvPVX (sv) + current output position |
109 | char *cur; // SvPVX (sv) + current output position |
88 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
89 | SV *sv; // result scalar |
111 | SV *sv; // result scalar |
90 | UV flags; // F_* |
112 | U32 flags; // F_* |
91 | int indent; // indentation level |
113 | U32 indent; // indentation level |
92 | int max_depth; // max. recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
93 | } enc_t; |
115 | } enc_t; |
94 | |
116 | |
95 | static void |
117 | inline void |
96 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
97 | { |
119 | { |
98 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
99 | { |
121 | { |
100 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
101 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
102 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
103 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
104 | } |
126 | } |
105 | } |
127 | } |
106 | |
128 | |
107 | static void |
129 | inline void |
108 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
109 | { |
131 | { |
110 | need (enc, 1); |
132 | need (enc, 1); |
111 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
112 | } |
134 | } |
… | |
… | |
120 | |
142 | |
121 | while (str < end) |
143 | while (str < end) |
122 | { |
144 | { |
123 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
124 | |
146 | |
125 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
126 | { |
148 | { |
127 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
128 | { |
150 | { |
129 | need (enc, len += 1); |
151 | need (enc, len += 1); |
130 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
131 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
132 | } |
154 | } |
133 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
134 | { |
156 | { |
135 | need (enc, len += 1); |
157 | need (enc, len += 1); |
136 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
137 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
138 | } |
160 | } |
… | |
… | |
156 | STRLEN clen; |
178 | STRLEN clen; |
157 | UV uch; |
179 | UV uch; |
158 | |
180 | |
159 | if (is_utf8) |
181 | if (is_utf8) |
160 | { |
182 | { |
161 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
162 | uch = decode_utf8 (str, end - str, &clen); |
183 | uch = decode_utf8 (str, end - str, &clen); |
163 | if (clen == (STRLEN)-1) |
184 | if (clen == (STRLEN)-1) |
164 | 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); |
165 | } |
186 | } |
166 | else |
187 | else |
… | |
… | |
170 | } |
191 | } |
171 | |
192 | |
172 | if (uch > 0x10FFFFUL) |
193 | if (uch > 0x10FFFFUL) |
173 | 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); |
174 | |
195 | |
175 | if (uch < 0x80 || enc->flags & F_ASCII) |
196 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
176 | { |
197 | { |
177 | if (uch > 0xFFFFUL) |
198 | if (uch > 0xFFFFUL) |
178 | { |
199 | { |
179 | need (enc, len += 11); |
200 | need (enc, len += 11); |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
201 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
194 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
215 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
195 | } |
216 | } |
196 | |
217 | |
197 | str += clen; |
218 | str += clen; |
198 | } |
219 | } |
|
|
220 | else if (enc->flags & F_LATIN1) |
|
|
221 | { |
|
|
222 | *enc->cur++ = uch; |
|
|
223 | str += clen; |
|
|
224 | } |
199 | else if (is_utf8) |
225 | else if (is_utf8) |
200 | { |
226 | { |
201 | need (enc, len += clen); |
227 | need (enc, len += clen); |
202 | do |
228 | do |
203 | { |
229 | { |
… | |
… | |
205 | } |
231 | } |
206 | while (--clen); |
232 | while (--clen); |
207 | } |
233 | } |
208 | else |
234 | else |
209 | { |
235 | { |
210 | 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 |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
237 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
212 | ++str; |
238 | ++str; |
213 | } |
239 | } |
214 | } |
240 | } |
215 | } |
241 | } |
… | |
… | |
217 | |
243 | |
218 | --len; |
244 | --len; |
219 | } |
245 | } |
220 | } |
246 | } |
221 | |
247 | |
222 | static void |
248 | inline void |
223 | encode_indent (enc_t *enc) |
249 | encode_indent (enc_t *enc) |
224 | { |
250 | { |
225 | if (enc->flags & F_INDENT) |
251 | if (enc->flags & F_INDENT) |
226 | { |
252 | { |
227 | int spaces = enc->indent * INDENT_STEP; |
253 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
230 | memset (enc->cur, ' ', spaces); |
256 | memset (enc->cur, ' ', spaces); |
231 | enc->cur += spaces; |
257 | enc->cur += spaces; |
232 | } |
258 | } |
233 | } |
259 | } |
234 | |
260 | |
235 | static void |
261 | inline void |
236 | encode_space (enc_t *enc) |
262 | encode_space (enc_t *enc) |
237 | { |
263 | { |
238 | need (enc, 1); |
264 | need (enc, 1); |
239 | encode_ch (enc, ' '); |
265 | encode_ch (enc, ' '); |
240 | } |
266 | } |
241 | |
267 | |
242 | static void |
268 | inline void |
243 | encode_nl (enc_t *enc) |
269 | encode_nl (enc_t *enc) |
244 | { |
270 | { |
245 | if (enc->flags & F_INDENT) |
271 | if (enc->flags & F_INDENT) |
246 | { |
272 | { |
247 | need (enc, 1); |
273 | need (enc, 1); |
248 | encode_ch (enc, '\n'); |
274 | encode_ch (enc, '\n'); |
249 | } |
275 | } |
250 | } |
276 | } |
251 | |
277 | |
252 | static void |
278 | inline void |
253 | encode_comma (enc_t *enc) |
279 | encode_comma (enc_t *enc) |
254 | { |
280 | { |
255 | encode_ch (enc, ','); |
281 | encode_ch (enc, ','); |
256 | |
282 | |
257 | if (enc->flags & F_INDENT) |
283 | if (enc->flags & F_INDENT) |
… | |
… | |
264 | |
290 | |
265 | static void |
291 | static void |
266 | encode_av (enc_t *enc, AV *av) |
292 | encode_av (enc_t *enc, AV *av) |
267 | { |
293 | { |
268 | 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)"); |
269 | |
298 | |
270 | encode_ch (enc, '['); encode_nl (enc); |
299 | encode_ch (enc, '['); encode_nl (enc); |
271 | ++enc->indent; |
300 | ++enc->indent; |
272 | |
301 | |
273 | for (i = 0; i <= len; ++i) |
302 | for (i = 0; i <= len; ++i) |
… | |
… | |
340 | static void |
369 | static void |
341 | encode_hv (enc_t *enc, HV *hv) |
370 | encode_hv (enc_t *enc, HV *hv) |
342 | { |
371 | { |
343 | int count, i; |
372 | int count, i; |
344 | |
373 | |
|
|
374 | if (enc->indent >= enc->maxdepth) |
|
|
375 | croak ("data structure too deep (hit recursion limit)"); |
|
|
376 | |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
377 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
346 | |
378 | |
347 | if ((count = hv_iterinit (hv))) |
379 | if ((count = hv_iterinit (hv))) |
348 | { |
380 | { |
349 | // for canonical output we have to sort by keys first |
381 | // for canonical output we have to sort by keys first |
350 | // actually, this is mostly due to the stupid so-called |
382 | // actually, this is mostly due to the stupid so-called |
351 | // security workaround added somewhere in 5.8.x. |
383 | // security workaround added somewhere in 5.8.x. |
352 | // that randomises hash orderings |
384 | // that randomises hash orderings |
353 | if (enc->flags & F_CANONICAL) |
385 | if (enc->flags & F_CANONICAL) |
354 | { |
386 | { |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
356 | 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 |
357 | |
394 | |
358 | i = 0; |
395 | i = 0; |
359 | while ((he = hv_iternext (hv))) |
396 | while ((he = hv_iternext (hv))) |
360 | { |
397 | { |
361 | hes [i++] = he; |
398 | hes [i++] = he; |
… | |
… | |
396 | |
433 | |
397 | encode_nl (enc); |
434 | encode_nl (enc); |
398 | } |
435 | } |
399 | else |
436 | else |
400 | { |
437 | { |
401 | SV *sv; |
|
|
402 | HE *he = hv_iternext (hv); |
438 | HE *he = hv_iternext (hv); |
403 | |
439 | |
404 | for (;;) |
440 | for (;;) |
405 | { |
441 | { |
406 | encode_indent (enc); |
442 | encode_indent (enc); |
… | |
… | |
415 | encode_nl (enc); |
451 | encode_nl (enc); |
416 | } |
452 | } |
417 | } |
453 | } |
418 | |
454 | |
419 | --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)))); |
420 | } |
484 | } |
421 | |
485 | |
422 | static void |
486 | static void |
423 | encode_sv (enc_t *enc, SV *sv) |
487 | encode_sv (enc_t *enc, SV *sv) |
424 | { |
488 | { |
… | |
… | |
432 | encode_str (enc, str, len, SvUTF8 (sv)); |
496 | encode_str (enc, str, len, SvUTF8 (sv)); |
433 | encode_ch (enc, '"'); |
497 | encode_ch (enc, '"'); |
434 | } |
498 | } |
435 | else if (SvNOKp (sv)) |
499 | else if (SvNOKp (sv)) |
436 | { |
500 | { |
|
|
501 | // trust that perl will do the right thing w.r.t. JSON syntax. |
437 | need (enc, NV_DIG + 32); |
502 | need (enc, NV_DIG + 32); |
438 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
439 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
440 | } |
505 | } |
441 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
442 | { |
507 | { |
443 | 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); |
444 | enc->cur += |
513 | enc->cur += |
445 | SvIsUV(sv) |
514 | SvIsUV(sv) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
515 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
447 | : 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 | } |
448 | } |
545 | } |
449 | else if (SvROK (sv)) |
546 | else if (SvROK (sv)) |
450 | { |
547 | encode_rv (enc, SvRV (sv)); |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
|
|
454 | croak ("data structure too deep (hit recursion limit)"); |
|
|
455 | |
|
|
456 | switch (SvTYPE (rv)) |
|
|
457 | { |
|
|
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
460 | |
|
|
461 | default: |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | else if (!SvOK (sv)) |
548 | else if (!SvOK (sv)) |
467 | encode_str (enc, "null", 4, 0); |
549 | encode_str (enc, "null", 4, 0); |
468 | else |
550 | else |
469 | 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", |
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
552 | SvPV_nolen (sv), SvFLAGS (sv)); |
471 | } |
553 | } |
472 | |
554 | |
473 | static SV * |
555 | static SV * |
474 | encode_json (SV *scalar, UV flags) |
556 | encode_json (SV *scalar, U32 flags) |
475 | { |
557 | { |
|
|
558 | enc_t enc; |
|
|
559 | |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
560 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
477 | 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)"); |
478 | |
562 | |
479 | enc_t enc; |
|
|
480 | enc.flags = flags; |
563 | enc.flags = flags; |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
564 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
482 | enc.cur = SvPVX (enc.sv); |
565 | enc.cur = SvPVX (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
566 | enc.end = SvEND (enc.sv); |
484 | enc.indent = 0; |
567 | enc.indent = 0; |
485 | enc.max_depth = 0x7fffffffUL; |
568 | enc.maxdepth = DEC_DEPTH (flags); |
486 | |
569 | |
487 | SvPOK_only (enc.sv); |
570 | SvPOK_only (enc.sv); |
488 | encode_sv (&enc, scalar); |
571 | encode_sv (&enc, scalar); |
489 | |
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 | |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
576 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
491 | SvUTF8_on (enc.sv); |
577 | SvUTF8_on (enc.sv); |
492 | |
|
|
493 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
494 | |
578 | |
495 | if (enc.flags & F_SHRINK) |
579 | if (enc.flags & F_SHRINK) |
496 | shrink (enc.sv); |
580 | shrink (enc.sv); |
497 | |
581 | |
498 | return enc.sv; |
582 | return enc.sv; |
… | |
… | |
505 | typedef struct |
589 | typedef struct |
506 | { |
590 | { |
507 | char *cur; // current parser pointer |
591 | char *cur; // current parser pointer |
508 | char *end; // end of input string |
592 | char *end; // end of input string |
509 | const char *err; // parse error, if != 0 |
593 | const char *err; // parse error, if != 0 |
510 | UV flags; // F_* |
594 | U32 flags; // F_* |
|
|
595 | U32 depth; // recursion depth |
|
|
596 | U32 maxdepth; // recursion depth limit |
511 | } dec_t; |
597 | } dec_t; |
512 | |
598 | |
513 | static void |
599 | inline void |
514 | decode_ws (dec_t *dec) |
600 | decode_ws (dec_t *dec) |
515 | { |
601 | { |
516 | for (;;) |
602 | for (;;) |
517 | { |
603 | { |
518 | char ch = *dec->cur; |
604 | char ch = *dec->cur; |
… | |
… | |
524 | ++dec->cur; |
610 | ++dec->cur; |
525 | } |
611 | } |
526 | } |
612 | } |
527 | |
613 | |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
614 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
615 | |
529 | #define EXPECT_CH(ch) SB \ |
616 | #define EXPECT_CH(ch) SB \ |
530 | if (*dec->cur != ch) \ |
617 | if (*dec->cur != ch) \ |
531 | ERR (# ch " expected"); \ |
618 | ERR (# ch " expected"); \ |
532 | ++dec->cur; \ |
619 | ++dec->cur; \ |
533 | SE |
620 | SE |
534 | |
621 | |
|
|
622 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
623 | #define DEC_DEC_DEPTH --dec->depth |
|
|
624 | |
535 | static SV *decode_sv (dec_t *dec); |
625 | static SV *decode_sv (dec_t *dec); |
536 | |
626 | |
537 | static signed char decode_hexdigit[256]; |
627 | static signed char decode_hexdigit[256]; |
538 | |
628 | |
539 | static UV |
629 | static UV |
540 | decode_4hex (dec_t *dec) |
630 | decode_4hex (dec_t *dec) |
541 | { |
631 | { |
542 | signed char d1, d2, d3, d4; |
632 | signed char d1, d2, d3, d4; |
543 | unsigned char *cur = (unsigned char *)dec->cur; |
633 | unsigned char *cur = (unsigned char *)dec->cur; |
544 | |
634 | |
545 | 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"); |
546 | 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"); |
547 | 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"); |
548 | 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"); |
549 | |
639 | |
550 | dec->cur += 4; |
640 | dec->cur += 4; |
551 | |
641 | |
552 | return ((UV)d1) << 12 |
642 | return ((UV)d1) << 12 |
553 | | ((UV)d2) << 8 |
643 | | ((UV)d2) << 8 |
… | |
… | |
561 | static SV * |
651 | static SV * |
562 | decode_str (dec_t *dec) |
652 | decode_str (dec_t *dec) |
563 | { |
653 | { |
564 | SV *sv = 0; |
654 | SV *sv = 0; |
565 | int utf8 = 0; |
655 | int utf8 = 0; |
|
|
656 | char *dec_cur = dec->cur; |
566 | |
657 | |
567 | do |
658 | do |
568 | { |
659 | { |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
660 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
570 | char *cur = buf; |
661 | char *cur = buf; |
571 | |
662 | |
572 | do |
663 | do |
573 | { |
664 | { |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
665 | unsigned char ch = *(unsigned char *)dec_cur++; |
575 | |
666 | |
576 | if (ch == '"') |
667 | if (expect_false (ch == '"')) |
577 | { |
668 | { |
578 | --dec->cur; |
669 | --dec_cur; |
579 | break; |
670 | break; |
580 | } |
671 | } |
581 | else if (ch == '\\') |
672 | else if (expect_false (ch == '\\')) |
582 | { |
673 | { |
583 | switch (*dec->cur) |
674 | switch (*dec_cur) |
584 | { |
675 | { |
585 | case '\\': |
676 | case '\\': |
586 | case '/': |
677 | case '/': |
587 | case '"': *cur++ = *dec->cur++; break; |
678 | case '"': *cur++ = *dec_cur++; break; |
588 | |
679 | |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
680 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
681 | case 't': ++dec_cur; *cur++ = '\011'; break; |
591 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
682 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
592 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
683 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
593 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
684 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
594 | |
685 | |
595 | case 'u': |
686 | case 'u': |
596 | { |
687 | { |
597 | UV lo, hi; |
688 | UV lo, hi; |
598 | ++dec->cur; |
689 | ++dec_cur; |
599 | |
690 | |
|
|
691 | dec->cur = dec_cur; |
600 | hi = decode_4hex (dec); |
692 | hi = decode_4hex (dec); |
|
|
693 | dec_cur = dec->cur; |
601 | if (hi == (UV)-1) |
694 | if (hi == (UV)-1) |
602 | goto fail; |
695 | goto fail; |
603 | |
696 | |
604 | // possibly a surrogate pair |
697 | // possibly a surrogate pair |
605 | if (hi >= 0xd800) |
698 | if (hi >= 0xd800) |
606 | if (hi < 0xdc00) |
699 | if (hi < 0xdc00) |
607 | { |
700 | { |
608 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
701 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
609 | ERR ("missing low surrogate character in surrogate pair"); |
702 | ERR ("missing low surrogate character in surrogate pair"); |
610 | |
703 | |
611 | dec->cur += 2; |
704 | dec_cur += 2; |
612 | |
705 | |
|
|
706 | dec->cur = dec_cur; |
613 | lo = decode_4hex (dec); |
707 | lo = decode_4hex (dec); |
|
|
708 | dec_cur = dec->cur; |
614 | if (lo == (UV)-1) |
709 | if (lo == (UV)-1) |
615 | goto fail; |
710 | goto fail; |
616 | |
711 | |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
712 | if (lo < 0xdc00 || lo >= 0xe000) |
618 | ERR ("surrogate pair expected"); |
713 | ERR ("surrogate pair expected"); |
… | |
… | |
632 | *cur++ = hi; |
727 | *cur++ = hi; |
633 | } |
728 | } |
634 | break; |
729 | break; |
635 | |
730 | |
636 | default: |
731 | default: |
637 | --dec->cur; |
732 | --dec_cur; |
638 | ERR ("illegal backslash escape sequence in string"); |
733 | ERR ("illegal backslash escape sequence in string"); |
639 | } |
734 | } |
640 | } |
735 | } |
641 | else if (ch >= 0x20 && ch <= 0x7f) |
736 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
642 | *cur++ = ch; |
737 | *cur++ = ch; |
643 | else if (ch >= 0x80) |
738 | else if (ch >= 0x80) |
644 | { |
739 | { |
645 | --dec->cur; |
|
|
646 | |
|
|
647 | STRLEN clen; |
740 | STRLEN clen; |
|
|
741 | UV uch; |
|
|
742 | |
|
|
743 | --dec_cur; |
|
|
744 | |
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
745 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
649 | if (clen == (STRLEN)-1) |
746 | if (clen == (STRLEN)-1) |
650 | ERR ("malformed UTF-8 character in JSON string"); |
747 | ERR ("malformed UTF-8 character in JSON string"); |
651 | |
748 | |
652 | do |
749 | do |
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
750 | *cur++ = *dec_cur++; |
655 | } |
|
|
656 | while (--clen); |
751 | while (--clen); |
657 | |
752 | |
658 | utf8 = 1; |
753 | utf8 = 1; |
659 | } |
754 | } |
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
755 | else |
|
|
756 | { |
|
|
757 | --dec_cur; |
|
|
758 | |
|
|
759 | if (!ch) |
|
|
760 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
761 | else |
663 | ERR ("invalid character encountered"); |
762 | ERR ("invalid character encountered while parsing JSON string"); |
664 | |
763 | } |
665 | } |
764 | } |
666 | while (cur < buf + SHORT_STRING_LEN); |
765 | while (cur < buf + SHORT_STRING_LEN); |
667 | |
766 | |
|
|
767 | { |
668 | STRLEN len = cur - buf; |
768 | STRLEN len = cur - buf; |
669 | |
769 | |
670 | if (sv) |
770 | if (sv) |
671 | { |
771 | { |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
772 | SvGROW (sv, SvCUR (sv) + len + 1); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
773 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
774 | SvCUR_set (sv, SvCUR (sv) + len); |
675 | } |
775 | } |
676 | else |
776 | else |
677 | sv = newSVpvn (buf, len); |
777 | sv = newSVpvn (buf, len); |
678 | } |
778 | } |
|
|
779 | } |
679 | while (*dec->cur != '"'); |
780 | while (*dec_cur != '"'); |
680 | |
781 | |
681 | ++dec->cur; |
782 | ++dec_cur; |
682 | |
783 | |
683 | if (sv) |
784 | if (sv) |
684 | { |
785 | { |
685 | SvPOK_only (sv); |
786 | SvPOK_only (sv); |
686 | *SvEND (sv) = 0; |
787 | *SvEND (sv) = 0; |
… | |
… | |
689 | SvUTF8_on (sv); |
790 | SvUTF8_on (sv); |
690 | } |
791 | } |
691 | else |
792 | else |
692 | sv = newSVpvn ("", 0); |
793 | sv = newSVpvn ("", 0); |
693 | |
794 | |
|
|
795 | dec->cur = dec_cur; |
694 | return sv; |
796 | return sv; |
695 | |
797 | |
696 | fail: |
798 | fail: |
|
|
799 | dec->cur = dec_cur; |
697 | return 0; |
800 | return 0; |
698 | } |
801 | } |
699 | |
802 | |
700 | static SV * |
803 | static SV * |
701 | decode_num (dec_t *dec) |
804 | decode_num (dec_t *dec) |
… | |
… | |
759 | is_nv = 1; |
862 | is_nv = 1; |
760 | } |
863 | } |
761 | |
864 | |
762 | if (!is_nv) |
865 | if (!is_nv) |
763 | { |
866 | { |
764 | UV uv; |
867 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
765 | int numtype = grok_number (start, dec->cur - start, &uv); |
868 | if (*start == '-') |
766 | if (numtype & IS_NUMBER_IN_UV) |
869 | switch (dec->cur - start) |
767 | if (numtype & IS_NUMBER_NEG) |
|
|
768 | { |
870 | { |
769 | if (uv < (UV)IV_MIN) |
871 | case 2: return newSViv (-( start [1] - '0' * 1)); |
770 | 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)); |
771 | } |
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 | } |
772 | else |
894 | else |
773 | return newSVuv (uv); |
895 | return newSVuv (uv); |
|
|
896 | |
|
|
897 | // here would likely be the place for bigint support |
774 | } |
898 | } |
|
|
899 | } |
775 | |
900 | |
|
|
901 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
776 | return newSVnv (Atof (start)); |
902 | return newSVnv (Atof (start)); |
777 | |
903 | |
778 | fail: |
904 | fail: |
779 | return 0; |
905 | return 0; |
780 | } |
906 | } |
… | |
… | |
782 | static SV * |
908 | static SV * |
783 | decode_av (dec_t *dec) |
909 | decode_av (dec_t *dec) |
784 | { |
910 | { |
785 | AV *av = newAV (); |
911 | AV *av = newAV (); |
786 | |
912 | |
|
|
913 | DEC_INC_DEPTH; |
787 | decode_ws (dec); |
914 | decode_ws (dec); |
|
|
915 | |
788 | if (*dec->cur == ']') |
916 | if (*dec->cur == ']') |
789 | ++dec->cur; |
917 | ++dec->cur; |
790 | else |
918 | else |
791 | for (;;) |
919 | for (;;) |
792 | { |
920 | { |
… | |
… | |
810 | ERR (", or ] expected while parsing array"); |
938 | ERR (", or ] expected while parsing array"); |
811 | |
939 | |
812 | ++dec->cur; |
940 | ++dec->cur; |
813 | } |
941 | } |
814 | |
942 | |
|
|
943 | DEC_DEC_DEPTH; |
815 | return newRV_noinc ((SV *)av); |
944 | return newRV_noinc ((SV *)av); |
816 | |
945 | |
817 | fail: |
946 | fail: |
818 | SvREFCNT_dec (av); |
947 | SvREFCNT_dec (av); |
|
|
948 | DEC_DEC_DEPTH; |
819 | return 0; |
949 | return 0; |
820 | } |
950 | } |
821 | |
951 | |
822 | static SV * |
952 | static SV * |
823 | decode_hv (dec_t *dec) |
953 | decode_hv (dec_t *dec) |
824 | { |
954 | { |
825 | HV *hv = newHV (); |
955 | HV *hv = newHV (); |
826 | |
956 | |
|
|
957 | DEC_INC_DEPTH; |
827 | decode_ws (dec); |
958 | decode_ws (dec); |
|
|
959 | |
828 | if (*dec->cur == '}') |
960 | if (*dec->cur == '}') |
829 | ++dec->cur; |
961 | ++dec->cur; |
830 | else |
962 | else |
831 | for (;;) |
963 | for (;;) |
832 | { |
964 | { |
… | |
… | |
845 | { |
977 | { |
846 | SvREFCNT_dec (key); |
978 | SvREFCNT_dec (key); |
847 | goto fail; |
979 | goto fail; |
848 | } |
980 | } |
849 | |
981 | |
850 | //TODO: optimise |
|
|
851 | hv_store_ent (hv, key, value, 0); |
982 | hv_store_ent (hv, key, value, 0); |
|
|
983 | SvREFCNT_dec (key); |
852 | |
984 | |
853 | decode_ws (dec); |
985 | decode_ws (dec); |
854 | |
986 | |
855 | if (*dec->cur == '}') |
987 | if (*dec->cur == '}') |
856 | { |
988 | { |
… | |
… | |
862 | ERR (", or } expected while parsing object/hash"); |
994 | ERR (", or } expected while parsing object/hash"); |
863 | |
995 | |
864 | ++dec->cur; |
996 | ++dec->cur; |
865 | } |
997 | } |
866 | |
998 | |
|
|
999 | DEC_DEC_DEPTH; |
867 | return newRV_noinc ((SV *)hv); |
1000 | return newRV_noinc ((SV *)hv); |
868 | |
1001 | |
869 | fail: |
1002 | fail: |
870 | SvREFCNT_dec (hv); |
1003 | SvREFCNT_dec (hv); |
|
|
1004 | DEC_DEC_DEPTH; |
871 | return 0; |
1005 | return 0; |
872 | } |
1006 | } |
873 | |
1007 | |
874 | static SV * |
1008 | static SV * |
875 | decode_sv (dec_t *dec) |
1009 | decode_sv (dec_t *dec) |
876 | { |
1010 | { |
877 | decode_ws (dec); |
1011 | decode_ws (dec); |
|
|
1012 | |
|
|
1013 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1014 | // to parse anything. |
878 | switch (*dec->cur) |
1015 | switch (*dec->cur) |
879 | { |
1016 | { |
880 | case '"': ++dec->cur; return decode_str (dec); |
1017 | case '"': ++dec->cur; return decode_str (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
1018 | case '[': ++dec->cur; return decode_av (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
1019 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
918 | ERR ("'null' expected"); |
1055 | ERR ("'null' expected"); |
919 | |
1056 | |
920 | break; |
1057 | break; |
921 | |
1058 | |
922 | default: |
1059 | default: |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1060 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
924 | break; |
1061 | break; |
925 | } |
1062 | } |
926 | |
1063 | |
927 | fail: |
1064 | fail: |
928 | return 0; |
1065 | return 0; |
929 | } |
1066 | } |
930 | |
1067 | |
931 | static SV * |
1068 | static SV * |
932 | decode_json (SV *string, UV flags) |
1069 | decode_json (SV *string, U32 flags, UV *offset_return) |
933 | { |
1070 | { |
|
|
1071 | dec_t dec; |
|
|
1072 | UV offset; |
934 | SV *sv; |
1073 | SV *sv; |
|
|
1074 | |
|
|
1075 | SvGETMAGIC (string); |
|
|
1076 | SvUPGRADE (string, SVt_PV); |
935 | |
1077 | |
936 | if (flags & F_UTF8) |
1078 | if (flags & F_UTF8) |
937 | sv_utf8_downgrade (string, 0); |
1079 | sv_utf8_downgrade (string, 0); |
938 | else |
1080 | else |
939 | sv_utf8_upgrade (string); |
1081 | sv_utf8_upgrade (string); |
940 | |
1082 | |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1083 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
942 | |
1084 | |
943 | dec_t dec; |
|
|
944 | dec.flags = flags; |
1085 | dec.flags = flags; |
945 | dec.cur = SvPVX (string); |
1086 | dec.cur = SvPVX (string); |
946 | dec.end = SvEND (string); |
1087 | dec.end = SvEND (string); |
947 | dec.err = 0; |
1088 | dec.err = 0; |
|
|
1089 | dec.depth = 0; |
|
|
1090 | dec.maxdepth = DEC_DEPTH (dec.flags); |
948 | |
1091 | |
|
|
1092 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
949 | sv = decode_sv (&dec); |
1093 | sv = decode_sv (&dec); |
950 | |
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 | |
951 | if (!sv) |
1118 | if (!sv) |
952 | { |
1119 | { |
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
|
|
955 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
956 | SV *uni = sv_newmortal (); |
1120 | SV *uni = sv_newmortal (); |
957 | |
1121 | |
958 | // horrible hack to silence warning inside pv_uni_display |
1122 | // horrible hack to silence warning inside pv_uni_display |
959 | COP cop = *PL_curcop; |
1123 | COP cop = *PL_curcop; |
960 | cop.cop_warnings = pWARN_NONE; |
1124 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
962 | SAVEVPTR (PL_curcop); |
1126 | SAVEVPTR (PL_curcop); |
963 | PL_curcop = &cop; |
1127 | PL_curcop = &cop; |
964 | 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); |
965 | LEAVE; |
1129 | LEAVE; |
966 | |
1130 | |
967 | croak ("%s, at character offset %d (%s)", |
1131 | croak ("%s, at character offset %d [\"%s\"]", |
968 | dec.err, |
1132 | dec.err, |
969 | (int)offset, |
1133 | (int)offset, |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1134 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
971 | } |
1135 | } |
972 | |
1136 | |
… | |
… | |
985 | |
1149 | |
986 | BOOT: |
1150 | BOOT: |
987 | { |
1151 | { |
988 | int i; |
1152 | int i; |
989 | |
1153 | |
990 | memset (decode_hexdigit, 0xff, 256); |
|
|
991 | for (i = 10; i--; ) |
1154 | for (i = 0; i < 256; ++i) |
992 | decode_hexdigit ['0' + i] = i; |
1155 | decode_hexdigit [i] = |
993 | |
1156 | i >= '0' && i <= '9' ? i - '0' |
994 | for (i = 7; i--; ) |
1157 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
995 | { |
1158 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
996 | decode_hexdigit ['a' + i] = 10 + i; |
1159 | : -1; |
997 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
998 | } |
|
|
999 | |
1160 | |
1000 | json_stash = gv_stashpv ("JSON::XS", 1); |
1161 | json_stash = gv_stashpv ("JSON::XS", 1); |
1001 | } |
1162 | } |
1002 | |
1163 | |
1003 | PROTOTYPES: DISABLE |
1164 | PROTOTYPES: DISABLE |
… | |
… | |
1009 | RETVAL |
1170 | RETVAL |
1010 | |
1171 | |
1011 | SV *ascii (SV *self, int enable = 1) |
1172 | SV *ascii (SV *self, int enable = 1) |
1012 | ALIAS: |
1173 | ALIAS: |
1013 | ascii = F_ASCII |
1174 | ascii = F_ASCII |
|
|
1175 | latin1 = F_LATIN1 |
1014 | utf8 = F_UTF8 |
1176 | utf8 = F_UTF8 |
1015 | indent = F_INDENT |
1177 | indent = F_INDENT |
1016 | canonical = F_CANONICAL |
1178 | canonical = F_CANONICAL |
1017 | space_before = F_SPACE_BEFORE |
1179 | space_before = F_SPACE_BEFORE |
1018 | space_after = F_SPACE_AFTER |
1180 | space_after = F_SPACE_AFTER |
… | |
… | |
1030 | RETVAL = newSVsv (self); |
1192 | RETVAL = newSVsv (self); |
1031 | } |
1193 | } |
1032 | OUTPUT: |
1194 | OUTPUT: |
1033 | RETVAL |
1195 | RETVAL |
1034 | |
1196 | |
|
|
1197 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1198 | CODE: |
|
|
1199 | { |
|
|
1200 | UV *uv = SvJSON (self); |
|
|
1201 | UV log2 = 0; |
|
|
1202 | |
|
|
1203 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1204 | |
|
|
1205 | while ((1UL << log2) < max_depth) |
|
|
1206 | ++log2; |
|
|
1207 | |
|
|
1208 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1209 | |
|
|
1210 | RETVAL = newSVsv (self); |
|
|
1211 | } |
|
|
1212 | OUTPUT: |
|
|
1213 | RETVAL |
|
|
1214 | |
1035 | void encode (SV *self, SV *scalar) |
1215 | void encode (SV *self, SV *scalar) |
1036 | PPCODE: |
1216 | PPCODE: |
1037 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1217 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1038 | |
1218 | |
1039 | void decode (SV *self, SV *jsonstr) |
1219 | void decode (SV *self, SV *jsonstr) |
1040 | PPCODE: |
1220 | PPCODE: |
1041 | 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 | } |
1042 | |
1231 | |
1043 | PROTOTYPES: ENABLE |
1232 | PROTOTYPES: ENABLE |
1044 | |
1233 | |
1045 | void to_json (SV *scalar) |
1234 | void to_json (SV *scalar) |
1046 | ALIAS: |
1235 | ALIAS: |
1047 | objToJson = 0 |
1236 | objToJson = 0 |
1048 | PPCODE: |
1237 | PPCODE: |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1238 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1050 | |
1239 | |
1051 | void from_json (SV *jsonstr) |
1240 | void from_json (SV *jsonstr) |
1052 | ALIAS: |
1241 | ALIAS: |
1053 | jsonToObj = 0 |
1242 | jsonToObj = 0 |
1054 | PPCODE: |
1243 | PPCODE: |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1244 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1056 | |
1245 | |