… | |
… | |
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 | |
|
|
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
17 | #define F_DEFAULT 0 |
19 | #define F_DEFAULT 0 |
18 | |
20 | |
19 | #define INIT_SIZE 32 // initial scalar size to be allocated |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
20 | |
22 | |
21 | #define SB do { |
23 | #define SB do { |
… | |
… | |
146 | for (i_ = enc->indent * 3; i_--; )\ |
148 | for (i_ = enc->indent * 3; i_--; )\ |
147 | encode_ch (enc, ' '); \ |
149 | encode_ch (enc, ' '); \ |
148 | } \ |
150 | } \ |
149 | SE |
151 | SE |
150 | |
152 | |
151 | #define SPACE SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, ' '); } SE |
153 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
152 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
154 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
153 | #define COMMA SB \ |
155 | #define COMMA SB \ |
154 | encode_ch (enc, ','); \ |
156 | encode_ch (enc, ','); \ |
155 | if (enc->flags & F_INDENT) \ |
157 | if (enc->flags & F_INDENT) \ |
156 | NL; \ |
158 | NL; \ |
… | |
… | |
358 | } |
360 | } |
359 | |
361 | |
360 | static SV * |
362 | static SV * |
361 | encode_json (SV *scalar, UV flags) |
363 | encode_json (SV *scalar, UV flags) |
362 | { |
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 | |
363 | enc_t enc; |
368 | enc_t enc; |
364 | enc.flags = flags; |
369 | enc.flags = flags; |
365 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
370 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
366 | enc.cur = SvPVX (enc.sv); |
371 | enc.cur = SvPVX (enc.sv); |
367 | enc.end = SvEND (enc.sv); |
372 | enc.end = SvEND (enc.sv); |
… | |
… | |
771 | dec.err, |
776 | dec.err, |
772 | (int)offset, |
777 | (int)offset, |
773 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
778 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
774 | } |
779 | } |
775 | |
780 | |
776 | sv_dump (sv);//D |
|
|
777 | 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; |
778 | } |
787 | } |
779 | |
788 | |
780 | MODULE = JSON::XS PACKAGE = JSON::XS |
789 | MODULE = JSON::XS PACKAGE = JSON::XS |
781 | |
790 | |
782 | BOOT: |
791 | BOOT: |
… | |
… | |
809 | indent = F_INDENT |
818 | indent = F_INDENT |
810 | canonical = F_CANONICAL |
819 | canonical = F_CANONICAL |
811 | space_before = F_SPACE_BEFORE |
820 | space_before = F_SPACE_BEFORE |
812 | space_after = F_SPACE_AFTER |
821 | space_after = F_SPACE_AFTER |
813 | json_rpc = F_JSON_RPC |
822 | json_rpc = F_JSON_RPC |
|
|
823 | pretty = F_PRETTY |
|
|
824 | allow_nonref = F_ALLOW_NONREF |
814 | CODE: |
825 | CODE: |
815 | { |
826 | { |
816 | UV *uv = SvJSON (self); |
827 | UV *uv = SvJSON (self); |
817 | if (enable) |
828 | if (enable) |
818 | *uv |= ix; |
829 | *uv |= ix; |
… | |
… | |
826 | |
837 | |
827 | void encode (SV *self, SV *scalar) |
838 | void encode (SV *self, SV *scalar) |
828 | PPCODE: |
839 | PPCODE: |
829 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
840 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
830 | |
841 | |
831 | void decode (SV *self, SV *jsondata) |
842 | void decode (SV *self, SV *jsonstr) |
832 | PPCODE: |
843 | PPCODE: |
833 | XPUSHs (decode_json (jsondata, *SvJSON (self))); |
844 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
834 | |
845 | |
|
|
846 | void to_json (SV *scalar) |
|
|
847 | PPCODE: |
|
|
848 | XPUSHs (encode_json (scalar, F_UTF8)); |
|
|
849 | |
|
|
850 | void from_json (SV *jsonstr) |
|
|
851 | PPCODE: |
|
|
852 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
|
|
853 | |