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 | |
… | |
… | |
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 |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define F_MAXDEPTH 0xf8000000UL |
32 | #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 |
33 | |
37 | |
34 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
35 | |
40 | |
36 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
37 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
42 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
38 | |
43 | |
39 | #define INIT_SIZE 32 // initial scalar size to be allocated |
44 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
53 | #endif |
58 | #endif |
54 | |
59 | |
55 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
56 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
57 | |
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 | |
58 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
59 | static SV *json_true, *json_false; |
72 | static SV *json_true, *json_false; |
60 | |
73 | |
|
|
74 | typedef struct { |
|
|
75 | U32 flags; |
|
|
76 | SV *cb_object; |
|
|
77 | HV *cb_sk_object; |
|
|
78 | } JSON; |
|
|
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 | |
|
|
440 | encode_nl (enc); |
|
|
441 | } |
472 | } |
|
|
473 | } |
442 | else |
474 | else |
443 | { |
475 | { |
|
|
476 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
444 | HE *he = hv_iternext (hv); |
477 | if ((he = hv_iternext (hv))) |
445 | |
|
|
446 | for (;;) |
478 | for (;;) |
447 | { |
479 | { |
448 | encode_indent (enc); |
480 | encode_indent (enc); |
449 | encode_he (enc, he); |
481 | encode_hk (enc, he); |
|
|
482 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
450 | |
483 | |
451 | if (!(he = hv_iternext (hv))) |
484 | if (!(he = hv_iternext (hv))) |
452 | break; |
485 | break; |
453 | |
486 | |
454 | encode_comma (enc); |
487 | encode_comma (enc); |
455 | } |
488 | } |
|
|
489 | } |
456 | |
490 | |
457 | encode_nl (enc); |
491 | encode_nl (enc); |
458 | } |
|
|
459 | } |
|
|
460 | |
492 | |
461 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
493 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
462 | } |
494 | } |
463 | |
495 | |
464 | // encode objects, arrays and special \0=false and \1=true values. |
496 | // encode objects, arrays and special \0=false and \1=true values. |
… | |
… | |
470 | SvGETMAGIC (sv); |
502 | SvGETMAGIC (sv); |
471 | svt = SvTYPE (sv); |
503 | svt = SvTYPE (sv); |
472 | |
504 | |
473 | if (expect_false (SvOBJECT (sv))) |
505 | if (expect_false (SvOBJECT (sv))) |
474 | { |
506 | { |
|
|
507 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
508 | ? json_boolean_stash |
|
|
509 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
510 | |
475 | if (SvSTASH (sv) == json_boolean_stash) |
511 | if (SvSTASH (sv) == stash) |
476 | { |
512 | { |
477 | if (SvIV (sv) == 0) |
513 | if (SvIV (sv)) |
|
|
514 | encode_str (enc, "true", 4, 0); |
|
|
515 | else |
478 | encode_str (enc, "false", 5, 0); |
516 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
517 | } |
482 | else |
518 | else |
483 | { |
519 | { |
484 | #if 0 |
520 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
521 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
486 | { |
522 | { |
487 | // not yet |
523 | // not yet |
488 | } |
524 | } |
489 | #endif |
525 | #endif |
490 | if (enc->flags & F_CONV_BLESSED) |
526 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
527 | { |
492 | // we re-bless the reference to get overload and other niceties right |
528 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
529 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
530 | |
495 | if (to_json) |
531 | if (to_json) |
496 | { |
532 | { |
497 | dSP; |
533 | dSP; |
498 | ENTER; |
534 | |
499 | SAVETMPS; |
|
|
500 | PUSHMARK (SP); |
535 | ENTER; SAVETMPS; PUSHMARK (SP); |
501 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
536 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
502 | |
537 | |
503 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
538 | // calling with G_SCALAR ensures that we always get a 1 return value |
504 | // check anyways. |
|
|
505 | PUTBACK; |
539 | PUTBACK; |
506 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
540 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
507 | SPAGAIN; |
541 | SPAGAIN; |
508 | |
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 | |
509 | encode_sv (enc, POPs); |
550 | encode_sv (enc, sv); |
510 | |
551 | |
511 | FREETMPS; |
552 | FREETMPS; LEAVE; |
512 | LEAVE; |
|
|
513 | } |
553 | } |
514 | else if (enc->flags & F_ALLOW_BLESSED) |
554 | else if (enc->json.flags & F_ALLOW_BLESSED) |
515 | encode_str (enc, "null", 4, 0); |
555 | encode_str (enc, "null", 4, 0); |
516 | else |
556 | else |
517 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
557 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
518 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
558 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
519 | } |
559 | } |
520 | else if (enc->flags & F_ALLOW_BLESSED) |
560 | else if (enc->json.flags & F_ALLOW_BLESSED) |
521 | encode_str (enc, "null", 4, 0); |
561 | encode_str (enc, "null", 4, 0); |
522 | else |
562 | else |
523 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
563 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
524 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
564 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
525 | } |
565 | } |
… | |
… | |
528 | encode_hv (enc, (HV *)sv); |
568 | encode_hv (enc, (HV *)sv); |
529 | else if (svt == SVt_PVAV) |
569 | else if (svt == SVt_PVAV) |
530 | encode_av (enc, (AV *)sv); |
570 | encode_av (enc, (AV *)sv); |
531 | else if (svt < SVt_PVAV) |
571 | else if (svt < SVt_PVAV) |
532 | { |
572 | { |
533 | 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') |
534 | encode_str (enc, "false", 5, 0); |
579 | encode_str (enc, "false", 5, 0); |
535 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
536 | encode_str (enc, "true", 4, 0); |
|
|
537 | else |
580 | else |
538 | 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", |
539 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
582 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
540 | } |
583 | } |
541 | else |
584 | else |
… | |
… | |
611 | 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", |
612 | SvPV_nolen (sv), SvFLAGS (sv)); |
655 | SvPV_nolen (sv), SvFLAGS (sv)); |
613 | } |
656 | } |
614 | |
657 | |
615 | static SV * |
658 | static SV * |
616 | encode_json (SV *scalar, U32 flags) |
659 | encode_json (SV *scalar, JSON *json) |
617 | { |
660 | { |
618 | enc_t enc; |
661 | enc_t enc; |
619 | |
662 | |
620 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
663 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
621 | 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)"); |
622 | |
665 | |
623 | enc.flags = flags; |
666 | enc.json = *json; |
624 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
667 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
625 | enc.cur = SvPVX (enc.sv); |
668 | enc.cur = SvPVX (enc.sv); |
626 | enc.end = SvEND (enc.sv); |
669 | enc.end = SvEND (enc.sv); |
627 | enc.indent = 0; |
670 | enc.indent = 0; |
628 | enc.maxdepth = DEC_DEPTH (flags); |
671 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
629 | |
672 | |
630 | SvPOK_only (enc.sv); |
673 | SvPOK_only (enc.sv); |
631 | encode_sv (&enc, scalar); |
674 | encode_sv (&enc, scalar); |
632 | |
675 | |
633 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
676 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
634 | *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 |
635 | |
678 | |
636 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
679 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
637 | SvUTF8_on (enc.sv); |
680 | SvUTF8_on (enc.sv); |
638 | |
681 | |
639 | if (enc.flags & F_SHRINK) |
682 | if (enc.json.flags & F_SHRINK) |
640 | shrink (enc.sv); |
683 | shrink (enc.sv); |
641 | |
684 | |
642 | return enc.sv; |
685 | return enc.sv; |
643 | } |
686 | } |
644 | |
687 | |
… | |
… | |
649 | typedef struct |
692 | typedef struct |
650 | { |
693 | { |
651 | char *cur; // current parser pointer |
694 | char *cur; // current parser pointer |
652 | char *end; // end of input string |
695 | char *end; // end of input string |
653 | const char *err; // parse error, if != 0 |
696 | const char *err; // parse error, if != 0 |
654 | U32 flags; // F_* |
697 | JSON json; |
655 | U32 depth; // recursion depth |
698 | U32 depth; // recursion depth |
656 | U32 maxdepth; // recursion depth limit |
699 | U32 maxdepth; // recursion depth limit |
657 | } dec_t; |
700 | } dec_t; |
658 | |
701 | |
659 | inline void |
702 | inline void |
… | |
… | |
922 | is_nv = 1; |
965 | is_nv = 1; |
923 | } |
966 | } |
924 | |
967 | |
925 | if (!is_nv) |
968 | if (!is_nv) |
926 | { |
969 | { |
|
|
970 | int len = dec->cur - start; |
|
|
971 | |
927 | // 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 |
928 | if (*start == '-') |
973 | if (*start == '-') |
929 | switch (dec->cur - start) |
974 | switch (len) |
930 | { |
975 | { |
931 | case 2: return newSViv (-( start [1] - '0' * 1)); |
976 | case 2: return newSViv (-( start [1] - '0' * 1)); |
932 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
977 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
933 | 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)); |
934 | 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)); |
935 | } |
980 | } |
936 | else |
981 | else |
937 | switch (dec->cur - start) |
982 | switch (len) |
938 | { |
983 | { |
939 | case 1: return newSViv ( start [0] - '0' * 1); |
984 | case 1: return newSViv ( start [0] - '0' * 1); |
940 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
985 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
941 | 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); |
942 | 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); |
943 | } |
988 | } |
944 | |
989 | |
945 | { |
990 | { |
946 | UV uv; |
991 | UV uv; |
947 | int numtype = grok_number (start, dec->cur - start, &uv); |
992 | int numtype = grok_number (start, len, &uv); |
948 | if (numtype & IS_NUMBER_IN_UV) |
993 | if (numtype & IS_NUMBER_IN_UV) |
949 | if (numtype & IS_NUMBER_NEG) |
994 | if (numtype & IS_NUMBER_NEG) |
950 | { |
995 | { |
951 | if (uv < (UV)IV_MIN) |
996 | if (uv < (UV)IV_MIN) |
952 | return newSViv (-(IV)uv); |
997 | return newSViv (-(IV)uv); |
953 | } |
998 | } |
954 | else |
999 | else |
955 | return newSVuv (uv); |
1000 | return newSVuv (uv); |
956 | |
|
|
957 | // here would likely be the place for bigint support |
|
|
958 | } |
1001 | } |
959 | } |
|
|
960 | |
1002 | |
961 | // 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 |
962 | return newSVnv (Atof (start)); |
1019 | return newSVnv (Atof (start)); |
963 | |
1020 | |
964 | fail: |
1021 | fail: |
965 | return 0; |
1022 | return 0; |
966 | } |
1023 | } |
… | |
… | |
1010 | } |
1067 | } |
1011 | |
1068 | |
1012 | static SV * |
1069 | static SV * |
1013 | decode_hv (dec_t *dec) |
1070 | decode_hv (dec_t *dec) |
1014 | { |
1071 | { |
|
|
1072 | SV *sv; |
1015 | HV *hv = newHV (); |
1073 | HV *hv = newHV (); |
1016 | |
1074 | |
1017 | DEC_INC_DEPTH; |
1075 | DEC_INC_DEPTH; |
1018 | decode_ws (dec); |
1076 | decode_ws (dec); |
1019 | |
1077 | |
1020 | if (*dec->cur == '}') |
1078 | if (*dec->cur == '}') |
1021 | ++dec->cur; |
1079 | ++dec->cur; |
1022 | else |
1080 | else |
1023 | for (;;) |
1081 | for (;;) |
1024 | { |
1082 | { |
1025 | SV *key, *value; |
|
|
1026 | |
|
|
1027 | decode_ws (dec); EXPECT_CH ('"'); |
1083 | decode_ws (dec); EXPECT_CH ('"'); |
1028 | |
1084 | |
1029 | key = decode_str (dec); |
1085 | // heuristic: assume that |
1030 | if (!key) |
1086 | // a) decode_str + hv_store_ent are abysmally slow. |
1031 | 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 |
1032 | |
1094 | |
1033 | decode_ws (dec); EXPECT_CH (':'); |
1095 | for (;;) |
1034 | |
|
|
1035 | value = decode_sv (dec); |
|
|
1036 | if (!value) |
|
|
1037 | { |
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); |
1038 | 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) |
1039 | goto fail; |
1130 | goto fail; |
|
|
1131 | |
|
|
1132 | hv_store (hv, key, len, value, 0); |
|
|
1133 | |
|
|
1134 | break; |
|
|
1135 | } |
|
|
1136 | |
|
|
1137 | ++p; |
1040 | } |
1138 | } |
1041 | |
1139 | } |
1042 | hv_store_ent (hv, key, value, 0); |
|
|
1043 | SvREFCNT_dec (key); |
|
|
1044 | |
1140 | |
1045 | decode_ws (dec); |
1141 | decode_ws (dec); |
1046 | |
1142 | |
1047 | if (*dec->cur == '}') |
1143 | if (*dec->cur == '}') |
1048 | { |
1144 | { |
… | |
… | |
1055 | |
1151 | |
1056 | ++dec->cur; |
1152 | ++dec->cur; |
1057 | } |
1153 | } |
1058 | |
1154 | |
1059 | DEC_DEC_DEPTH; |
1155 | DEC_DEC_DEPTH; |
1060 | 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; |
1061 | |
1217 | |
1062 | fail: |
1218 | fail: |
1063 | SvREFCNT_dec (hv); |
1219 | SvREFCNT_dec (hv); |
1064 | DEC_DEC_DEPTH; |
1220 | DEC_DEC_DEPTH; |
1065 | return 0; |
1221 | return 0; |
… | |
… | |
1085 | |
1241 | |
1086 | case 't': |
1242 | case 't': |
1087 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1243 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1088 | { |
1244 | { |
1089 | dec->cur += 4; |
1245 | dec->cur += 4; |
|
|
1246 | #if JSON_SLOW |
|
|
1247 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1248 | #endif |
1090 | return SvREFCNT_inc (json_true); |
1249 | return SvREFCNT_inc (json_true); |
1091 | } |
1250 | } |
1092 | else |
1251 | else |
1093 | ERR ("'true' expected"); |
1252 | ERR ("'true' expected"); |
1094 | |
1253 | |
… | |
… | |
1096 | |
1255 | |
1097 | case 'f': |
1256 | case 'f': |
1098 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1257 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1099 | { |
1258 | { |
1100 | dec->cur += 5; |
1259 | dec->cur += 5; |
|
|
1260 | #if JSON_SLOW |
|
|
1261 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1262 | #endif |
1101 | return SvREFCNT_inc (json_false); |
1263 | return SvREFCNT_inc (json_false); |
1102 | } |
1264 | } |
1103 | else |
1265 | else |
1104 | ERR ("'false' expected"); |
1266 | ERR ("'false' expected"); |
1105 | |
1267 | |
… | |
… | |
1124 | fail: |
1286 | fail: |
1125 | return 0; |
1287 | return 0; |
1126 | } |
1288 | } |
1127 | |
1289 | |
1128 | static SV * |
1290 | static SV * |
1129 | decode_json (SV *string, U32 flags, UV *offset_return) |
1291 | decode_json (SV *string, JSON *json, UV *offset_return) |
1130 | { |
1292 | { |
1131 | dec_t dec; |
1293 | dec_t dec; |
1132 | UV offset; |
1294 | UV offset; |
1133 | SV *sv; |
1295 | SV *sv; |
1134 | |
1296 | |
1135 | SvGETMAGIC (string); |
1297 | SvGETMAGIC (string); |
1136 | SvUPGRADE (string, SVt_PV); |
1298 | SvUPGRADE (string, SVt_PV); |
1137 | |
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 | |
1138 | if (flags & F_UTF8) |
1304 | if (json->flags & F_UTF8) |
1139 | sv_utf8_downgrade (string, 0); |
1305 | sv_utf8_downgrade (string, 0); |
1140 | else |
1306 | else |
1141 | sv_utf8_upgrade (string); |
1307 | sv_utf8_upgrade (string); |
1142 | |
1308 | |
1143 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1309 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1144 | |
1310 | |
1145 | dec.flags = flags; |
1311 | dec.json = *json; |
1146 | dec.cur = SvPVX (string); |
1312 | dec.cur = SvPVX (string); |
1147 | dec.end = SvEND (string); |
1313 | dec.end = SvEND (string); |
1148 | dec.err = 0; |
1314 | dec.err = 0; |
1149 | dec.depth = 0; |
1315 | dec.depth = 0; |
1150 | 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; |
1151 | |
1320 | |
1152 | *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 |
1153 | sv = decode_sv (&dec); |
1322 | sv = decode_sv (&dec); |
1154 | |
1323 | |
1155 | if (!(offset_return || !sv)) |
1324 | if (!(offset_return || !sv)) |
… | |
… | |
1165 | } |
1334 | } |
1166 | } |
1335 | } |
1167 | |
1336 | |
1168 | if (offset_return || !sv) |
1337 | if (offset_return || !sv) |
1169 | { |
1338 | { |
1170 | offset = dec.flags & F_UTF8 |
1339 | offset = dec.json.flags & F_UTF8 |
1171 | ? dec.cur - SvPVX (string) |
1340 | ? dec.cur - SvPVX (string) |
1172 | : utf8_distance (dec.cur, SvPVX (string)); |
1341 | : utf8_distance (dec.cur, SvPVX (string)); |
1173 | |
1342 | |
1174 | if (offset_return) |
1343 | if (offset_return) |
1175 | *offset_return = offset; |
1344 | *offset_return = offset; |
… | |
… | |
1194 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1363 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1195 | } |
1364 | } |
1196 | |
1365 | |
1197 | sv = sv_2mortal (sv); |
1366 | sv = sv_2mortal (sv); |
1198 | |
1367 | |
1199 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1368 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1200 | 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)"); |
1201 | |
1370 | |
1202 | return sv; |
1371 | return sv; |
1203 | } |
1372 | } |
1204 | |
1373 | |
… | |
… | |
1225 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1394 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1226 | } |
1395 | } |
1227 | |
1396 | |
1228 | PROTOTYPES: DISABLE |
1397 | PROTOTYPES: DISABLE |
1229 | |
1398 | |
1230 | SV *new (char *dummy) |
1399 | void CLONE (...) |
1231 | CODE: |
1400 | CODE: |
1232 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1401 | json_stash = 0; |
1233 | OUTPUT: |
1402 | json_boolean_stash = 0; |
1234 | RETVAL |
|
|
1235 | |
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 | |
1236 | SV *ascii (SV *self, int enable = 1) |
1414 | void ascii (JSON *self, int enable = 1) |
1237 | ALIAS: |
1415 | ALIAS: |
1238 | ascii = F_ASCII |
1416 | ascii = F_ASCII |
1239 | latin1 = F_LATIN1 |
1417 | latin1 = F_LATIN1 |
1240 | utf8 = F_UTF8 |
1418 | utf8 = F_UTF8 |
1241 | indent = F_INDENT |
1419 | indent = F_INDENT |
… | |
… | |
1245 | pretty = F_PRETTY |
1423 | pretty = F_PRETTY |
1246 | allow_nonref = F_ALLOW_NONREF |
1424 | allow_nonref = F_ALLOW_NONREF |
1247 | shrink = F_SHRINK |
1425 | shrink = F_SHRINK |
1248 | allow_blessed = F_ALLOW_BLESSED |
1426 | allow_blessed = F_ALLOW_BLESSED |
1249 | convert_blessed = F_CONV_BLESSED |
1427 | convert_blessed = F_CONV_BLESSED |
1250 | CODE: |
1428 | PPCODE: |
1251 | { |
1429 | { |
1252 | UV *uv = SvJSON (self); |
|
|
1253 | if (enable) |
1430 | if (enable) |
1254 | *uv |= ix; |
1431 | self->flags |= ix; |
1255 | else |
1432 | else |
1256 | *uv &= ~ix; |
1433 | self->flags &= ~ix; |
1257 | |
1434 | |
1258 | RETVAL = newSVsv (self); |
1435 | XPUSHs (ST (0)); |
1259 | } |
1436 | } |
1260 | OUTPUT: |
|
|
1261 | RETVAL |
|
|
1262 | |
1437 | |
1263 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1438 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1264 | CODE: |
1439 | PPCODE: |
1265 | { |
1440 | { |
1266 | UV *uv = SvJSON (self); |
|
|
1267 | UV log2 = 0; |
1441 | UV log2 = 0; |
1268 | |
1442 | |
1269 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1443 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1270 | |
1444 | |
1271 | while ((1UL << log2) < max_depth) |
1445 | while ((1UL << log2) < max_depth) |
1272 | ++log2; |
1446 | ++log2; |
1273 | |
1447 | |
1274 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1448 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1275 | |
1449 | |
1276 | RETVAL = newSVsv (self); |
1450 | XPUSHs (ST (0)); |
1277 | } |
1451 | } |
1278 | OUTPUT: |
|
|
1279 | RETVAL |
|
|
1280 | |
1452 | |
1281 | void encode (SV *self, SV *scalar) |
1453 | void max_size (JSON *self, UV max_size = 0) |
1282 | PPCODE: |
1454 | PPCODE: |
1283 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1455 | { |
|
|
1456 | UV log2 = 0; |
1284 | |
1457 | |
1285 | 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) |
1286 | 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: |
1287 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1506 | XPUSHs (decode_json (jsonstr, self, 0)); |
1288 | |
1507 | |
1289 | void decode_prefix (SV *self, SV *jsonstr) |
1508 | void decode_prefix (JSON *self, SV *jsonstr) |
1290 | PPCODE: |
1509 | PPCODE: |
1291 | { |
1510 | { |
1292 | UV offset; |
1511 | UV offset; |
1293 | EXTEND (SP, 2); |
1512 | EXTEND (SP, 2); |
1294 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1513 | PUSHs (decode_json (jsonstr, self, &offset)); |
1295 | PUSHs (sv_2mortal (newSVuv (offset))); |
1514 | PUSHs (sv_2mortal (newSVuv (offset))); |
1296 | } |
1515 | } |
1297 | |
1516 | |
|
|
1517 | void DESTROY (JSON *self) |
|
|
1518 | CODE: |
|
|
1519 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1520 | SvREFCNT_dec (self->cb_object); |
|
|
1521 | |
1298 | PROTOTYPES: ENABLE |
1522 | PROTOTYPES: ENABLE |
1299 | |
1523 | |
1300 | void to_json (SV *scalar) |
1524 | void to_json (SV *scalar) |
1301 | ALIAS: |
|
|
1302 | objToJson = 0 |
|
|
1303 | PPCODE: |
1525 | PPCODE: |
|
|
1526 | { |
|
|
1527 | JSON json = { F_DEFAULT | F_UTF8 }; |
1304 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1528 | XPUSHs (encode_json (scalar, &json)); |
|
|
1529 | } |
1305 | |
1530 | |
1306 | void from_json (SV *jsonstr) |
1531 | void from_json (SV *jsonstr) |
1307 | ALIAS: |
|
|
1308 | jsonToObj = 0 |
|
|
1309 | PPCODE: |
1532 | PPCODE: |
|
|
1533 | { |
|
|
1534 | JSON json = { F_DEFAULT | F_UTF8 }; |
1310 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1535 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1536 | } |
1311 | |
1537 | |