… | |
… | |
9 | |
9 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
12 | #endif |
13 | |
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
24 | #define F_CANONICAL 0x00000010UL |
18 | #define F_SPACE_BEFORE 0x00000010UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
19 | #define F_SPACE_AFTER 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
20 | #define F_ALLOW_NONREF 0x00000080UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
21 | #define F_SHRINK 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
22 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
24 | |
35 | |
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
|
|
29 | |
38 | |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
32 | |
41 | |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
35 | |
44 | |
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
37 | |
46 | |
38 | #define SB do { |
47 | #define SB do { |
39 | #define SE } while (0) |
48 | #define SE } while (0) |
40 | |
49 | |
|
|
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
41 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
42 | |
67 | |
43 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
44 | // utility functions |
69 | // utility functions |
45 | |
70 | |
46 | static UV * |
71 | static UV * |
… | |
… | |
69 | // decode an utf-8 character and return it, or (UV)-1 in |
94 | // decode an utf-8 character and return it, or (UV)-1 in |
70 | // case of an error. |
95 | // case of an error. |
71 | // we special-case "safe" characters from U+80 .. U+7FF, |
96 | // we special-case "safe" characters from U+80 .. U+7FF, |
72 | // but use the very good perl function to parse anything else. |
97 | // but use the very good perl function to parse anything else. |
73 | // note that we never call this function for a ascii codepoints |
98 | // note that we never call this function for a ascii codepoints |
74 | static UV |
99 | inline UV |
75 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
100 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
76 | { |
101 | { |
77 | if (s[0] > 0xdf || s[0] < 0xc2) |
102 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
78 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
103 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
79 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
104 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
80 | { |
105 | { |
81 | *clen = 2; |
106 | *clen = 2; |
82 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
107 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
95 | typedef struct |
120 | typedef struct |
96 | { |
121 | { |
97 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
98 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
99 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
100 | U32 flags; // F_* |
125 | struct json json; |
101 | U32 indent; // indentation level |
126 | U32 indent; // indentation level |
102 | U32 maxdepth; // max. indentation/recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
103 | } enc_t; |
128 | } enc_t; |
104 | |
129 | |
105 | static void |
130 | inline void |
106 | need (enc_t *enc, STRLEN len) |
131 | need (enc_t *enc, STRLEN len) |
107 | { |
132 | { |
108 | if (enc->cur + len >= enc->end) |
133 | if (expect_false (enc->cur + len >= enc->end)) |
109 | { |
134 | { |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
135 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | SvGROW (enc->sv, cur + len + 1); |
136 | SvGROW (enc->sv, cur + len + 1); |
112 | enc->cur = SvPVX (enc->sv) + cur; |
137 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
138 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
114 | } |
139 | } |
115 | } |
140 | } |
116 | |
141 | |
117 | static void |
142 | inline void |
118 | encode_ch (enc_t *enc, char ch) |
143 | encode_ch (enc_t *enc, char ch) |
119 | { |
144 | { |
120 | need (enc, 1); |
145 | need (enc, 1); |
121 | *enc->cur++ = ch; |
146 | *enc->cur++ = ch; |
122 | } |
147 | } |
… | |
… | |
130 | |
155 | |
131 | while (str < end) |
156 | while (str < end) |
132 | { |
157 | { |
133 | unsigned char ch = *(unsigned char *)str; |
158 | unsigned char ch = *(unsigned char *)str; |
134 | |
159 | |
135 | if (ch >= 0x20 && ch < 0x80) // most common case |
160 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
136 | { |
161 | { |
137 | if (ch == '"') // but with slow exceptions |
162 | if (expect_false (ch == '"')) // but with slow exceptions |
138 | { |
163 | { |
139 | need (enc, len += 1); |
164 | need (enc, len += 1); |
140 | *enc->cur++ = '\\'; |
165 | *enc->cur++ = '\\'; |
141 | *enc->cur++ = '"'; |
166 | *enc->cur++ = '"'; |
142 | } |
167 | } |
143 | else if (ch == '\\') |
168 | else if (expect_false (ch == '\\')) |
144 | { |
169 | { |
145 | need (enc, len += 1); |
170 | need (enc, len += 1); |
146 | *enc->cur++ = '\\'; |
171 | *enc->cur++ = '\\'; |
147 | *enc->cur++ = '\\'; |
172 | *enc->cur++ = '\\'; |
148 | } |
173 | } |
… | |
… | |
166 | STRLEN clen; |
191 | STRLEN clen; |
167 | UV uch; |
192 | UV uch; |
168 | |
193 | |
169 | if (is_utf8) |
194 | if (is_utf8) |
170 | { |
195 | { |
171 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
172 | uch = decode_utf8 (str, end - str, &clen); |
196 | uch = decode_utf8 (str, end - str, &clen); |
173 | if (clen == (STRLEN)-1) |
197 | if (clen == (STRLEN)-1) |
174 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
198 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
175 | } |
199 | } |
176 | else |
200 | else |
… | |
… | |
180 | } |
204 | } |
181 | |
205 | |
182 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
183 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | |
208 | |
185 | if (uch < 0x80 || enc->flags & F_ASCII) |
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
186 | { |
210 | { |
187 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
188 | { |
212 | { |
189 | need (enc, len += 11); |
213 | need (enc, len += 11); |
190 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
204 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | } |
229 | } |
206 | |
230 | |
207 | str += clen; |
231 | str += clen; |
208 | } |
232 | } |
|
|
233 | else if (enc->json.flags & F_LATIN1) |
|
|
234 | { |
|
|
235 | *enc->cur++ = uch; |
|
|
236 | str += clen; |
|
|
237 | } |
209 | else if (is_utf8) |
238 | else if (is_utf8) |
210 | { |
239 | { |
211 | need (enc, len += clen); |
240 | need (enc, len += clen); |
212 | do |
241 | do |
213 | { |
242 | { |
… | |
… | |
227 | |
256 | |
228 | --len; |
257 | --len; |
229 | } |
258 | } |
230 | } |
259 | } |
231 | |
260 | |
232 | static void |
261 | inline void |
233 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
234 | { |
263 | { |
235 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
236 | { |
265 | { |
237 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
238 | |
267 | |
239 | need (enc, spaces); |
268 | need (enc, spaces); |
240 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
241 | enc->cur += spaces; |
270 | enc->cur += spaces; |
242 | } |
271 | } |
243 | } |
272 | } |
244 | |
273 | |
245 | static void |
274 | inline void |
246 | encode_space (enc_t *enc) |
275 | encode_space (enc_t *enc) |
247 | { |
276 | { |
248 | need (enc, 1); |
277 | need (enc, 1); |
249 | encode_ch (enc, ' '); |
278 | encode_ch (enc, ' '); |
250 | } |
279 | } |
251 | |
280 | |
252 | static void |
281 | inline void |
253 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
254 | { |
283 | { |
255 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
256 | { |
285 | { |
257 | need (enc, 1); |
286 | need (enc, 1); |
258 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
259 | } |
288 | } |
260 | } |
289 | } |
261 | |
290 | |
262 | static void |
291 | inline void |
263 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
264 | { |
293 | { |
265 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
266 | |
295 | |
267 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
268 | encode_nl (enc); |
297 | encode_nl (enc); |
269 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
270 | encode_space (enc); |
299 | encode_space (enc); |
271 | } |
300 | } |
272 | |
301 | |
273 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
274 | |
303 | |
… | |
… | |
317 | else |
346 | else |
318 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
319 | |
348 | |
320 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
321 | |
350 | |
322 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
323 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
324 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
325 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
326 | } |
355 | } |
327 | |
356 | |
328 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
329 | static int |
358 | static int |
… | |
… | |
364 | { |
393 | { |
365 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
366 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
367 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
368 | // that randomises hash orderings |
397 | // that randomises hash orderings |
369 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
370 | { |
399 | { |
371 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
372 | int fast = 1; |
400 | int fast = 1; |
|
|
401 | HE *he; |
|
|
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
403 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
404 | #else |
|
|
405 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
406 | #endif |
373 | |
407 | |
374 | i = 0; |
408 | i = 0; |
375 | while ((he = hv_iternext (hv))) |
409 | while ((he = hv_iternext (hv))) |
376 | { |
410 | { |
377 | hes [i++] = he; |
411 | hes [i++] = he; |
… | |
… | |
441 | svtype svt; |
475 | svtype svt; |
442 | |
476 | |
443 | SvGETMAGIC (sv); |
477 | SvGETMAGIC (sv); |
444 | svt = SvTYPE (sv); |
478 | svt = SvTYPE (sv); |
445 | |
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv) == 0) |
|
|
485 | encode_str (enc, "false", 5, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
511 | // check anyways. |
|
|
512 | PUTBACK; |
|
|
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | encode_sv (enc, POPs); |
|
|
517 | |
|
|
518 | FREETMPS; |
|
|
519 | LEAVE; |
|
|
520 | } |
|
|
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
522 | encode_str (enc, "null", 4, 0); |
|
|
523 | else |
|
|
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
526 | } |
|
|
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
528 | encode_str (enc, "null", 4, 0); |
|
|
529 | else |
|
|
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
532 | } |
|
|
533 | } |
446 | if (svt == SVt_PVHV) |
534 | else if (svt == SVt_PVHV) |
447 | encode_hv (enc, (HV *)sv); |
535 | encode_hv (enc, (HV *)sv); |
448 | else if (svt == SVt_PVAV) |
536 | else if (svt == SVt_PVAV) |
449 | encode_av (enc, (AV *)sv); |
537 | encode_av (enc, (AV *)sv); |
450 | else if (svt < SVt_PVAV) |
538 | else if (svt < SVt_PVAV) |
451 | { |
539 | { |
… | |
… | |
475 | encode_str (enc, str, len, SvUTF8 (sv)); |
563 | encode_str (enc, str, len, SvUTF8 (sv)); |
476 | encode_ch (enc, '"'); |
564 | encode_ch (enc, '"'); |
477 | } |
565 | } |
478 | else if (SvNOKp (sv)) |
566 | else if (SvNOKp (sv)) |
479 | { |
567 | { |
|
|
568 | // trust that perl will do the right thing w.r.t. JSON syntax. |
480 | need (enc, NV_DIG + 32); |
569 | need (enc, NV_DIG + 32); |
481 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
570 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
482 | enc->cur += strlen (enc->cur); |
571 | enc->cur += strlen (enc->cur); |
483 | } |
572 | } |
484 | else if (SvIOKp (sv)) |
573 | else if (SvIOKp (sv)) |
485 | { |
574 | { |
486 | need (enc, 64); |
575 | // we assume we can always read an IV as a UV |
|
|
576 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
577 | { |
|
|
578 | // large integer, use the (rather slow) snprintf way. |
|
|
579 | need (enc, sizeof (UV) * 3); |
487 | enc->cur += |
580 | enc->cur += |
488 | SvIsUV(sv) |
581 | SvIsUV(sv) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
582 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
583 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
584 | } |
|
|
585 | else |
|
|
586 | { |
|
|
587 | // optimise the "small number case" |
|
|
588 | // code will likely be branchless and use only a single multiplication |
|
|
589 | I32 i = SvIV (sv); |
|
|
590 | U32 u; |
|
|
591 | char digit, nz = 0; |
|
|
592 | |
|
|
593 | need (enc, 6); |
|
|
594 | |
|
|
595 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
596 | u = i < 0 ? -i : i; |
|
|
597 | |
|
|
598 | // convert to 4.28 fixed-point representation |
|
|
599 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
600 | |
|
|
601 | // now output digit by digit, each time masking out the integer part |
|
|
602 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
603 | // resulting in a net multiplication by 10. |
|
|
604 | // we always write the digit to memory but conditionally increment |
|
|
605 | // the pointer, to ease the usage of conditional move instructions. |
|
|
606 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
607 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
608 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
609 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
610 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
611 | } |
491 | } |
612 | } |
492 | else if (SvROK (sv)) |
613 | else if (SvROK (sv)) |
493 | encode_rv (enc, SvRV (sv)); |
614 | encode_rv (enc, SvRV (sv)); |
494 | else if (!SvOK (sv)) |
615 | else if (!SvOK (sv)) |
495 | encode_str (enc, "null", 4, 0); |
616 | encode_str (enc, "null", 4, 0); |
… | |
… | |
497 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
498 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
499 | } |
620 | } |
500 | |
621 | |
501 | static SV * |
622 | static SV * |
502 | encode_json (SV *scalar, U32 flags) |
623 | encode_json (SV *scalar, struct json *json) |
503 | { |
624 | { |
504 | enc_t enc; |
625 | enc_t enc; |
505 | |
626 | |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | |
629 | |
509 | enc.flags = flags; |
630 | enc.json = *json; |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
513 | enc.indent = 0; |
634 | enc.indent = 0; |
514 | enc.maxdepth = DEC_DEPTH (flags); |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
515 | |
636 | |
516 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
517 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
518 | |
639 | |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
|
|
520 | SvUTF8_on (enc.sv); |
|
|
521 | |
|
|
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
641 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
524 | |
642 | |
|
|
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
|
|
644 | SvUTF8_on (enc.sv); |
|
|
645 | |
525 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
526 | shrink (enc.sv); |
647 | shrink (enc.sv); |
527 | |
648 | |
528 | return enc.sv; |
649 | return enc.sv; |
529 | } |
650 | } |
530 | |
651 | |
… | |
… | |
535 | typedef struct |
656 | typedef struct |
536 | { |
657 | { |
537 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
538 | char *end; // end of input string |
659 | char *end; // end of input string |
539 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
540 | U32 flags; // F_* |
661 | struct json json; |
541 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
542 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
543 | } dec_t; |
664 | } dec_t; |
544 | |
665 | |
545 | static void |
666 | inline void |
546 | decode_ws (dec_t *dec) |
667 | decode_ws (dec_t *dec) |
547 | { |
668 | { |
548 | for (;;) |
669 | for (;;) |
549 | { |
670 | { |
550 | char ch = *dec->cur; |
671 | char ch = *dec->cur; |
… | |
… | |
576 | decode_4hex (dec_t *dec) |
697 | decode_4hex (dec_t *dec) |
577 | { |
698 | { |
578 | signed char d1, d2, d3, d4; |
699 | signed char d1, d2, d3, d4; |
579 | unsigned char *cur = (unsigned char *)dec->cur; |
700 | unsigned char *cur = (unsigned char *)dec->cur; |
580 | |
701 | |
581 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
702 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
582 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
703 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
583 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
704 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
584 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
705 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
585 | |
706 | |
586 | dec->cur += 4; |
707 | dec->cur += 4; |
587 | |
708 | |
588 | return ((UV)d1) << 12 |
709 | return ((UV)d1) << 12 |
589 | | ((UV)d2) << 8 |
710 | | ((UV)d2) << 8 |
… | |
… | |
597 | static SV * |
718 | static SV * |
598 | decode_str (dec_t *dec) |
719 | decode_str (dec_t *dec) |
599 | { |
720 | { |
600 | SV *sv = 0; |
721 | SV *sv = 0; |
601 | int utf8 = 0; |
722 | int utf8 = 0; |
|
|
723 | char *dec_cur = dec->cur; |
602 | |
724 | |
603 | do |
725 | do |
604 | { |
726 | { |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
727 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
606 | char *cur = buf; |
728 | char *cur = buf; |
607 | |
729 | |
608 | do |
730 | do |
609 | { |
731 | { |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
732 | unsigned char ch = *(unsigned char *)dec_cur++; |
611 | |
733 | |
612 | if (ch == '"') |
734 | if (expect_false (ch == '"')) |
613 | { |
735 | { |
614 | --dec->cur; |
736 | --dec_cur; |
615 | break; |
737 | break; |
616 | } |
738 | } |
617 | else if (ch == '\\') |
739 | else if (expect_false (ch == '\\')) |
618 | { |
740 | { |
619 | switch (*dec->cur) |
741 | switch (*dec_cur) |
620 | { |
742 | { |
621 | case '\\': |
743 | case '\\': |
622 | case '/': |
744 | case '/': |
623 | case '"': *cur++ = *dec->cur++; break; |
745 | case '"': *cur++ = *dec_cur++; break; |
624 | |
746 | |
625 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
747 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
626 | case 't': ++dec->cur; *cur++ = '\011'; break; |
748 | case 't': ++dec_cur; *cur++ = '\011'; break; |
627 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
749 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
628 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
750 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
629 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
751 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
630 | |
752 | |
631 | case 'u': |
753 | case 'u': |
632 | { |
754 | { |
633 | UV lo, hi; |
755 | UV lo, hi; |
634 | ++dec->cur; |
756 | ++dec_cur; |
635 | |
757 | |
|
|
758 | dec->cur = dec_cur; |
636 | hi = decode_4hex (dec); |
759 | hi = decode_4hex (dec); |
|
|
760 | dec_cur = dec->cur; |
637 | if (hi == (UV)-1) |
761 | if (hi == (UV)-1) |
638 | goto fail; |
762 | goto fail; |
639 | |
763 | |
640 | // possibly a surrogate pair |
764 | // possibly a surrogate pair |
641 | if (hi >= 0xd800) |
765 | if (hi >= 0xd800) |
642 | if (hi < 0xdc00) |
766 | if (hi < 0xdc00) |
643 | { |
767 | { |
644 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
768 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
645 | ERR ("missing low surrogate character in surrogate pair"); |
769 | ERR ("missing low surrogate character in surrogate pair"); |
646 | |
770 | |
647 | dec->cur += 2; |
771 | dec_cur += 2; |
648 | |
772 | |
|
|
773 | dec->cur = dec_cur; |
649 | lo = decode_4hex (dec); |
774 | lo = decode_4hex (dec); |
|
|
775 | dec_cur = dec->cur; |
650 | if (lo == (UV)-1) |
776 | if (lo == (UV)-1) |
651 | goto fail; |
777 | goto fail; |
652 | |
778 | |
653 | if (lo < 0xdc00 || lo >= 0xe000) |
779 | if (lo < 0xdc00 || lo >= 0xe000) |
654 | ERR ("surrogate pair expected"); |
780 | ERR ("surrogate pair expected"); |
… | |
… | |
668 | *cur++ = hi; |
794 | *cur++ = hi; |
669 | } |
795 | } |
670 | break; |
796 | break; |
671 | |
797 | |
672 | default: |
798 | default: |
673 | --dec->cur; |
799 | --dec_cur; |
674 | ERR ("illegal backslash escape sequence in string"); |
800 | ERR ("illegal backslash escape sequence in string"); |
675 | } |
801 | } |
676 | } |
802 | } |
677 | else if (ch >= 0x20 && ch <= 0x7f) |
803 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
678 | *cur++ = ch; |
804 | *cur++ = ch; |
679 | else if (ch >= 0x80) |
805 | else if (ch >= 0x80) |
680 | { |
806 | { |
681 | STRLEN clen; |
807 | STRLEN clen; |
682 | UV uch; |
808 | UV uch; |
683 | |
809 | |
684 | --dec->cur; |
810 | --dec_cur; |
685 | |
811 | |
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
812 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
687 | if (clen == (STRLEN)-1) |
813 | if (clen == (STRLEN)-1) |
688 | ERR ("malformed UTF-8 character in JSON string"); |
814 | ERR ("malformed UTF-8 character in JSON string"); |
689 | |
815 | |
690 | do |
816 | do |
691 | *cur++ = *dec->cur++; |
817 | *cur++ = *dec_cur++; |
692 | while (--clen); |
818 | while (--clen); |
693 | |
819 | |
694 | utf8 = 1; |
820 | utf8 = 1; |
695 | } |
821 | } |
696 | else |
822 | else |
697 | { |
823 | { |
698 | --dec->cur; |
824 | --dec_cur; |
699 | |
825 | |
700 | if (!ch) |
826 | if (!ch) |
701 | ERR ("unexpected end of string while parsing JSON string"); |
827 | ERR ("unexpected end of string while parsing JSON string"); |
702 | else |
828 | else |
703 | ERR ("invalid character encountered while parsing JSON string"); |
829 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
716 | } |
842 | } |
717 | else |
843 | else |
718 | sv = newSVpvn (buf, len); |
844 | sv = newSVpvn (buf, len); |
719 | } |
845 | } |
720 | } |
846 | } |
721 | while (*dec->cur != '"'); |
847 | while (*dec_cur != '"'); |
722 | |
848 | |
723 | ++dec->cur; |
849 | ++dec_cur; |
724 | |
850 | |
725 | if (sv) |
851 | if (sv) |
726 | { |
852 | { |
727 | SvPOK_only (sv); |
853 | SvPOK_only (sv); |
728 | *SvEND (sv) = 0; |
854 | *SvEND (sv) = 0; |
… | |
… | |
731 | SvUTF8_on (sv); |
857 | SvUTF8_on (sv); |
732 | } |
858 | } |
733 | else |
859 | else |
734 | sv = newSVpvn ("", 0); |
860 | sv = newSVpvn ("", 0); |
735 | |
861 | |
|
|
862 | dec->cur = dec_cur; |
736 | return sv; |
863 | return sv; |
737 | |
864 | |
738 | fail: |
865 | fail: |
|
|
866 | dec->cur = dec_cur; |
739 | return 0; |
867 | return 0; |
740 | } |
868 | } |
741 | |
869 | |
742 | static SV * |
870 | static SV * |
743 | decode_num (dec_t *dec) |
871 | decode_num (dec_t *dec) |
… | |
… | |
801 | is_nv = 1; |
929 | is_nv = 1; |
802 | } |
930 | } |
803 | |
931 | |
804 | if (!is_nv) |
932 | if (!is_nv) |
805 | { |
933 | { |
806 | UV uv; |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
807 | int numtype = grok_number (start, dec->cur - start, &uv); |
935 | if (*start == '-') |
808 | if (numtype & IS_NUMBER_IN_UV) |
936 | switch (dec->cur - start) |
809 | if (numtype & IS_NUMBER_NEG) |
|
|
810 | { |
937 | { |
811 | if (uv < (UV)IV_MIN) |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
812 | return newSViv (-(IV)uv); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
813 | } |
942 | } |
|
|
943 | else |
|
|
944 | switch (dec->cur - start) |
|
|
945 | { |
|
|
946 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
950 | } |
|
|
951 | |
|
|
952 | { |
|
|
953 | UV uv; |
|
|
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
955 | if (numtype & IS_NUMBER_IN_UV) |
|
|
956 | if (numtype & IS_NUMBER_NEG) |
|
|
957 | { |
|
|
958 | if (uv < (UV)IV_MIN) |
|
|
959 | return newSViv (-(IV)uv); |
|
|
960 | } |
814 | else |
961 | else |
815 | return newSVuv (uv); |
962 | return newSVuv (uv); |
|
|
963 | |
|
|
964 | // here would likely be the place for bigint support |
816 | } |
965 | } |
|
|
966 | } |
817 | |
967 | |
|
|
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
818 | return newSVnv (Atof (start)); |
969 | return newSVnv (Atof (start)); |
819 | |
970 | |
820 | fail: |
971 | fail: |
821 | return 0; |
972 | return 0; |
822 | } |
973 | } |
… | |
… | |
876 | if (*dec->cur == '}') |
1027 | if (*dec->cur == '}') |
877 | ++dec->cur; |
1028 | ++dec->cur; |
878 | else |
1029 | else |
879 | for (;;) |
1030 | for (;;) |
880 | { |
1031 | { |
881 | SV *key, *value; |
|
|
882 | |
|
|
883 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
884 | |
1033 | |
885 | key = decode_str (dec); |
1034 | // heuristic: assume that |
886 | if (!key) |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
887 | goto fail; |
1036 | // b) most hash keys are short, simple ascii text. |
|
|
1037 | // => try to "fast-match" such strings to avoid |
|
|
1038 | // the overhead of decode_str + hv_store_ent. |
|
|
1039 | { |
|
|
1040 | SV *value; |
|
|
1041 | char *p = dec->cur; |
|
|
1042 | char *e = p + 24; // only try up to 24 bytes |
888 | |
1043 | |
889 | decode_ws (dec); EXPECT_CH (':'); |
1044 | for (;;) |
890 | |
|
|
891 | value = decode_sv (dec); |
|
|
892 | if (!value) |
|
|
893 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
|
|
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1048 | { |
|
|
1049 | // slow path, back up and use decode_str |
|
|
1050 | SV *key = decode_str (dec); |
|
|
1051 | if (!key) |
|
|
1052 | goto fail; |
|
|
1053 | |
|
|
1054 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1055 | |
|
|
1056 | value = decode_sv (dec); |
|
|
1057 | if (!value) |
|
|
1058 | { |
|
|
1059 | SvREFCNT_dec (key); |
|
|
1060 | goto fail; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | hv_store_ent (hv, key, value, 0); |
894 | SvREFCNT_dec (key); |
1064 | SvREFCNT_dec (key); |
|
|
1065 | |
|
|
1066 | break; |
|
|
1067 | } |
|
|
1068 | else if (*p == '"') |
|
|
1069 | { |
|
|
1070 | // fast path, got a simple key |
|
|
1071 | char *key = dec->cur; |
|
|
1072 | int len = p - key; |
|
|
1073 | dec->cur = p + 1; |
|
|
1074 | |
|
|
1075 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1076 | |
|
|
1077 | value = decode_sv (dec); |
|
|
1078 | if (!value) |
895 | goto fail; |
1079 | goto fail; |
|
|
1080 | |
|
|
1081 | hv_store (hv, key, len, value, 0); |
|
|
1082 | |
|
|
1083 | break; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | ++p; |
896 | } |
1087 | } |
897 | |
1088 | } |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
|
|
900 | |
1089 | |
901 | decode_ws (dec); |
1090 | decode_ws (dec); |
902 | |
1091 | |
903 | if (*dec->cur == '}') |
1092 | if (*dec->cur == '}') |
904 | { |
1093 | { |
… | |
… | |
923 | |
1112 | |
924 | static SV * |
1113 | static SV * |
925 | decode_sv (dec_t *dec) |
1114 | decode_sv (dec_t *dec) |
926 | { |
1115 | { |
927 | decode_ws (dec); |
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1119 | // to parse anything. |
928 | switch (*dec->cur) |
1120 | switch (*dec->cur) |
929 | { |
1121 | { |
930 | case '"': ++dec->cur; return decode_str (dec); |
1122 | case '"': ++dec->cur; return decode_str (dec); |
931 | case '[': ++dec->cur; return decode_av (dec); |
1123 | case '[': ++dec->cur; return decode_av (dec); |
932 | case '{': ++dec->cur; return decode_hv (dec); |
1124 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
938 | |
1130 | |
939 | case 't': |
1131 | case 't': |
940 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
941 | { |
1133 | { |
942 | dec->cur += 4; |
1134 | dec->cur += 4; |
943 | return newSViv (1); |
1135 | return SvREFCNT_inc (json_true); |
944 | } |
1136 | } |
945 | else |
1137 | else |
946 | ERR ("'true' expected"); |
1138 | ERR ("'true' expected"); |
947 | |
1139 | |
948 | break; |
1140 | break; |
949 | |
1141 | |
950 | case 'f': |
1142 | case 'f': |
951 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
952 | { |
1144 | { |
953 | dec->cur += 5; |
1145 | dec->cur += 5; |
954 | return newSViv (0); |
1146 | return SvREFCNT_inc (json_false); |
955 | } |
1147 | } |
956 | else |
1148 | else |
957 | ERR ("'false' expected"); |
1149 | ERR ("'false' expected"); |
958 | |
1150 | |
959 | break; |
1151 | break; |
… | |
… | |
977 | fail: |
1169 | fail: |
978 | return 0; |
1170 | return 0; |
979 | } |
1171 | } |
980 | |
1172 | |
981 | static SV * |
1173 | static SV * |
982 | decode_json (SV *string, U32 flags) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
983 | { |
1175 | { |
984 | dec_t dec; |
1176 | dec_t dec; |
|
|
1177 | UV offset; |
985 | SV *sv; |
1178 | SV *sv; |
986 | |
1179 | |
987 | SvGETMAGIC (string); |
1180 | SvGETMAGIC (string); |
988 | SvUPGRADE (string, SVt_PV); |
1181 | SvUPGRADE (string, SVt_PV); |
989 | |
1182 | |
|
|
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1186 | |
990 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
991 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
992 | else |
1189 | else |
993 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
994 | |
1191 | |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
996 | |
1193 | |
997 | dec.flags = flags; |
1194 | dec.json = *json; |
998 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
999 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
1000 | dec.err = 0; |
1197 | dec.err = 0; |
1001 | dec.depth = 0; |
1198 | dec.depth = 0; |
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1003 | |
1200 | |
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1005 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
1006 | |
1203 | |
|
|
1204 | if (!(offset_return || !sv)) |
|
|
1205 | { |
|
|
1206 | // check for trailing garbage |
|
|
1207 | decode_ws (&dec); |
|
|
1208 | |
|
|
1209 | if (*dec.cur) |
|
|
1210 | { |
|
|
1211 | dec.err = "garbage after JSON object"; |
|
|
1212 | SvREFCNT_dec (sv); |
|
|
1213 | sv = 0; |
|
|
1214 | } |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | if (offset_return || !sv) |
|
|
1218 | { |
|
|
1219 | offset = dec.json.flags & F_UTF8 |
|
|
1220 | ? dec.cur - SvPVX (string) |
|
|
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1222 | |
|
|
1223 | if (offset_return) |
|
|
1224 | *offset_return = offset; |
|
|
1225 | } |
|
|
1226 | |
1007 | if (!sv) |
1227 | if (!sv) |
1008 | { |
1228 | { |
1009 | IV offset = dec.flags & F_UTF8 |
|
|
1010 | ? dec.cur - SvPVX (string) |
|
|
1011 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1012 | SV *uni = sv_newmortal (); |
1229 | SV *uni = sv_newmortal (); |
1013 | |
1230 | |
1014 | // horrible hack to silence warning inside pv_uni_display |
1231 | // horrible hack to silence warning inside pv_uni_display |
1015 | COP cop = *PL_curcop; |
1232 | COP cop = *PL_curcop; |
1016 | cop.cop_warnings = pWARN_NONE; |
1233 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1027 | } |
1244 | } |
1028 | |
1245 | |
1029 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
1030 | |
1247 | |
1031 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1032 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1249 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1033 | |
1250 | |
1034 | return sv; |
1251 | return sv; |
1035 | } |
1252 | } |
1036 | |
1253 | |
… | |
… | |
1040 | MODULE = JSON::XS PACKAGE = JSON::XS |
1257 | MODULE = JSON::XS PACKAGE = JSON::XS |
1041 | |
1258 | |
1042 | BOOT: |
1259 | BOOT: |
1043 | { |
1260 | { |
1044 | int i; |
1261 | int i; |
1045 | |
|
|
1046 | memset (decode_hexdigit, 0xff, 256); |
|
|
1047 | |
1262 | |
1048 | for (i = 0; i < 256; ++i) |
1263 | for (i = 0; i < 256; ++i) |
1049 | decode_hexdigit [i] = |
1264 | decode_hexdigit [i] = |
1050 | i >= '0' && i <= '9' ? i - '0' |
1265 | i >= '0' && i <= '9' ? i - '0' |
1051 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1266 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1052 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1267 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1053 | : -1; |
1268 | : -1; |
1054 | |
1269 | |
1055 | json_stash = gv_stashpv ("JSON::XS", 1); |
1270 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1271 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1272 | |
|
|
1273 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1056 | } |
1275 | } |
1057 | |
1276 | |
1058 | PROTOTYPES: DISABLE |
1277 | PROTOTYPES: DISABLE |
1059 | |
1278 | |
1060 | SV *new (char *dummy) |
1279 | SV *new (char *dummy) |
… | |
… | |
1063 | OUTPUT: |
1282 | OUTPUT: |
1064 | RETVAL |
1283 | RETVAL |
1065 | |
1284 | |
1066 | SV *ascii (SV *self, int enable = 1) |
1285 | SV *ascii (SV *self, int enable = 1) |
1067 | ALIAS: |
1286 | ALIAS: |
1068 | ascii = F_ASCII |
1287 | ascii = F_ASCII |
|
|
1288 | latin1 = F_LATIN1 |
1069 | utf8 = F_UTF8 |
1289 | utf8 = F_UTF8 |
1070 | indent = F_INDENT |
1290 | indent = F_INDENT |
1071 | canonical = F_CANONICAL |
1291 | canonical = F_CANONICAL |
1072 | space_before = F_SPACE_BEFORE |
1292 | space_before = F_SPACE_BEFORE |
1073 | space_after = F_SPACE_AFTER |
1293 | space_after = F_SPACE_AFTER |
1074 | pretty = F_PRETTY |
1294 | pretty = F_PRETTY |
1075 | allow_nonref = F_ALLOW_NONREF |
1295 | allow_nonref = F_ALLOW_NONREF |
1076 | shrink = F_SHRINK |
1296 | shrink = F_SHRINK |
|
|
1297 | allow_blessed = F_ALLOW_BLESSED |
|
|
1298 | convert_blessed = F_CONV_BLESSED |
1077 | CODE: |
1299 | CODE: |
1078 | { |
1300 | { |
1079 | UV *uv = SvJSON (self); |
1301 | UV *uv = SvJSON (self); |
1080 | if (enable) |
1302 | if (enable) |
1081 | *uv |= ix; |
1303 | *uv |= ix; |
… | |
… | |
1103 | RETVAL = newSVsv (self); |
1325 | RETVAL = newSVsv (self); |
1104 | } |
1326 | } |
1105 | OUTPUT: |
1327 | OUTPUT: |
1106 | RETVAL |
1328 | RETVAL |
1107 | |
1329 | |
|
|
1330 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1331 | CODE: |
|
|
1332 | { |
|
|
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
|
|
1335 | |
|
|
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
|
|
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1108 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1109 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1110 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1111 | |
1355 | |
1112 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1113 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1114 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
|
|
1362 | |
|
|
1363 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1364 | PPCODE: |
|
|
1365 | { |
|
|
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
|
|
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
|
|
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1371 | } |
1115 | |
1372 | |
1116 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1117 | |
1374 | |
1118 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1119 | ALIAS: |
|
|
1120 | objToJson = 0 |
|
|
1121 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1122 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1123 | |
1381 | |
1124 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1125 | ALIAS: |
|
|
1126 | jsonToObj = 0 |
|
|
1127 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1128 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1129 | |
1388 | |