1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
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 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
14 | // some old perls do not have this, try to make it work, no |
15 | // some old perls do not have this, try to make it work, no |
15 | // guarentees, though. |
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
16 | #ifndef UTF8_MAXBYTES |
17 | #ifndef UTF8_MAXBYTES |
17 | # define UTF8_MAXBYTES 13 |
18 | # define UTF8_MAXBYTES 13 |
18 | #endif |
19 | #endif |
19 | |
20 | |
20 | #define F_ASCII 0x00000001UL |
21 | #define F_ASCII 0x00000001UL |
21 | #define F_LATIN1 0x00000002UL |
22 | #define F_LATIN1 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
23 | #define F_UTF8 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
24 | #define F_INDENT 0x00000008UL |
24 | #define F_CANONICAL 0x00000010UL |
25 | #define F_CANONICAL 0x00000010UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
|
|
30 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
31 | #define F_CONV_BLESSED 0x00000800UL |
29 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define F_MAXDEPTH 0xf8000000UL |
30 | #define S_MAXDEPTH 27 |
33 | #define S_MAXDEPTH 27 |
|
|
34 | #define F_MAXSIZE 0x01f00000UL |
|
|
35 | #define S_MAXSIZE 20 |
|
|
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
31 | |
37 | |
32 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
33 | |
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
34 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
35 | // F_BLESSED? <=> { $__class__$ => } |
|
|
36 | |
40 | |
37 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
38 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
42 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
39 | |
43 | |
40 | #define INIT_SIZE 32 // initial scalar size to be allocated |
44 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
54 | #endif |
58 | #endif |
55 | |
59 | |
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
58 | |
62 | |
|
|
63 | #ifdef USE_ITHREADS |
|
|
64 | # define JSON_SLOW 1 |
|
|
65 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
66 | #else |
|
|
67 | # define JSON_SLOW 0 |
|
|
68 | # define JSON_STASH json_stash |
|
|
69 | #endif |
|
|
70 | |
59 | static HV *json_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
72 | static SV *json_true, *json_false; |
|
|
73 | |
|
|
74 | typedef struct { |
|
|
75 | U32 flags; |
|
|
76 | SV *cb_object; |
|
|
77 | HV *cb_sk_object; |
|
|
78 | } JSON; |
60 | |
79 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
80 | ///////////////////////////////////////////////////////////////////////////// |
62 | // utility functions |
81 | // utility functions |
63 | |
82 | |
64 | static UV * |
83 | inline void |
65 | SvJSON (SV *sv) |
|
|
66 | { |
|
|
67 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
68 | croak ("object is not of type JSON::XS"); |
|
|
69 | |
|
|
70 | return &SvUVX (SvRV (sv)); |
|
|
71 | } |
|
|
72 | |
|
|
73 | static void |
|
|
74 | shrink (SV *sv) |
84 | shrink (SV *sv) |
75 | { |
85 | { |
76 | sv_utf8_downgrade (sv, 1); |
86 | sv_utf8_downgrade (sv, 1); |
77 | if (SvLEN (sv) > SvCUR (sv) + 1) |
87 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | { |
88 | { |
… | |
… | |
113 | typedef struct |
123 | typedef struct |
114 | { |
124 | { |
115 | char *cur; // SvPVX (sv) + current output position |
125 | char *cur; // SvPVX (sv) + current output position |
116 | char *end; // SvEND (sv) |
126 | char *end; // SvEND (sv) |
117 | SV *sv; // result scalar |
127 | SV *sv; // result scalar |
118 | U32 flags; // F_* |
128 | JSON json; |
119 | U32 indent; // indentation level |
129 | U32 indent; // indentation level |
120 | U32 maxdepth; // max. indentation/recursion level |
130 | U32 maxdepth; // max. indentation/recursion level |
121 | } enc_t; |
131 | } enc_t; |
122 | |
132 | |
123 | inline void |
133 | inline void |
… | |
… | |
197 | } |
207 | } |
198 | |
208 | |
199 | if (uch > 0x10FFFFUL) |
209 | if (uch > 0x10FFFFUL) |
200 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
210 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
201 | |
211 | |
202 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
212 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
203 | { |
213 | { |
204 | if (uch > 0xFFFFUL) |
214 | if (uch > 0xFFFFUL) |
205 | { |
215 | { |
206 | need (enc, len += 11); |
216 | need (enc, len += 11); |
207 | sprintf (enc->cur, "\\u%04x\\u%04x", |
217 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
221 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
231 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | } |
232 | } |
223 | |
233 | |
224 | str += clen; |
234 | str += clen; |
225 | } |
235 | } |
226 | else if (enc->flags & F_LATIN1) |
236 | else if (enc->json.flags & F_LATIN1) |
227 | { |
237 | { |
228 | *enc->cur++ = uch; |
238 | *enc->cur++ = uch; |
229 | str += clen; |
239 | str += clen; |
230 | } |
240 | } |
231 | else if (is_utf8) |
241 | else if (is_utf8) |
… | |
… | |
252 | } |
262 | } |
253 | |
263 | |
254 | inline void |
264 | inline void |
255 | encode_indent (enc_t *enc) |
265 | encode_indent (enc_t *enc) |
256 | { |
266 | { |
257 | if (enc->flags & F_INDENT) |
267 | if (enc->json.flags & F_INDENT) |
258 | { |
268 | { |
259 | int spaces = enc->indent * INDENT_STEP; |
269 | int spaces = enc->indent * INDENT_STEP; |
260 | |
270 | |
261 | need (enc, spaces); |
271 | need (enc, spaces); |
262 | memset (enc->cur, ' ', spaces); |
272 | memset (enc->cur, ' ', spaces); |
… | |
… | |
272 | } |
282 | } |
273 | |
283 | |
274 | inline void |
284 | inline void |
275 | encode_nl (enc_t *enc) |
285 | encode_nl (enc_t *enc) |
276 | { |
286 | { |
277 | if (enc->flags & F_INDENT) |
287 | if (enc->json.flags & F_INDENT) |
278 | { |
288 | { |
279 | need (enc, 1); |
289 | need (enc, 1); |
280 | encode_ch (enc, '\n'); |
290 | encode_ch (enc, '\n'); |
281 | } |
291 | } |
282 | } |
292 | } |
… | |
… | |
284 | inline void |
294 | inline void |
285 | encode_comma (enc_t *enc) |
295 | encode_comma (enc_t *enc) |
286 | { |
296 | { |
287 | encode_ch (enc, ','); |
297 | encode_ch (enc, ','); |
288 | |
298 | |
289 | if (enc->flags & F_INDENT) |
299 | if (enc->json.flags & F_INDENT) |
290 | encode_nl (enc); |
300 | encode_nl (enc); |
291 | else if (enc->flags & F_SPACE_AFTER) |
301 | else if (enc->json.flags & F_SPACE_AFTER) |
292 | encode_space (enc); |
302 | encode_space (enc); |
293 | } |
303 | } |
294 | |
304 | |
295 | static void encode_sv (enc_t *enc, SV *sv); |
305 | static void encode_sv (enc_t *enc, SV *sv); |
296 | |
306 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
315 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
316 | ++enc->indent; |
307 | |
317 | |
308 | for (i = 0; i <= len; ++i) |
318 | for (i = 0; i <= len; ++i) |
309 | { |
319 | { |
|
|
320 | SV **svp = av_fetch (av, i, 0); |
|
|
321 | |
310 | encode_indent (enc); |
322 | encode_indent (enc); |
311 | encode_sv (enc, *av_fetch (av, i, 0)); |
323 | |
|
|
324 | if (svp) |
|
|
325 | encode_sv (enc, *svp); |
|
|
326 | else |
|
|
327 | encode_str (enc, "null", 4, 0); |
312 | |
328 | |
313 | if (i < len) |
329 | if (i < len) |
314 | encode_comma (enc); |
330 | encode_comma (enc); |
315 | } |
331 | } |
316 | |
332 | |
… | |
… | |
319 | --enc->indent; |
335 | --enc->indent; |
320 | encode_indent (enc); encode_ch (enc, ']'); |
336 | encode_indent (enc); encode_ch (enc, ']'); |
321 | } |
337 | } |
322 | |
338 | |
323 | static void |
339 | static void |
324 | encode_he (enc_t *enc, HE *he) |
340 | encode_hk (enc_t *enc, HE *he) |
325 | { |
341 | { |
326 | encode_ch (enc, '"'); |
342 | encode_ch (enc, '"'); |
327 | |
343 | |
328 | if (HeKLEN (he) == HEf_SVKEY) |
344 | if (HeKLEN (he) == HEf_SVKEY) |
329 | { |
345 | { |
… | |
… | |
339 | else |
355 | else |
340 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
356 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
341 | |
357 | |
342 | encode_ch (enc, '"'); |
358 | encode_ch (enc, '"'); |
343 | |
359 | |
344 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
360 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
361 | encode_ch (enc, ':'); |
346 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
362 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
|
|
348 | } |
363 | } |
349 | |
364 | |
350 | // compare hash entries, used when all keys are bytestrings |
365 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
366 | static int |
352 | he_cmp_fast (const void *a_, const void *b_) |
367 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
357 | HE *b = *(HE **)b_; |
372 | HE *b = *(HE **)b_; |
358 | |
373 | |
359 | STRLEN la = HeKLEN (a); |
374 | STRLEN la = HeKLEN (a); |
360 | STRLEN lb = HeKLEN (b); |
375 | STRLEN lb = HeKLEN (b); |
361 | |
376 | |
362 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
377 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
363 | cmp = la - lb; |
378 | cmp = lb - la; |
364 | |
379 | |
365 | return cmp; |
380 | return cmp; |
366 | } |
381 | } |
367 | |
382 | |
368 | // compare hash entries, used when some keys are sv's or utf-x |
383 | // compare hash entries, used when some keys are sv's or utf-x |
369 | static int |
384 | static int |
370 | he_cmp_slow (const void *a, const void *b) |
385 | he_cmp_slow (const void *a, const void *b) |
371 | { |
386 | { |
372 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
387 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
373 | } |
388 | } |
374 | |
389 | |
375 | static void |
390 | static void |
376 | encode_hv (enc_t *enc, HV *hv) |
391 | encode_hv (enc_t *enc, HV *hv) |
377 | { |
392 | { |
|
|
393 | HE *he; |
378 | int count, i; |
394 | int count; |
379 | |
395 | |
380 | if (enc->indent >= enc->maxdepth) |
396 | if (enc->indent >= enc->maxdepth) |
381 | croak ("data structure too deep (hit recursion limit)"); |
397 | croak ("data structure too deep (hit recursion limit)"); |
382 | |
398 | |
383 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
399 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
384 | |
400 | |
385 | if ((count = hv_iterinit (hv))) |
|
|
386 | { |
|
|
387 | // for canonical output we have to sort by keys first |
401 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
402 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
403 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
404 | // that randomises hash orderings |
391 | if (enc->flags & F_CANONICAL) |
405 | if (enc->json.flags & F_CANONICAL) |
|
|
406 | { |
|
|
407 | int count = hv_iterinit (hv); |
|
|
408 | |
|
|
409 | if (SvMAGICAL (hv)) |
392 | { |
410 | { |
|
|
411 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
412 | // but I don't care for the speed of this special case. |
|
|
413 | // note also that we will run into undefined behaviour when the two iterations |
|
|
414 | // do not result in the same count, something I might care for in some later release. |
|
|
415 | |
|
|
416 | count = 0; |
|
|
417 | while (hv_iternext (hv)) |
|
|
418 | ++count; |
|
|
419 | |
|
|
420 | hv_iterinit (hv); |
|
|
421 | } |
|
|
422 | |
|
|
423 | if (count) |
|
|
424 | { |
393 | int fast = 1; |
425 | int i, fast = 1; |
394 | HE *he; |
|
|
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
426 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
427 | HE **hes = _alloca (count * sizeof (HE)); |
397 | #else |
428 | #else |
398 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
429 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
399 | #endif |
430 | #endif |
… | |
… | |
426 | |
457 | |
427 | FREETMPS; |
458 | FREETMPS; |
428 | LEAVE; |
459 | LEAVE; |
429 | } |
460 | } |
430 | |
461 | |
431 | for (i = 0; i < count; ++i) |
462 | while (count--) |
432 | { |
463 | { |
433 | encode_indent (enc); |
464 | encode_indent (enc); |
|
|
465 | he = hes [count]; |
434 | encode_he (enc, hes [i]); |
466 | encode_hk (enc, he); |
|
|
467 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
435 | |
468 | |
436 | if (i < count - 1) |
469 | if (count) |
437 | encode_comma (enc); |
470 | encode_comma (enc); |
438 | } |
471 | } |
439 | |
472 | } |
|
|
473 | } |
|
|
474 | else |
|
|
475 | { |
|
|
476 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
477 | if ((he = hv_iternext (hv))) |
|
|
478 | for (;;) |
|
|
479 | { |
440 | encode_nl (enc); |
480 | encode_indent (enc); |
|
|
481 | encode_hk (enc, he); |
|
|
482 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
483 | |
|
|
484 | if (!(he = hv_iternext (hv))) |
|
|
485 | break; |
|
|
486 | |
|
|
487 | encode_comma (enc); |
|
|
488 | } |
|
|
489 | } |
|
|
490 | |
|
|
491 | encode_nl (enc); |
|
|
492 | |
|
|
493 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
494 | } |
|
|
495 | |
|
|
496 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
497 | static void |
|
|
498 | encode_rv (enc_t *enc, SV *sv) |
|
|
499 | { |
|
|
500 | svtype svt; |
|
|
501 | |
|
|
502 | SvGETMAGIC (sv); |
|
|
503 | svt = SvTYPE (sv); |
|
|
504 | |
|
|
505 | if (expect_false (SvOBJECT (sv))) |
|
|
506 | { |
|
|
507 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
508 | ? json_boolean_stash |
|
|
509 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
510 | |
|
|
511 | if (SvSTASH (sv) == stash) |
|
|
512 | { |
|
|
513 | if (SvIV (sv)) |
|
|
514 | encode_str (enc, "true", 4, 0); |
|
|
515 | else |
|
|
516 | encode_str (enc, "false", 5, 0); |
441 | } |
517 | } |
442 | else |
518 | else |
443 | { |
519 | { |
444 | HE *he = hv_iternext (hv); |
520 | #if 0 |
445 | |
521 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
446 | for (;;) |
|
|
447 | { |
|
|
448 | encode_indent (enc); |
|
|
449 | encode_he (enc, he); |
|
|
450 | |
|
|
451 | if (!(he = hv_iternext (hv))) |
|
|
452 | break; |
|
|
453 | |
|
|
454 | encode_comma (enc); |
|
|
455 | } |
522 | { |
|
|
523 | // not yet |
|
|
524 | } |
|
|
525 | #endif |
|
|
526 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
527 | { |
|
|
528 | // we re-bless the reference to get overload and other niceties right |
|
|
529 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
456 | |
530 | |
|
|
531 | if (to_json) |
|
|
532 | { |
|
|
533 | dSP; |
|
|
534 | |
|
|
535 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
536 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
537 | |
|
|
538 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
539 | PUTBACK; |
|
|
540 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
541 | SPAGAIN; |
|
|
542 | |
|
|
543 | // catch this surprisingly common error |
|
|
544 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
545 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
546 | |
|
|
547 | sv = POPs; |
|
|
548 | PUTBACK; |
|
|
549 | |
457 | encode_nl (enc); |
550 | encode_sv (enc, sv); |
|
|
551 | |
|
|
552 | FREETMPS; LEAVE; |
|
|
553 | } |
|
|
554 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
555 | encode_str (enc, "null", 4, 0); |
|
|
556 | else |
|
|
557 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
558 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
559 | } |
|
|
560 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
561 | encode_str (enc, "null", 4, 0); |
|
|
562 | else |
|
|
563 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
564 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
458 | } |
565 | } |
459 | } |
566 | } |
460 | |
|
|
461 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
462 | } |
|
|
463 | |
|
|
464 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
465 | static void |
|
|
466 | encode_rv (enc_t *enc, SV *sv) |
|
|
467 | { |
|
|
468 | svtype svt; |
|
|
469 | |
|
|
470 | SvGETMAGIC (sv); |
|
|
471 | svt = SvTYPE (sv); |
|
|
472 | |
|
|
473 | if (svt == SVt_PVHV) |
567 | else if (svt == SVt_PVHV) |
474 | encode_hv (enc, (HV *)sv); |
568 | encode_hv (enc, (HV *)sv); |
475 | else if (svt == SVt_PVAV) |
569 | else if (svt == SVt_PVAV) |
476 | encode_av (enc, (AV *)sv); |
570 | encode_av (enc, (AV *)sv); |
477 | else if (svt < SVt_PVAV) |
571 | else if (svt < SVt_PVAV) |
478 | { |
572 | { |
479 | if (SvNIOK (sv) && SvIV (sv) == 0) |
573 | STRLEN len = 0; |
|
|
574 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
575 | |
|
|
576 | if (len == 1 && *pv == '1') |
|
|
577 | encode_str (enc, "true", 4, 0); |
|
|
578 | else if (len == 1 && *pv == '0') |
480 | encode_str (enc, "false", 5, 0); |
579 | encode_str (enc, "false", 5, 0); |
481 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
482 | encode_str (enc, "true", 4, 0); |
|
|
483 | else |
580 | else |
484 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
581 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
485 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
582 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
486 | } |
583 | } |
487 | else |
584 | else |
… | |
… | |
557 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
654 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
558 | SvPV_nolen (sv), SvFLAGS (sv)); |
655 | SvPV_nolen (sv), SvFLAGS (sv)); |
559 | } |
656 | } |
560 | |
657 | |
561 | static SV * |
658 | static SV * |
562 | encode_json (SV *scalar, U32 flags) |
659 | encode_json (SV *scalar, JSON *json) |
563 | { |
660 | { |
564 | enc_t enc; |
661 | enc_t enc; |
565 | |
662 | |
566 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
663 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
567 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
664 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
568 | |
665 | |
569 | enc.flags = flags; |
666 | enc.json = *json; |
570 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
667 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
571 | enc.cur = SvPVX (enc.sv); |
668 | enc.cur = SvPVX (enc.sv); |
572 | enc.end = SvEND (enc.sv); |
669 | enc.end = SvEND (enc.sv); |
573 | enc.indent = 0; |
670 | enc.indent = 0; |
574 | enc.maxdepth = DEC_DEPTH (flags); |
671 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
575 | |
672 | |
576 | SvPOK_only (enc.sv); |
673 | SvPOK_only (enc.sv); |
577 | encode_sv (&enc, scalar); |
674 | encode_sv (&enc, scalar); |
578 | |
675 | |
579 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
676 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
580 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
677 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
581 | |
678 | |
582 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
679 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
583 | SvUTF8_on (enc.sv); |
680 | SvUTF8_on (enc.sv); |
584 | |
681 | |
585 | if (enc.flags & F_SHRINK) |
682 | if (enc.json.flags & F_SHRINK) |
586 | shrink (enc.sv); |
683 | shrink (enc.sv); |
587 | |
684 | |
588 | return enc.sv; |
685 | return enc.sv; |
589 | } |
686 | } |
590 | |
687 | |
… | |
… | |
595 | typedef struct |
692 | typedef struct |
596 | { |
693 | { |
597 | char *cur; // current parser pointer |
694 | char *cur; // current parser pointer |
598 | char *end; // end of input string |
695 | char *end; // end of input string |
599 | const char *err; // parse error, if != 0 |
696 | const char *err; // parse error, if != 0 |
600 | U32 flags; // F_* |
697 | JSON json; |
601 | U32 depth; // recursion depth |
698 | U32 depth; // recursion depth |
602 | U32 maxdepth; // recursion depth limit |
699 | U32 maxdepth; // recursion depth limit |
603 | } dec_t; |
700 | } dec_t; |
604 | |
701 | |
605 | inline void |
702 | inline void |
… | |
… | |
868 | is_nv = 1; |
965 | is_nv = 1; |
869 | } |
966 | } |
870 | |
967 | |
871 | if (!is_nv) |
968 | if (!is_nv) |
872 | { |
969 | { |
|
|
970 | int len = dec->cur - start; |
|
|
971 | |
873 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
972 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
874 | if (*start == '-') |
973 | if (*start == '-') |
875 | switch (dec->cur - start) |
974 | switch (len) |
876 | { |
975 | { |
877 | case 2: return newSViv (-( start [1] - '0' * 1)); |
976 | case 2: return newSViv (-( start [1] - '0' * 1)); |
878 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
977 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
879 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
978 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
880 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
979 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
881 | } |
980 | } |
882 | else |
981 | else |
883 | switch (dec->cur - start) |
982 | switch (len) |
884 | { |
983 | { |
885 | case 1: return newSViv ( start [0] - '0' * 1); |
984 | case 1: return newSViv ( start [0] - '0' * 1); |
886 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
985 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
887 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
986 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
888 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
987 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
889 | } |
988 | } |
890 | |
989 | |
891 | { |
990 | { |
892 | UV uv; |
991 | UV uv; |
893 | int numtype = grok_number (start, dec->cur - start, &uv); |
992 | int numtype = grok_number (start, len, &uv); |
894 | if (numtype & IS_NUMBER_IN_UV) |
993 | if (numtype & IS_NUMBER_IN_UV) |
895 | if (numtype & IS_NUMBER_NEG) |
994 | if (numtype & IS_NUMBER_NEG) |
896 | { |
995 | { |
897 | if (uv < (UV)IV_MIN) |
996 | if (uv < (UV)IV_MIN) |
898 | return newSViv (-(IV)uv); |
997 | return newSViv (-(IV)uv); |
899 | } |
998 | } |
900 | else |
999 | else |
901 | return newSVuv (uv); |
1000 | return newSVuv (uv); |
902 | |
|
|
903 | // here would likely be the place for bigint support |
|
|
904 | } |
1001 | } |
905 | } |
|
|
906 | |
1002 | |
907 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1003 | len -= *start == '-' ? 1 : 0; |
|
|
1004 | |
|
|
1005 | // does not fit into IV or UV, try NV |
|
|
1006 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1007 | #if defined (LDBL_DIG) |
|
|
1008 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1009 | #endif |
|
|
1010 | ) |
|
|
1011 | // fits into NV without loss of precision |
|
|
1012 | return newSVnv (Atof (start)); |
|
|
1013 | |
|
|
1014 | // everything else fails, convert it to a string |
|
|
1015 | return newSVpvn (start, dec->cur - start); |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | // loss of precision here |
908 | return newSVnv (Atof (start)); |
1019 | return newSVnv (Atof (start)); |
909 | |
1020 | |
910 | fail: |
1021 | fail: |
911 | return 0; |
1022 | return 0; |
912 | } |
1023 | } |
… | |
… | |
956 | } |
1067 | } |
957 | |
1068 | |
958 | static SV * |
1069 | static SV * |
959 | decode_hv (dec_t *dec) |
1070 | decode_hv (dec_t *dec) |
960 | { |
1071 | { |
|
|
1072 | SV *sv; |
961 | HV *hv = newHV (); |
1073 | HV *hv = newHV (); |
962 | |
1074 | |
963 | DEC_INC_DEPTH; |
1075 | DEC_INC_DEPTH; |
964 | decode_ws (dec); |
1076 | decode_ws (dec); |
965 | |
1077 | |
966 | if (*dec->cur == '}') |
1078 | if (*dec->cur == '}') |
967 | ++dec->cur; |
1079 | ++dec->cur; |
968 | else |
1080 | else |
969 | for (;;) |
1081 | for (;;) |
970 | { |
1082 | { |
971 | SV *key, *value; |
|
|
972 | |
|
|
973 | decode_ws (dec); EXPECT_CH ('"'); |
1083 | decode_ws (dec); EXPECT_CH ('"'); |
974 | |
1084 | |
975 | key = decode_str (dec); |
1085 | // heuristic: assume that |
976 | if (!key) |
1086 | // a) decode_str + hv_store_ent are abysmally slow. |
977 | goto fail; |
1087 | // b) most hash keys are short, simple ascii text. |
|
|
1088 | // => try to "fast-match" such strings to avoid |
|
|
1089 | // the overhead of decode_str + hv_store_ent. |
|
|
1090 | { |
|
|
1091 | SV *value; |
|
|
1092 | char *p = dec->cur; |
|
|
1093 | char *e = p + 24; // only try up to 24 bytes |
978 | |
1094 | |
979 | decode_ws (dec); EXPECT_CH (':'); |
1095 | for (;;) |
980 | |
|
|
981 | value = decode_sv (dec); |
|
|
982 | if (!value) |
|
|
983 | { |
1096 | { |
|
|
1097 | // the >= 0x80 is true on most architectures |
|
|
1098 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1099 | { |
|
|
1100 | // slow path, back up and use decode_str |
|
|
1101 | SV *key = decode_str (dec); |
|
|
1102 | if (!key) |
|
|
1103 | goto fail; |
|
|
1104 | |
|
|
1105 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1106 | |
|
|
1107 | value = decode_sv (dec); |
|
|
1108 | if (!value) |
|
|
1109 | { |
|
|
1110 | SvREFCNT_dec (key); |
|
|
1111 | goto fail; |
|
|
1112 | } |
|
|
1113 | |
|
|
1114 | hv_store_ent (hv, key, value, 0); |
984 | SvREFCNT_dec (key); |
1115 | SvREFCNT_dec (key); |
|
|
1116 | |
|
|
1117 | break; |
|
|
1118 | } |
|
|
1119 | else if (*p == '"') |
|
|
1120 | { |
|
|
1121 | // fast path, got a simple key |
|
|
1122 | char *key = dec->cur; |
|
|
1123 | int len = p - key; |
|
|
1124 | dec->cur = p + 1; |
|
|
1125 | |
|
|
1126 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1127 | |
|
|
1128 | value = decode_sv (dec); |
|
|
1129 | if (!value) |
985 | goto fail; |
1130 | goto fail; |
|
|
1131 | |
|
|
1132 | hv_store (hv, key, len, value, 0); |
|
|
1133 | |
|
|
1134 | break; |
|
|
1135 | } |
|
|
1136 | |
|
|
1137 | ++p; |
986 | } |
1138 | } |
987 | |
1139 | } |
988 | hv_store_ent (hv, key, value, 0); |
|
|
989 | SvREFCNT_dec (key); |
|
|
990 | |
1140 | |
991 | decode_ws (dec); |
1141 | decode_ws (dec); |
992 | |
1142 | |
993 | if (*dec->cur == '}') |
1143 | if (*dec->cur == '}') |
994 | { |
1144 | { |
… | |
… | |
1001 | |
1151 | |
1002 | ++dec->cur; |
1152 | ++dec->cur; |
1003 | } |
1153 | } |
1004 | |
1154 | |
1005 | DEC_DEC_DEPTH; |
1155 | DEC_DEC_DEPTH; |
1006 | return newRV_noinc ((SV *)hv); |
1156 | sv = newRV_noinc ((SV *)hv); |
|
|
1157 | |
|
|
1158 | // check filter callbacks |
|
|
1159 | if (dec->json.flags & F_HOOK) |
|
|
1160 | { |
|
|
1161 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1162 | { |
|
|
1163 | HE *cb, *he; |
|
|
1164 | |
|
|
1165 | hv_iterinit (hv); |
|
|
1166 | he = hv_iternext (hv); |
|
|
1167 | hv_iterinit (hv); |
|
|
1168 | |
|
|
1169 | // the next line creates a mortal sv each time its called. |
|
|
1170 | // might want to optimise this for common cases. |
|
|
1171 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1172 | |
|
|
1173 | if (cb) |
|
|
1174 | { |
|
|
1175 | dSP; |
|
|
1176 | int count; |
|
|
1177 | |
|
|
1178 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1179 | XPUSHs (HeVAL (he)); |
|
|
1180 | |
|
|
1181 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1182 | |
|
|
1183 | if (count == 1) |
|
|
1184 | { |
|
|
1185 | sv = newSVsv (POPs); |
|
|
1186 | FREETMPS; LEAVE; |
|
|
1187 | return sv; |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | FREETMPS; LEAVE; |
|
|
1191 | } |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | if (dec->json.cb_object) |
|
|
1195 | { |
|
|
1196 | dSP; |
|
|
1197 | int count; |
|
|
1198 | |
|
|
1199 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1200 | XPUSHs (sv_2mortal (sv)); |
|
|
1201 | |
|
|
1202 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1203 | |
|
|
1204 | if (count == 1) |
|
|
1205 | { |
|
|
1206 | sv = newSVsv (POPs); |
|
|
1207 | FREETMPS; LEAVE; |
|
|
1208 | return sv; |
|
|
1209 | } |
|
|
1210 | |
|
|
1211 | SvREFCNT_inc (sv); |
|
|
1212 | FREETMPS; LEAVE; |
|
|
1213 | } |
|
|
1214 | } |
|
|
1215 | |
|
|
1216 | return sv; |
1007 | |
1217 | |
1008 | fail: |
1218 | fail: |
1009 | SvREFCNT_dec (hv); |
1219 | SvREFCNT_dec (hv); |
1010 | DEC_DEC_DEPTH; |
1220 | DEC_DEC_DEPTH; |
1011 | return 0; |
1221 | return 0; |
… | |
… | |
1031 | |
1241 | |
1032 | case 't': |
1242 | case 't': |
1033 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1243 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1034 | { |
1244 | { |
1035 | dec->cur += 4; |
1245 | dec->cur += 4; |
1036 | return newSViv (1); |
1246 | #if JSON_SLOW |
|
|
1247 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1248 | #endif |
|
|
1249 | return SvREFCNT_inc (json_true); |
1037 | } |
1250 | } |
1038 | else |
1251 | else |
1039 | ERR ("'true' expected"); |
1252 | ERR ("'true' expected"); |
1040 | |
1253 | |
1041 | break; |
1254 | break; |
1042 | |
1255 | |
1043 | case 'f': |
1256 | case 'f': |
1044 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1257 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1045 | { |
1258 | { |
1046 | dec->cur += 5; |
1259 | dec->cur += 5; |
1047 | return newSViv (0); |
1260 | #if JSON_SLOW |
|
|
1261 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1262 | #endif |
|
|
1263 | return SvREFCNT_inc (json_false); |
1048 | } |
1264 | } |
1049 | else |
1265 | else |
1050 | ERR ("'false' expected"); |
1266 | ERR ("'false' expected"); |
1051 | |
1267 | |
1052 | break; |
1268 | break; |
… | |
… | |
1070 | fail: |
1286 | fail: |
1071 | return 0; |
1287 | return 0; |
1072 | } |
1288 | } |
1073 | |
1289 | |
1074 | static SV * |
1290 | static SV * |
1075 | decode_json (SV *string, U32 flags, UV *offset_return) |
1291 | decode_json (SV *string, JSON *json, UV *offset_return) |
1076 | { |
1292 | { |
1077 | dec_t dec; |
1293 | dec_t dec; |
1078 | UV offset; |
1294 | UV offset; |
1079 | SV *sv; |
1295 | SV *sv; |
1080 | |
1296 | |
1081 | SvGETMAGIC (string); |
1297 | SvGETMAGIC (string); |
1082 | SvUPGRADE (string, SVt_PV); |
1298 | SvUPGRADE (string, SVt_PV); |
1083 | |
1299 | |
|
|
1300 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1301 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1302 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1303 | |
1084 | if (flags & F_UTF8) |
1304 | if (json->flags & F_UTF8) |
1085 | sv_utf8_downgrade (string, 0); |
1305 | sv_utf8_downgrade (string, 0); |
1086 | else |
1306 | else |
1087 | sv_utf8_upgrade (string); |
1307 | sv_utf8_upgrade (string); |
1088 | |
1308 | |
1089 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1309 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1090 | |
1310 | |
1091 | dec.flags = flags; |
1311 | dec.json = *json; |
1092 | dec.cur = SvPVX (string); |
1312 | dec.cur = SvPVX (string); |
1093 | dec.end = SvEND (string); |
1313 | dec.end = SvEND (string); |
1094 | dec.err = 0; |
1314 | dec.err = 0; |
1095 | dec.depth = 0; |
1315 | dec.depth = 0; |
1096 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1316 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1317 | |
|
|
1318 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1319 | dec.json.flags |= F_HOOK; |
1097 | |
1320 | |
1098 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1321 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1099 | sv = decode_sv (&dec); |
1322 | sv = decode_sv (&dec); |
1100 | |
1323 | |
1101 | if (!(offset_return || !sv)) |
1324 | if (!(offset_return || !sv)) |
… | |
… | |
1111 | } |
1334 | } |
1112 | } |
1335 | } |
1113 | |
1336 | |
1114 | if (offset_return || !sv) |
1337 | if (offset_return || !sv) |
1115 | { |
1338 | { |
1116 | offset = dec.flags & F_UTF8 |
1339 | offset = dec.json.flags & F_UTF8 |
1117 | ? dec.cur - SvPVX (string) |
1340 | ? dec.cur - SvPVX (string) |
1118 | : utf8_distance (dec.cur, SvPVX (string)); |
1341 | : utf8_distance (dec.cur, SvPVX (string)); |
1119 | |
1342 | |
1120 | if (offset_return) |
1343 | if (offset_return) |
1121 | *offset_return = offset; |
1344 | *offset_return = offset; |
… | |
… | |
1140 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1363 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1141 | } |
1364 | } |
1142 | |
1365 | |
1143 | sv = sv_2mortal (sv); |
1366 | sv = sv_2mortal (sv); |
1144 | |
1367 | |
1145 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1368 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1146 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1369 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1147 | |
1370 | |
1148 | return sv; |
1371 | return sv; |
1149 | } |
1372 | } |
1150 | |
1373 | |
… | |
… | |
1162 | i >= '0' && i <= '9' ? i - '0' |
1385 | i >= '0' && i <= '9' ? i - '0' |
1163 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1386 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1164 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1387 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1165 | : -1; |
1388 | : -1; |
1166 | |
1389 | |
1167 | json_stash = gv_stashpv ("JSON::XS", 1); |
1390 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1391 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1392 | |
|
|
1393 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1394 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1168 | } |
1395 | } |
1169 | |
1396 | |
1170 | PROTOTYPES: DISABLE |
1397 | PROTOTYPES: DISABLE |
1171 | |
1398 | |
1172 | SV *new (char *dummy) |
1399 | void CLONE (...) |
1173 | CODE: |
1400 | CODE: |
1174 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1401 | json_stash = 0; |
1175 | OUTPUT: |
1402 | json_boolean_stash = 0; |
1176 | RETVAL |
|
|
1177 | |
1403 | |
|
|
1404 | void new (char *klass) |
|
|
1405 | PPCODE: |
|
|
1406 | { |
|
|
1407 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1408 | SvPOK_only (pv); |
|
|
1409 | Zero (SvPVX (pv), 1, JSON); |
|
|
1410 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1411 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
|
|
1412 | } |
|
|
1413 | |
1178 | SV *ascii (SV *self, int enable = 1) |
1414 | void ascii (JSON *self, int enable = 1) |
1179 | ALIAS: |
1415 | ALIAS: |
1180 | ascii = F_ASCII |
1416 | ascii = F_ASCII |
1181 | latin1 = F_LATIN1 |
1417 | latin1 = F_LATIN1 |
1182 | utf8 = F_UTF8 |
1418 | utf8 = F_UTF8 |
1183 | indent = F_INDENT |
1419 | indent = F_INDENT |
1184 | canonical = F_CANONICAL |
1420 | canonical = F_CANONICAL |
1185 | space_before = F_SPACE_BEFORE |
1421 | space_before = F_SPACE_BEFORE |
1186 | space_after = F_SPACE_AFTER |
1422 | space_after = F_SPACE_AFTER |
1187 | pretty = F_PRETTY |
1423 | pretty = F_PRETTY |
1188 | allow_nonref = F_ALLOW_NONREF |
1424 | allow_nonref = F_ALLOW_NONREF |
1189 | shrink = F_SHRINK |
1425 | shrink = F_SHRINK |
|
|
1426 | allow_blessed = F_ALLOW_BLESSED |
|
|
1427 | convert_blessed = F_CONV_BLESSED |
1190 | CODE: |
1428 | PPCODE: |
1191 | { |
1429 | { |
1192 | UV *uv = SvJSON (self); |
|
|
1193 | if (enable) |
1430 | if (enable) |
1194 | *uv |= ix; |
1431 | self->flags |= ix; |
1195 | else |
1432 | else |
1196 | *uv &= ~ix; |
1433 | self->flags &= ~ix; |
1197 | |
1434 | |
1198 | RETVAL = newSVsv (self); |
1435 | XPUSHs (ST (0)); |
1199 | } |
1436 | } |
1200 | OUTPUT: |
|
|
1201 | RETVAL |
|
|
1202 | |
1437 | |
1203 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1438 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1204 | CODE: |
1439 | PPCODE: |
1205 | { |
1440 | { |
1206 | UV *uv = SvJSON (self); |
|
|
1207 | UV log2 = 0; |
1441 | UV log2 = 0; |
1208 | |
1442 | |
1209 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1443 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1210 | |
1444 | |
1211 | while ((1UL << log2) < max_depth) |
1445 | while ((1UL << log2) < max_depth) |
1212 | ++log2; |
1446 | ++log2; |
1213 | |
1447 | |
1214 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1448 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1215 | |
1449 | |
1216 | RETVAL = newSVsv (self); |
1450 | XPUSHs (ST (0)); |
1217 | } |
1451 | } |
1218 | OUTPUT: |
|
|
1219 | RETVAL |
|
|
1220 | |
1452 | |
1221 | void encode (SV *self, SV *scalar) |
1453 | void max_size (JSON *self, UV max_size = 0) |
1222 | PPCODE: |
1454 | PPCODE: |
1223 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1455 | { |
|
|
1456 | UV log2 = 0; |
1224 | |
1457 | |
1225 | void decode (SV *self, SV *jsonstr) |
1458 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1459 | if (max_size == 1) max_size = 2; |
|
|
1460 | |
|
|
1461 | while ((1UL << log2) < max_size) |
|
|
1462 | ++log2; |
|
|
1463 | |
|
|
1464 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1465 | |
|
|
1466 | XPUSHs (ST (0)); |
|
|
1467 | } |
|
|
1468 | |
|
|
1469 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1226 | PPCODE: |
1470 | PPCODE: |
|
|
1471 | { |
|
|
1472 | SvREFCNT_dec (self->cb_object); |
|
|
1473 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1474 | |
|
|
1475 | XPUSHs (ST (0)); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1479 | PPCODE: |
|
|
1480 | { |
|
|
1481 | if (!self->cb_sk_object) |
|
|
1482 | self->cb_sk_object = newHV (); |
|
|
1483 | |
|
|
1484 | if (SvOK (cb)) |
|
|
1485 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1486 | else |
|
|
1487 | { |
|
|
1488 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1489 | |
|
|
1490 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1491 | { |
|
|
1492 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1493 | self->cb_sk_object = 0; |
|
|
1494 | } |
|
|
1495 | } |
|
|
1496 | |
|
|
1497 | XPUSHs (ST (0)); |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | void encode (JSON *self, SV *scalar) |
|
|
1501 | PPCODE: |
|
|
1502 | XPUSHs (encode_json (scalar, self)); |
|
|
1503 | |
|
|
1504 | void decode (JSON *self, SV *jsonstr) |
|
|
1505 | PPCODE: |
1227 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1506 | XPUSHs (decode_json (jsonstr, self, 0)); |
1228 | |
1507 | |
1229 | void decode_prefix (SV *self, SV *jsonstr) |
1508 | void decode_prefix (JSON *self, SV *jsonstr) |
1230 | PPCODE: |
1509 | PPCODE: |
1231 | { |
1510 | { |
1232 | UV offset; |
1511 | UV offset; |
1233 | EXTEND (SP, 2); |
1512 | EXTEND (SP, 2); |
1234 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1513 | PUSHs (decode_json (jsonstr, self, &offset)); |
1235 | PUSHs (sv_2mortal (newSVuv (offset))); |
1514 | PUSHs (sv_2mortal (newSVuv (offset))); |
1236 | } |
1515 | } |
1237 | |
1516 | |
|
|
1517 | void DESTROY (JSON *self) |
|
|
1518 | CODE: |
|
|
1519 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1520 | SvREFCNT_dec (self->cb_object); |
|
|
1521 | |
1238 | PROTOTYPES: ENABLE |
1522 | PROTOTYPES: ENABLE |
1239 | |
1523 | |
1240 | void to_json (SV *scalar) |
1524 | void to_json (SV *scalar) |
1241 | ALIAS: |
|
|
1242 | objToJson = 0 |
|
|
1243 | PPCODE: |
1525 | PPCODE: |
|
|
1526 | { |
|
|
1527 | JSON json = { F_DEFAULT | F_UTF8 }; |
1244 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1528 | XPUSHs (encode_json (scalar, &json)); |
|
|
1529 | } |
1245 | |
1530 | |
1246 | void from_json (SV *jsonstr) |
1531 | void from_json (SV *jsonstr) |
1247 | ALIAS: |
|
|
1248 | jsonToObj = 0 |
|
|
1249 | PPCODE: |
1532 | PPCODE: |
|
|
1533 | { |
|
|
1534 | JSON json = { F_DEFAULT | F_UTF8 }; |
1250 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1535 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1536 | } |
1251 | |
1537 | |