… | |
… | |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004 |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008 |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010 |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020 |
15 | #define F_JSON_RPC 0x00000040 |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
16 | |
17 | |
17 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
18 | #define F_DEFAULT 0 |
19 | #define F_DEFAULT 0 |
19 | |
20 | |
20 | #define INIT_SIZE 32 // initial scalar size to be allocated |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
359 | } |
360 | } |
360 | |
361 | |
361 | static SV * |
362 | static SV * |
362 | encode_json (SV *scalar, UV flags) |
363 | encode_json (SV *scalar, UV flags) |
363 | { |
364 | { |
|
|
365 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
|
|
366 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
|
|
367 | |
364 | enc_t enc; |
368 | enc_t enc; |
365 | enc.flags = flags; |
369 | enc.flags = flags; |
366 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
370 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
367 | enc.cur = SvPVX (enc.sv); |
371 | enc.cur = SvPVX (enc.sv); |
368 | enc.end = SvEND (enc.sv); |
372 | enc.end = SvEND (enc.sv); |
… | |
… | |
772 | dec.err, |
776 | dec.err, |
773 | (int)offset, |
777 | (int)offset, |
774 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
778 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
775 | } |
779 | } |
776 | |
780 | |
777 | sv_dump (sv);//D |
|
|
778 | return sv_2mortal (sv); |
781 | sv = sv_2mortal (sv); |
|
|
782 | |
|
|
783 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
|
|
784 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
|
|
785 | |
|
|
786 | return sv; |
779 | } |
787 | } |
780 | |
788 | |
781 | MODULE = JSON::XS PACKAGE = JSON::XS |
789 | MODULE = JSON::XS PACKAGE = JSON::XS |
782 | |
790 | |
783 | BOOT: |
791 | BOOT: |
… | |
… | |
811 | canonical = F_CANONICAL |
819 | canonical = F_CANONICAL |
812 | space_before = F_SPACE_BEFORE |
820 | space_before = F_SPACE_BEFORE |
813 | space_after = F_SPACE_AFTER |
821 | space_after = F_SPACE_AFTER |
814 | json_rpc = F_JSON_RPC |
822 | json_rpc = F_JSON_RPC |
815 | pretty = F_PRETTY |
823 | pretty = F_PRETTY |
|
|
824 | allow_nonref = F_ALLOW_NONREF |
816 | CODE: |
825 | CODE: |
817 | { |
826 | { |
818 | UV *uv = SvJSON (self); |
827 | UV *uv = SvJSON (self); |
819 | if (enable) |
828 | if (enable) |
820 | *uv |= ix; |
829 | *uv |= ix; |