… | |
… | |
21 | |
21 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
22 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
23 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
24 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
27 | |
27 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
29 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
30 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
76 | { |
76 | { |
77 | *clen = 2; |
77 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
79 | } |
80 | else |
80 | else |
|
|
81 | { |
|
|
82 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
83 | return (UV)-1; |
|
|
84 | } |
82 | } |
85 | } |
83 | |
86 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
87 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
88 | // encoder |
86 | |
89 | |
… | |
… | |
268 | static void |
271 | static void |
269 | encode_av (enc_t *enc, AV *av) |
272 | encode_av (enc_t *enc, AV *av) |
270 | { |
273 | { |
271 | int i, len = av_len (av); |
274 | int i, len = av_len (av); |
272 | |
275 | |
|
|
276 | if (enc->indent >= enc->maxdepth) |
|
|
277 | croak ("data structure too deep (hit recursion limit)"); |
|
|
278 | |
273 | encode_ch (enc, '['); encode_nl (enc); |
279 | encode_ch (enc, '['); encode_nl (enc); |
274 | ++enc->indent; |
280 | ++enc->indent; |
275 | |
281 | |
276 | for (i = 0; i <= len; ++i) |
282 | for (i = 0; i <= len; ++i) |
277 | { |
283 | { |
… | |
… | |
342 | |
348 | |
343 | static void |
349 | static void |
344 | encode_hv (enc_t *enc, HV *hv) |
350 | encode_hv (enc_t *enc, HV *hv) |
345 | { |
351 | { |
346 | int count, i; |
352 | int count, i; |
|
|
353 | |
|
|
354 | if (enc->indent >= enc->maxdepth) |
|
|
355 | croak ("data structure too deep (hit recursion limit)"); |
347 | |
356 | |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
357 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 | |
358 | |
350 | if ((count = hv_iterinit (hv))) |
359 | if ((count = hv_iterinit (hv))) |
351 | { |
360 | { |
… | |
… | |
420 | } |
429 | } |
421 | |
430 | |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
431 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
423 | } |
432 | } |
424 | |
433 | |
|
|
434 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
435 | static void |
|
|
436 | encode_rv (enc_t *enc, SV *sv) |
|
|
437 | { |
|
|
438 | SvGETMAGIC (sv); |
|
|
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
|
|
441 | |
|
|
442 | if (svt == SVt_PVHV) |
|
|
443 | encode_hv (enc, (HV *)sv); |
|
|
444 | else if (svt == SVt_PVAV) |
|
|
445 | encode_av (enc, (AV *)sv); |
|
|
446 | else if (svt < SVt_PVAV) |
|
|
447 | { |
|
|
448 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
449 | encode_str (enc, "false", 5, 0); |
|
|
450 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
451 | encode_str (enc, "true", 4, 0); |
|
|
452 | else |
|
|
453 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
454 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
455 | } |
|
|
456 | else |
|
|
457 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
458 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
459 | } |
|
|
460 | |
425 | static void |
461 | static void |
426 | encode_sv (enc_t *enc, SV *sv) |
462 | encode_sv (enc_t *enc, SV *sv) |
427 | { |
463 | { |
428 | SvGETMAGIC (sv); |
464 | SvGETMAGIC (sv); |
429 | |
465 | |
… | |
… | |
448 | SvIsUV(sv) |
484 | SvIsUV(sv) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
485 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
486 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
451 | } |
487 | } |
452 | else if (SvROK (sv)) |
488 | else if (SvROK (sv)) |
453 | { |
489 | encode_rv (enc, SvRV (sv)); |
454 | SV *rv = SvRV (sv); |
|
|
455 | |
|
|
456 | if (enc->indent >= enc->maxdepth) |
|
|
457 | croak ("data structure too deep (hit recursion limit)"); |
|
|
458 | |
|
|
459 | switch (SvTYPE (rv)) |
|
|
460 | { |
|
|
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
462 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
463 | |
|
|
464 | default: |
|
|
465 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
466 | SvPV_nolen (sv)); |
|
|
467 | } |
|
|
468 | } |
|
|
469 | else if (!SvOK (sv)) |
490 | else if (!SvOK (sv)) |
470 | encode_str (enc, "null", 4, 0); |
491 | encode_str (enc, "null", 4, 0); |
471 | else |
492 | else |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
493 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
494 | SvPV_nolen (sv), SvFLAGS (sv)); |
… | |
… | |
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
658 | if (clen == (STRLEN)-1) |
679 | if (clen == (STRLEN)-1) |
659 | ERR ("malformed UTF-8 character in JSON string"); |
680 | ERR ("malformed UTF-8 character in JSON string"); |
660 | |
681 | |
661 | do |
682 | do |
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
683 | *cur++ = *dec->cur++; |
664 | } |
|
|
665 | while (--clen); |
684 | while (--clen); |
666 | |
685 | |
667 | utf8 = 1; |
686 | utf8 = 1; |
668 | } |
687 | } |
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
688 | else |
|
|
689 | { |
|
|
690 | --dec->cur; |
|
|
691 | |
|
|
692 | if (!ch) |
|
|
693 | ERR ("unexpected end of string while parsing json string"); |
|
|
694 | else |
672 | ERR ("invalid character encountered"); |
695 | ERR ("invalid character encountered while parsing json string"); |
673 | |
696 | } |
674 | } |
697 | } |
675 | while (cur < buf + SHORT_STRING_LEN); |
698 | while (cur < buf + SHORT_STRING_LEN); |
676 | |
699 | |
677 | STRLEN len = cur - buf; |
700 | STRLEN len = cur - buf; |
678 | |
701 | |
… | |
… | |
948 | static SV * |
971 | static SV * |
949 | decode_json (SV *string, U32 flags) |
972 | decode_json (SV *string, U32 flags) |
950 | { |
973 | { |
951 | SV *sv; |
974 | SV *sv; |
952 | |
975 | |
|
|
976 | SvUPGRADE (string, SVt_PV); |
|
|
977 | |
953 | if (flags & F_UTF8) |
978 | if (flags & F_UTF8) |
954 | sv_utf8_downgrade (string, 0); |
979 | sv_utf8_downgrade (string, 0); |
955 | else |
980 | else |
956 | sv_utf8_upgrade (string); |
981 | sv_utf8_upgrade (string); |
957 | |
982 | |
… | |
… | |
963 | dec.end = SvEND (string); |
988 | dec.end = SvEND (string); |
964 | dec.err = 0; |
989 | dec.err = 0; |
965 | dec.depth = 0; |
990 | dec.depth = 0; |
966 | dec.maxdepth = DEC_DEPTH (dec.flags); |
991 | dec.maxdepth = DEC_DEPTH (dec.flags); |
967 | |
992 | |
968 | *SvEND (sv) = 0; // this shou[ld basically be a nop, too |
993 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
969 | sv = decode_sv (&dec); |
994 | sv = decode_sv (&dec); |
970 | |
995 | |
971 | if (!sv) |
996 | if (!sv) |
972 | { |
997 | { |
973 | IV offset = dec.flags & F_UTF8 |
998 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
982 | SAVEVPTR (PL_curcop); |
1007 | SAVEVPTR (PL_curcop); |
983 | PL_curcop = &cop; |
1008 | PL_curcop = &cop; |
984 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1009 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
985 | LEAVE; |
1010 | LEAVE; |
986 | |
1011 | |
987 | croak ("%s, at character offset %d (%s)", |
1012 | croak ("%s, at character offset %d [\"%s\"]", |
988 | dec.err, |
1013 | dec.err, |
989 | (int)offset, |
1014 | (int)offset, |
990 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1015 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
991 | } |
1016 | } |
992 | |
1017 | |