… | |
… | |
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 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
|
|
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
|
|
25 | |
|
|
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
27 | |
|
|
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
30 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
33 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
35 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
36 | |
|
|
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
22 | |
38 | |
23 | #define SB do { |
39 | #define SB do { |
24 | #define SE } while (0) |
40 | #define SE } while (0) |
25 | |
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
52 | |
26 | static HV *json_stash; |
53 | static HV *json_stash; // JSON::XS:: |
|
|
54 | |
|
|
55 | ///////////////////////////////////////////////////////////////////////////// |
|
|
56 | // utility functions |
|
|
57 | |
|
|
58 | static UV * |
|
|
59 | SvJSON (SV *sv) |
|
|
60 | { |
|
|
61 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
62 | croak ("object is not of type JSON::XS"); |
|
|
63 | |
|
|
64 | return &SvUVX (SvRV (sv)); |
|
|
65 | } |
|
|
66 | |
|
|
67 | static void |
|
|
68 | shrink (SV *sv) |
|
|
69 | { |
|
|
70 | sv_utf8_downgrade (sv, 1); |
|
|
71 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
72 | { |
|
|
73 | #ifdef SvPV_shrink_to_cur |
|
|
74 | SvPV_shrink_to_cur (sv); |
|
|
75 | #elif defined (SvPV_renew) |
|
|
76 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
77 | #endif |
|
|
78 | } |
|
|
79 | } |
|
|
80 | |
|
|
81 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
82 | // case of an error. |
|
|
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
84 | // but use the very good perl function to parse anything else. |
|
|
85 | // note that we never call this function for a ascii codepoints |
|
|
86 | inline UV |
|
|
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
88 | { |
|
|
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
92 | { |
|
|
93 | *clen = 2; |
|
|
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
95 | } |
|
|
96 | else |
|
|
97 | { |
|
|
98 | *clen = (STRLEN)-1; |
|
|
99 | return (UV)-1; |
|
|
100 | } |
|
|
101 | } |
|
|
102 | |
|
|
103 | ///////////////////////////////////////////////////////////////////////////// |
|
|
104 | // encoder |
27 | |
105 | |
28 | // structure used for encoding JSON |
106 | // structure used for encoding JSON |
29 | typedef struct |
107 | typedef struct |
30 | { |
108 | { |
31 | char *cur; |
109 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
110 | char *end; // SvEND (sv) |
34 | SV *sv; |
111 | SV *sv; // result scalar |
35 | UV flags; |
112 | U32 flags; // F_* |
36 | int max_recurse; |
113 | U32 indent; // indentation level |
37 | int indent; |
114 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
115 | } enc_t; |
39 | |
116 | |
40 | // structure used for decoding JSON |
117 | inline void |
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | ///////////////////////////////////////////////////////////////////////////// |
|
|
59 | |
|
|
60 | static void |
|
|
61 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
62 | { |
119 | { |
63 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
64 | { |
121 | { |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
66 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
67 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
68 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
69 | } |
126 | } |
70 | } |
127 | } |
71 | |
128 | |
72 | static void |
129 | inline void |
73 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
74 | { |
131 | { |
75 | need (enc, 1); |
132 | need (enc, 1); |
76 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
77 | } |
134 | } |
… | |
… | |
85 | |
142 | |
86 | while (str < end) |
143 | while (str < end) |
87 | { |
144 | { |
88 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
89 | |
146 | |
90 | if (ch == '"') |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
91 | { |
148 | { |
|
|
149 | if (expect_false (ch == '"')) // but with slow exceptions |
|
|
150 | { |
92 | need (enc, len += 1); |
151 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
154 | } |
97 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
98 | { |
156 | { |
99 | need (enc, len += 1); |
157 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
160 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
161 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
162 | *enc->cur++ = ch; |
107 | ++str; |
163 | |
108 | } |
|
|
109 | else if (ch == '\015') |
|
|
110 | { |
|
|
111 | need (enc, len += 1); |
|
|
112 | *enc->cur++ = '\\'; |
|
|
113 | *enc->cur++ = 'r'; |
|
|
114 | ++str; |
|
|
115 | } |
|
|
116 | else if (ch == '\012') |
|
|
117 | { |
|
|
118 | need (enc, len += 1); |
|
|
119 | *enc->cur++ = '\\'; |
|
|
120 | *enc->cur++ = 'n'; |
|
|
121 | ++str; |
164 | ++str; |
122 | } |
165 | } |
123 | else |
166 | else |
124 | { |
167 | { |
125 | STRLEN clen; |
168 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
169 | { |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
170 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
131 | if (clen < 0) |
171 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
132 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
172 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
173 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
174 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
175 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
176 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
177 | { |
|
|
178 | STRLEN clen; |
|
|
179 | UV uch; |
|
|
180 | |
|
|
181 | if (is_utf8) |
|
|
182 | { |
|
|
183 | uch = decode_utf8 (str, end - str, &clen); |
|
|
184 | if (clen == (STRLEN)-1) |
|
|
185 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
186 | } |
|
|
187 | else |
|
|
188 | { |
|
|
189 | uch = ch; |
|
|
190 | clen = 1; |
|
|
191 | } |
|
|
192 | |
|
|
193 | if (uch > 0x10FFFFUL) |
|
|
194 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
195 | |
|
|
196 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
|
|
197 | { |
|
|
198 | if (uch > 0xFFFFUL) |
|
|
199 | { |
144 | need (enc, len += 11); |
200 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
201 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
202 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
203 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
148 | enc->cur += 12; |
204 | enc->cur += 12; |
|
|
205 | } |
|
|
206 | else |
|
|
207 | { |
|
|
208 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
209 | need (enc, len += 5); |
|
|
210 | *enc->cur++ = '\\'; |
|
|
211 | *enc->cur++ = 'u'; |
|
|
212 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
213 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
214 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
215 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
216 | } |
|
|
217 | |
|
|
218 | str += clen; |
|
|
219 | } |
|
|
220 | else if (enc->flags & F_LATIN1) |
|
|
221 | { |
|
|
222 | *enc->cur++ = uch; |
|
|
223 | str += clen; |
|
|
224 | } |
|
|
225 | else if (is_utf8) |
|
|
226 | { |
|
|
227 | need (enc, len += clen); |
|
|
228 | do |
|
|
229 | { |
|
|
230 | *enc->cur++ = *str++; |
|
|
231 | } |
|
|
232 | while (--clen); |
|
|
233 | } |
|
|
234 | else |
|
|
235 | { |
|
|
236 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
|
|
237 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
238 | ++str; |
|
|
239 | } |
149 | } |
240 | } |
150 | else |
|
|
151 | { |
|
|
152 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
153 | need (enc, len += 5); |
|
|
154 | *enc->cur++ = '\\'; |
|
|
155 | *enc->cur++ = 'u'; |
|
|
156 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
157 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
158 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
159 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
160 | } |
|
|
161 | |
|
|
162 | str += clen; |
|
|
163 | } |
|
|
164 | else if (is_utf8) |
|
|
165 | { |
|
|
166 | need (enc, len += clen); |
|
|
167 | while (clen--) |
|
|
168 | *enc->cur++ = *str++; |
|
|
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | need (enc, 10); // never more than 11 bytes needed |
|
|
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
174 | ++str; |
|
|
175 | } |
241 | } |
176 | } |
242 | } |
177 | |
243 | |
178 | --len; |
244 | --len; |
179 | } |
245 | } |
180 | } |
246 | } |
181 | |
247 | |
182 | #define INDENT SB \ |
248 | inline void |
|
|
249 | encode_indent (enc_t *enc) |
|
|
250 | { |
183 | if (enc->flags & F_INDENT) \ |
251 | if (enc->flags & F_INDENT) |
184 | { \ |
252 | { |
185 | int i_; \ |
253 | int spaces = enc->indent * INDENT_STEP; |
186 | need (enc, enc->indent); \ |
|
|
187 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
188 | encode_ch (enc, ' '); \ |
|
|
189 | } \ |
|
|
190 | SE |
|
|
191 | |
254 | |
192 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
255 | need (enc, spaces); |
193 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
256 | memset (enc->cur, ' ', spaces); |
194 | #define COMMA SB \ |
257 | enc->cur += spaces; |
|
|
258 | } |
|
|
259 | } |
|
|
260 | |
|
|
261 | inline void |
|
|
262 | encode_space (enc_t *enc) |
|
|
263 | { |
|
|
264 | need (enc, 1); |
195 | encode_ch (enc, ','); \ |
265 | encode_ch (enc, ' '); |
|
|
266 | } |
|
|
267 | |
|
|
268 | inline void |
|
|
269 | encode_nl (enc_t *enc) |
|
|
270 | { |
196 | if (enc->flags & F_INDENT) \ |
271 | if (enc->flags & F_INDENT) |
197 | NL; \ |
272 | { |
|
|
273 | need (enc, 1); |
|
|
274 | encode_ch (enc, '\n'); |
|
|
275 | } |
|
|
276 | } |
|
|
277 | |
|
|
278 | inline void |
|
|
279 | encode_comma (enc_t *enc) |
|
|
280 | { |
|
|
281 | encode_ch (enc, ','); |
|
|
282 | |
|
|
283 | if (enc->flags & F_INDENT) |
|
|
284 | encode_nl (enc); |
198 | else if (enc->flags & F_SPACE_AFTER) \ |
285 | else if (enc->flags & F_SPACE_AFTER) |
199 | SPACE; \ |
286 | encode_space (enc); |
200 | SE |
287 | } |
201 | |
288 | |
202 | static void encode_sv (enc_t *enc, SV *sv); |
289 | static void encode_sv (enc_t *enc, SV *sv); |
203 | |
290 | |
204 | static void |
291 | static void |
205 | encode_av (enc_t *enc, AV *av) |
292 | encode_av (enc_t *enc, AV *av) |
206 | { |
293 | { |
207 | int i, len = av_len (av); |
294 | int i, len = av_len (av); |
208 | |
295 | |
209 | encode_ch (enc, '['); NL; |
296 | if (enc->indent >= enc->maxdepth) |
|
|
297 | croak ("data structure too deep (hit recursion limit)"); |
|
|
298 | |
|
|
299 | encode_ch (enc, '['); encode_nl (enc); |
210 | ++enc->indent; |
300 | ++enc->indent; |
211 | |
301 | |
212 | for (i = 0; i <= len; ++i) |
302 | for (i = 0; i <= len; ++i) |
213 | { |
303 | { |
214 | INDENT; |
304 | encode_indent (enc); |
215 | encode_sv (enc, *av_fetch (av, i, 0)); |
305 | encode_sv (enc, *av_fetch (av, i, 0)); |
216 | |
306 | |
217 | if (i < len) |
307 | if (i < len) |
218 | COMMA; |
308 | encode_comma (enc); |
219 | } |
309 | } |
220 | |
310 | |
221 | NL; |
311 | encode_nl (enc); |
222 | |
312 | |
223 | --enc->indent; |
313 | --enc->indent; |
224 | INDENT; encode_ch (enc, ']'); |
314 | encode_indent (enc); encode_ch (enc, ']'); |
225 | } |
315 | } |
226 | |
316 | |
227 | static void |
317 | static void |
228 | encode_he (enc_t *enc, HE *he) |
318 | encode_he (enc_t *enc, HE *he) |
229 | { |
319 | { |
… | |
… | |
243 | else |
333 | else |
244 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
334 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
245 | |
335 | |
246 | encode_ch (enc, '"'); |
336 | encode_ch (enc, '"'); |
247 | |
337 | |
248 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
338 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
249 | encode_ch (enc, ':'); |
339 | encode_ch (enc, ':'); |
250 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
340 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
251 | encode_sv (enc, HeVAL (he)); |
341 | encode_sv (enc, HeVAL (he)); |
252 | } |
342 | } |
253 | |
343 | |
254 | // compare hash entries, used when all keys are bytestrings |
344 | // compare hash entries, used when all keys are bytestrings |
255 | static int |
345 | static int |
… | |
… | |
261 | HE *b = *(HE **)b_; |
351 | HE *b = *(HE **)b_; |
262 | |
352 | |
263 | STRLEN la = HeKLEN (a); |
353 | STRLEN la = HeKLEN (a); |
264 | STRLEN lb = HeKLEN (b); |
354 | STRLEN lb = HeKLEN (b); |
265 | |
355 | |
266 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
356 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
267 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
357 | cmp = la - lb; |
268 | |
358 | |
269 | return cmp; |
359 | return cmp; |
270 | } |
360 | } |
271 | |
361 | |
272 | // compare hash entries, used when some keys are sv's or utf-x |
362 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
279 | static void |
369 | static void |
280 | encode_hv (enc_t *enc, HV *hv) |
370 | encode_hv (enc_t *enc, HV *hv) |
281 | { |
371 | { |
282 | int count, i; |
372 | int count, i; |
283 | |
373 | |
|
|
374 | if (enc->indent >= enc->maxdepth) |
|
|
375 | croak ("data structure too deep (hit recursion limit)"); |
|
|
376 | |
284 | encode_ch (enc, '{'); NL; ++enc->indent; |
377 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
285 | |
378 | |
286 | if ((count = hv_iterinit (hv))) |
379 | if ((count = hv_iterinit (hv))) |
287 | { |
380 | { |
288 | // for canonical output we have to sort by keys first |
381 | // for canonical output we have to sort by keys first |
289 | // actually, this is mostly due to the stupid so-called |
382 | // actually, this is mostly due to the stupid so-called |
290 | // security workaround added somewhere in 5.8.x. |
383 | // security workaround added somewhere in 5.8.x. |
291 | // that randomises hash orderings |
384 | // that randomises hash orderings |
292 | if (enc->flags & F_CANONICAL) |
385 | if (enc->flags & F_CANONICAL) |
293 | { |
386 | { |
294 | HE *he, *hes [count]; |
|
|
295 | int fast = 1; |
387 | int fast = 1; |
|
|
388 | HE *he; |
|
|
389 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
390 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
391 | #else |
|
|
392 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
393 | #endif |
296 | |
394 | |
297 | i = 0; |
395 | i = 0; |
298 | while ((he = hv_iternext (hv))) |
396 | while ((he = hv_iternext (hv))) |
299 | { |
397 | { |
300 | hes [i++] = he; |
398 | hes [i++] = he; |
… | |
… | |
306 | |
404 | |
307 | if (fast) |
405 | if (fast) |
308 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
406 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
309 | else |
407 | else |
310 | { |
408 | { |
311 | // hack to disable "use bytes" |
409 | // hack to forcefully disable "use bytes" |
312 | COP *oldcop = PL_curcop, cop; |
410 | COP cop = *PL_curcop; |
313 | cop.op_private = 0; |
411 | cop.op_private = 0; |
|
|
412 | |
|
|
413 | ENTER; |
|
|
414 | SAVETMPS; |
|
|
415 | |
|
|
416 | SAVEVPTR (PL_curcop); |
314 | PL_curcop = &cop; |
417 | PL_curcop = &cop; |
315 | |
418 | |
316 | SAVETMPS; |
|
|
317 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
419 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
420 | |
318 | FREETMPS; |
421 | FREETMPS; |
319 | |
422 | LEAVE; |
320 | PL_curcop = oldcop; |
|
|
321 | } |
423 | } |
322 | |
424 | |
323 | for (i = 0; i < count; ++i) |
425 | for (i = 0; i < count; ++i) |
324 | { |
426 | { |
325 | INDENT; |
427 | encode_indent (enc); |
326 | encode_he (enc, hes [i]); |
428 | encode_he (enc, hes [i]); |
327 | |
429 | |
328 | if (i < count - 1) |
430 | if (i < count - 1) |
329 | COMMA; |
431 | encode_comma (enc); |
330 | } |
432 | } |
331 | |
433 | |
332 | NL; |
434 | encode_nl (enc); |
333 | } |
435 | } |
334 | else |
436 | else |
335 | { |
437 | { |
336 | SV *sv; |
|
|
337 | HE *he = hv_iternext (hv); |
438 | HE *he = hv_iternext (hv); |
338 | |
439 | |
339 | for (;;) |
440 | for (;;) |
340 | { |
441 | { |
341 | INDENT; |
442 | encode_indent (enc); |
342 | encode_he (enc, he); |
443 | encode_he (enc, he); |
343 | |
444 | |
344 | if (!(he = hv_iternext (hv))) |
445 | if (!(he = hv_iternext (hv))) |
345 | break; |
446 | break; |
346 | |
447 | |
347 | COMMA; |
448 | encode_comma (enc); |
348 | } |
449 | } |
349 | |
450 | |
350 | NL; |
451 | encode_nl (enc); |
351 | } |
452 | } |
352 | } |
453 | } |
353 | |
454 | |
354 | --enc->indent; INDENT; encode_ch (enc, '}'); |
455 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
456 | } |
|
|
457 | |
|
|
458 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
459 | static void |
|
|
460 | encode_rv (enc_t *enc, SV *sv) |
|
|
461 | { |
|
|
462 | svtype svt; |
|
|
463 | |
|
|
464 | SvGETMAGIC (sv); |
|
|
465 | svt = SvTYPE (sv); |
|
|
466 | |
|
|
467 | if (svt == SVt_PVHV) |
|
|
468 | encode_hv (enc, (HV *)sv); |
|
|
469 | else if (svt == SVt_PVAV) |
|
|
470 | encode_av (enc, (AV *)sv); |
|
|
471 | else if (svt < SVt_PVAV) |
|
|
472 | { |
|
|
473 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
474 | encode_str (enc, "false", 5, 0); |
|
|
475 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
476 | encode_str (enc, "true", 4, 0); |
|
|
477 | else |
|
|
478 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
479 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
480 | } |
|
|
481 | else |
|
|
482 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
483 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
355 | } |
484 | } |
356 | |
485 | |
357 | static void |
486 | static void |
358 | encode_sv (enc_t *enc, SV *sv) |
487 | encode_sv (enc_t *enc, SV *sv) |
359 | { |
488 | { |
… | |
… | |
367 | encode_str (enc, str, len, SvUTF8 (sv)); |
496 | encode_str (enc, str, len, SvUTF8 (sv)); |
368 | encode_ch (enc, '"'); |
497 | encode_ch (enc, '"'); |
369 | } |
498 | } |
370 | else if (SvNOKp (sv)) |
499 | else if (SvNOKp (sv)) |
371 | { |
500 | { |
|
|
501 | // trust that perl will do the right thing w.r.t. JSON syntax. |
372 | need (enc, NV_DIG + 32); |
502 | need (enc, NV_DIG + 32); |
373 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
374 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
375 | } |
505 | } |
376 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
377 | { |
507 | { |
378 | need (enc, 64); |
508 | // we assume we can always read an IV as a UV |
|
|
509 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
510 | { |
|
|
511 | // large integer, use the (rather slow) snprintf way. |
|
|
512 | need (enc, sizeof (UV) * 3); |
379 | enc->cur += |
513 | enc->cur += |
380 | SvIsUV(sv) |
514 | SvIsUV(sv) |
381 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
515 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
382 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
516 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
517 | } |
|
|
518 | else |
|
|
519 | { |
|
|
520 | // optimise the "small number case" |
|
|
521 | // code will likely be branchless and use only a single multiplication |
|
|
522 | I32 i = SvIV (sv); |
|
|
523 | U32 u; |
|
|
524 | char digit, nz = 0; |
|
|
525 | |
|
|
526 | need (enc, 6); |
|
|
527 | |
|
|
528 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
529 | u = i < 0 ? -i : i; |
|
|
530 | |
|
|
531 | // convert to 4.28 fixed-point representation |
|
|
532 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
533 | |
|
|
534 | // now output digit by digit, each time masking out the integer part |
|
|
535 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
536 | // resulting in a net multiplication by 10. |
|
|
537 | // we always write the digit to memory but conditionally increment |
|
|
538 | // the pointer, to ease the usage of conditional move instructions. |
|
|
539 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
540 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
541 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
542 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
543 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
544 | } |
383 | } |
545 | } |
384 | else if (SvROK (sv)) |
546 | else if (SvROK (sv)) |
385 | { |
547 | encode_rv (enc, SvRV (sv)); |
386 | if (!--enc->max_recurse) |
|
|
387 | croak ("data structure too deep (hit recursion limit)"); |
|
|
388 | |
|
|
389 | sv = SvRV (sv); |
|
|
390 | |
|
|
391 | switch (SvTYPE (sv)) |
|
|
392 | { |
|
|
393 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
|
|
394 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
|
|
395 | |
|
|
396 | default: |
|
|
397 | croak ("JSON can only represent references to arrays or hashes"); |
|
|
398 | } |
|
|
399 | } |
|
|
400 | else if (!SvOK (sv)) |
548 | else if (!SvOK (sv)) |
401 | encode_str (enc, "null", 4, 0); |
549 | encode_str (enc, "null", 4, 0); |
402 | else |
550 | else |
403 | croak ("encountered perl type that JSON cannot handle"); |
551 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
552 | SvPV_nolen (sv), SvFLAGS (sv)); |
404 | } |
553 | } |
405 | |
554 | |
406 | static SV * |
555 | static SV * |
407 | encode_json (SV *scalar, UV flags) |
556 | encode_json (SV *scalar, U32 flags) |
408 | { |
557 | { |
|
|
558 | enc_t enc; |
|
|
559 | |
409 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
560 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
410 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
561 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
411 | |
562 | |
412 | enc_t enc; |
|
|
413 | enc.flags = flags; |
563 | enc.flags = flags; |
414 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
564 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
415 | enc.cur = SvPVX (enc.sv); |
565 | enc.cur = SvPVX (enc.sv); |
416 | enc.end = SvEND (enc.sv); |
566 | enc.end = SvEND (enc.sv); |
417 | enc.max_recurse = 0; |
|
|
418 | enc.indent = 0; |
567 | enc.indent = 0; |
|
|
568 | enc.maxdepth = DEC_DEPTH (flags); |
419 | |
569 | |
420 | SvPOK_only (enc.sv); |
570 | SvPOK_only (enc.sv); |
421 | encode_sv (&enc, scalar); |
571 | encode_sv (&enc, scalar); |
422 | |
572 | |
|
|
573 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
574 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
575 | |
423 | if (!(flags & (F_ASCII | F_UTF8))) |
576 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
424 | SvUTF8_on (enc.sv); |
577 | SvUTF8_on (enc.sv); |
425 | |
578 | |
426 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
579 | if (enc.flags & F_SHRINK) |
|
|
580 | shrink (enc.sv); |
|
|
581 | |
427 | return enc.sv; |
582 | return enc.sv; |
428 | } |
583 | } |
429 | |
584 | |
430 | ///////////////////////////////////////////////////////////////////////////// |
585 | ///////////////////////////////////////////////////////////////////////////// |
|
|
586 | // decoder |
431 | |
587 | |
432 | #define WS \ |
588 | // structure used for decoding JSON |
|
|
589 | typedef struct |
|
|
590 | { |
|
|
591 | char *cur; // current parser pointer |
|
|
592 | char *end; // end of input string |
|
|
593 | const char *err; // parse error, if != 0 |
|
|
594 | U32 flags; // F_* |
|
|
595 | U32 depth; // recursion depth |
|
|
596 | U32 maxdepth; // recursion depth limit |
|
|
597 | } dec_t; |
|
|
598 | |
|
|
599 | inline void |
|
|
600 | decode_ws (dec_t *dec) |
|
|
601 | { |
433 | for (;;) \ |
602 | for (;;) |
434 | { \ |
603 | { |
435 | char ch = *dec->cur; \ |
604 | char ch = *dec->cur; |
|
|
605 | |
436 | if (ch > 0x20 \ |
606 | if (ch > 0x20 |
437 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
607 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
438 | break; \ |
608 | break; |
|
|
609 | |
439 | ++dec->cur; \ |
610 | ++dec->cur; |
440 | } |
611 | } |
|
|
612 | } |
441 | |
613 | |
442 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
614 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
615 | |
443 | #define EXPECT_CH(ch) SB \ |
616 | #define EXPECT_CH(ch) SB \ |
444 | if (*dec->cur != ch) \ |
617 | if (*dec->cur != ch) \ |
445 | ERR (# ch " expected"); \ |
618 | ERR (# ch " expected"); \ |
446 | ++dec->cur; \ |
619 | ++dec->cur; \ |
447 | SE |
620 | SE |
448 | |
621 | |
|
|
622 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
623 | #define DEC_DEC_DEPTH --dec->depth |
|
|
624 | |
449 | static SV *decode_sv (dec_t *dec); |
625 | static SV *decode_sv (dec_t *dec); |
450 | |
626 | |
451 | static signed char decode_hexdigit[256]; |
627 | static signed char decode_hexdigit[256]; |
452 | |
628 | |
453 | static UV |
629 | static UV |
454 | decode_4hex (dec_t *dec) |
630 | decode_4hex (dec_t *dec) |
455 | { |
631 | { |
456 | signed char d1, d2, d3, d4; |
632 | signed char d1, d2, d3, d4; |
|
|
633 | unsigned char *cur = (unsigned char *)dec->cur; |
457 | |
634 | |
458 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
635 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
459 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
636 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
460 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
637 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
461 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
638 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
462 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
463 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
|
|
464 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
465 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
|
|
466 | |
639 | |
467 | dec->cur += 4; |
640 | dec->cur += 4; |
468 | |
641 | |
469 | return ((UV)d1) << 12 |
642 | return ((UV)d1) << 12 |
470 | | ((UV)d2) << 8 |
643 | | ((UV)d2) << 8 |
… | |
… | |
473 | |
646 | |
474 | fail: |
647 | fail: |
475 | return (UV)-1; |
648 | return (UV)-1; |
476 | } |
649 | } |
477 | |
650 | |
478 | #define APPEND_GROW(n) SB \ |
|
|
479 | if (cur + (n) >= end) \ |
|
|
480 | { \ |
|
|
481 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
482 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
483 | cur = SvPVX (sv) + ofs; \ |
|
|
484 | end = SvEND (sv); \ |
|
|
485 | } \ |
|
|
486 | SE |
|
|
487 | |
|
|
488 | #define APPEND_CH(ch) SB \ |
|
|
489 | APPEND_GROW (1); \ |
|
|
490 | *cur++ = (ch); \ |
|
|
491 | SE |
|
|
492 | |
|
|
493 | static SV * |
651 | static SV * |
494 | decode_str (dec_t *dec) |
652 | decode_str (dec_t *dec) |
495 | { |
653 | { |
496 | SV *sv = NEWSV (0,2); |
654 | SV *sv = 0; |
497 | int utf8 = 0; |
655 | int utf8 = 0; |
498 | char *cur = SvPVX (sv); |
656 | char *dec_cur = dec->cur; |
499 | char *end = SvEND (sv); |
|
|
500 | |
657 | |
501 | for (;;) |
658 | do |
502 | { |
659 | { |
503 | unsigned char ch = *(unsigned char *)dec->cur; |
660 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
661 | char *cur = buf; |
504 | |
662 | |
505 | if (ch == '"') |
663 | do |
506 | break; |
|
|
507 | else if (ch == '\\') |
|
|
508 | { |
664 | { |
509 | switch (*++dec->cur) |
665 | unsigned char ch = *(unsigned char *)dec_cur++; |
|
|
666 | |
|
|
667 | if (expect_false (ch == '"')) |
510 | { |
668 | { |
511 | case '\\': |
669 | --dec_cur; |
512 | case '/': |
670 | break; |
513 | case '"': APPEND_CH (*dec->cur++); break; |
671 | } |
514 | |
672 | else if (expect_false (ch == '\\')) |
515 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
673 | { |
516 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
674 | switch (*dec_cur) |
517 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
518 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
519 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
520 | |
|
|
521 | case 'u': |
|
|
522 | { |
675 | { |
523 | UV lo, hi; |
676 | case '\\': |
524 | ++dec->cur; |
677 | case '/': |
|
|
678 | case '"': *cur++ = *dec_cur++; break; |
525 | |
679 | |
526 | hi = decode_4hex (dec); |
680 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
527 | if (hi == (UV)-1) |
681 | case 't': ++dec_cur; *cur++ = '\011'; break; |
528 | goto fail; |
682 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
|
|
683 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
|
|
684 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
529 | |
685 | |
530 | // possibly a surrogate pair |
686 | case 'u': |
531 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
532 | { |
687 | { |
533 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
688 | UV lo, hi; |
534 | ERR ("illegal surrogate character"); |
689 | ++dec_cur; |
535 | |
690 | |
536 | dec->cur += 2; |
691 | dec->cur = dec_cur; |
537 | |
|
|
538 | lo = decode_4hex (dec); |
692 | hi = decode_4hex (dec); |
|
|
693 | dec_cur = dec->cur; |
539 | if (lo == (UV)-1) |
694 | if (hi == (UV)-1) |
540 | goto fail; |
695 | goto fail; |
541 | |
696 | |
|
|
697 | // possibly a surrogate pair |
|
|
698 | if (hi >= 0xd800) |
|
|
699 | if (hi < 0xdc00) |
|
|
700 | { |
|
|
701 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
|
|
702 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
703 | |
|
|
704 | dec_cur += 2; |
|
|
705 | |
|
|
706 | dec->cur = dec_cur; |
|
|
707 | lo = decode_4hex (dec); |
|
|
708 | dec_cur = dec->cur; |
|
|
709 | if (lo == (UV)-1) |
|
|
710 | goto fail; |
|
|
711 | |
542 | if (lo < 0xdc00 || lo >= 0xe000) |
712 | if (lo < 0xdc00 || lo >= 0xe000) |
543 | ERR ("surrogate pair expected"); |
713 | ERR ("surrogate pair expected"); |
544 | |
714 | |
545 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
715 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
716 | } |
|
|
717 | else if (hi < 0xe000) |
|
|
718 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
719 | |
|
|
720 | if (hi >= 0x80) |
|
|
721 | { |
|
|
722 | utf8 = 1; |
|
|
723 | |
|
|
724 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
725 | } |
|
|
726 | else |
|
|
727 | *cur++ = hi; |
546 | } |
728 | } |
547 | else if (lo >= 0xdc00 && lo < 0xe000) |
|
|
548 | ERR ("illegal surrogate character"); |
|
|
549 | |
|
|
550 | if (hi >= 0x80) |
|
|
551 | { |
729 | break; |
552 | utf8 = 1; |
|
|
553 | |
730 | |
554 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
555 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
556 | } |
|
|
557 | else |
731 | default: |
558 | APPEND_CH (hi); |
732 | --dec_cur; |
|
|
733 | ERR ("illegal backslash escape sequence in string"); |
559 | } |
734 | } |
|
|
735 | } |
|
|
736 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
|
|
737 | *cur++ = ch; |
|
|
738 | else if (ch >= 0x80) |
|
|
739 | { |
|
|
740 | STRLEN clen; |
|
|
741 | UV uch; |
|
|
742 | |
|
|
743 | --dec_cur; |
|
|
744 | |
|
|
745 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
|
|
746 | if (clen == (STRLEN)-1) |
|
|
747 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
748 | |
|
|
749 | do |
|
|
750 | *cur++ = *dec_cur++; |
|
|
751 | while (--clen); |
|
|
752 | |
|
|
753 | utf8 = 1; |
|
|
754 | } |
|
|
755 | else |
|
|
756 | { |
|
|
757 | --dec_cur; |
|
|
758 | |
|
|
759 | if (!ch) |
|
|
760 | ERR ("unexpected end of string while parsing JSON string"); |
560 | break; |
761 | else |
|
|
762 | ERR ("invalid character encountered while parsing JSON string"); |
561 | } |
763 | } |
562 | } |
764 | } |
563 | else if (ch >= 0x20 && ch <= 0x7f) |
765 | while (cur < buf + SHORT_STRING_LEN); |
564 | APPEND_CH (*dec->cur++); |
766 | |
565 | else if (ch >= 0x80) |
767 | { |
|
|
768 | STRLEN len = cur - buf; |
|
|
769 | |
|
|
770 | if (sv) |
566 | { |
771 | { |
567 | STRLEN clen; |
772 | SvGROW (sv, SvCUR (sv) + len + 1); |
568 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
773 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
569 | if (clen < 0) |
774 | SvCUR_set (sv, SvCUR (sv) + len); |
570 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
571 | |
|
|
572 | APPEND_GROW (clen); |
|
|
573 | memcpy (cur, dec->cur, clen); |
|
|
574 | cur += clen; |
|
|
575 | dec->cur += clen; |
|
|
576 | } |
775 | } |
577 | else |
776 | else |
578 | ERR ("invalid character encountered"); |
777 | sv = newSVpvn (buf, len); |
579 | } |
778 | } |
|
|
779 | } |
|
|
780 | while (*dec_cur != '"'); |
580 | |
781 | |
581 | ++dec->cur; |
782 | ++dec_cur; |
582 | |
783 | |
583 | SvCUR_set (sv, cur - SvPVX (sv)); |
784 | if (sv) |
584 | |
785 | { |
585 | SvPOK_only (sv); |
786 | SvPOK_only (sv); |
586 | *SvEND (sv) = 0; |
787 | *SvEND (sv) = 0; |
587 | |
788 | |
588 | if (utf8) |
789 | if (utf8) |
589 | SvUTF8_on (sv); |
790 | SvUTF8_on (sv); |
|
|
791 | } |
|
|
792 | else |
|
|
793 | sv = newSVpvn ("", 0); |
590 | |
794 | |
|
|
795 | dec->cur = dec_cur; |
591 | return sv; |
796 | return sv; |
592 | |
797 | |
593 | fail: |
798 | fail: |
594 | SvREFCNT_dec (sv); |
799 | dec->cur = dec_cur; |
595 | return 0; |
800 | return 0; |
596 | } |
801 | } |
597 | |
802 | |
598 | static SV * |
803 | static SV * |
599 | decode_num (dec_t *dec) |
804 | decode_num (dec_t *dec) |
… | |
… | |
609 | { |
814 | { |
610 | ++dec->cur; |
815 | ++dec->cur; |
611 | if (*dec->cur >= '0' && *dec->cur <= '9') |
816 | if (*dec->cur >= '0' && *dec->cur <= '9') |
612 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
817 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
613 | } |
818 | } |
614 | |
819 | else if (*dec->cur < '0' || *dec->cur > '9') |
615 | // int |
820 | ERR ("malformed number (no digits after initial minus)"); |
|
|
821 | else |
|
|
822 | do |
|
|
823 | { |
|
|
824 | ++dec->cur; |
|
|
825 | } |
616 | while (*dec->cur >= '0' && *dec->cur <= '9') |
826 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
617 | ++dec->cur; |
|
|
618 | |
827 | |
619 | // [frac] |
828 | // [frac] |
620 | if (*dec->cur == '.') |
829 | if (*dec->cur == '.') |
621 | { |
830 | { |
622 | is_nv = 1; |
831 | ++dec->cur; |
|
|
832 | |
|
|
833 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
834 | ERR ("malformed number (no digits after decimal point)"); |
623 | |
835 | |
624 | do |
836 | do |
625 | { |
837 | { |
626 | ++dec->cur; |
838 | ++dec->cur; |
627 | } |
839 | } |
628 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
840 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
841 | |
|
|
842 | is_nv = 1; |
629 | } |
843 | } |
630 | |
844 | |
631 | // [exp] |
845 | // [exp] |
632 | if (*dec->cur == 'e' || *dec->cur == 'E') |
846 | if (*dec->cur == 'e' || *dec->cur == 'E') |
633 | { |
847 | { |
634 | is_nv = 1; |
|
|
635 | |
|
|
636 | ++dec->cur; |
848 | ++dec->cur; |
|
|
849 | |
637 | if (*dec->cur == '-' || *dec->cur == '+') |
850 | if (*dec->cur == '-' || *dec->cur == '+') |
638 | ++dec->cur; |
851 | ++dec->cur; |
639 | |
852 | |
|
|
853 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
854 | ERR ("malformed number (no digits after exp sign)"); |
|
|
855 | |
|
|
856 | do |
|
|
857 | { |
|
|
858 | ++dec->cur; |
|
|
859 | } |
640 | while (*dec->cur >= '0' && *dec->cur <= '9') |
860 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
641 | ++dec->cur; |
861 | |
|
|
862 | is_nv = 1; |
642 | } |
863 | } |
643 | |
864 | |
644 | if (!is_nv) |
865 | if (!is_nv) |
645 | { |
866 | { |
646 | UV uv; |
867 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
647 | int numtype = grok_number (start, dec->cur - start, &uv); |
868 | if (*start == '-') |
648 | if (numtype & IS_NUMBER_IN_UV) |
869 | switch (dec->cur - start) |
649 | if (numtype & IS_NUMBER_NEG) |
|
|
650 | { |
870 | { |
651 | if (uv < (UV)IV_MIN) |
871 | case 2: return newSViv (-( start [1] - '0' * 1)); |
652 | return newSViv (-(IV)uv); |
872 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
873 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
874 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
653 | } |
875 | } |
|
|
876 | else |
|
|
877 | switch (dec->cur - start) |
|
|
878 | { |
|
|
879 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
880 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
881 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
882 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
883 | } |
|
|
884 | |
|
|
885 | { |
|
|
886 | UV uv; |
|
|
887 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
888 | if (numtype & IS_NUMBER_IN_UV) |
|
|
889 | if (numtype & IS_NUMBER_NEG) |
|
|
890 | { |
|
|
891 | if (uv < (UV)IV_MIN) |
|
|
892 | return newSViv (-(IV)uv); |
|
|
893 | } |
654 | else |
894 | else |
655 | return newSVuv (uv); |
895 | return newSVuv (uv); |
|
|
896 | |
|
|
897 | // here would likely be the place for bigint support |
656 | } |
898 | } |
|
|
899 | } |
657 | |
900 | |
|
|
901 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
658 | return newSVnv (Atof (start)); |
902 | return newSVnv (Atof (start)); |
659 | |
903 | |
660 | fail: |
904 | fail: |
661 | return 0; |
905 | return 0; |
662 | } |
906 | } |
… | |
… | |
664 | static SV * |
908 | static SV * |
665 | decode_av (dec_t *dec) |
909 | decode_av (dec_t *dec) |
666 | { |
910 | { |
667 | AV *av = newAV (); |
911 | AV *av = newAV (); |
668 | |
912 | |
|
|
913 | DEC_INC_DEPTH; |
|
|
914 | decode_ws (dec); |
|
|
915 | |
|
|
916 | if (*dec->cur == ']') |
|
|
917 | ++dec->cur; |
|
|
918 | else |
669 | for (;;) |
919 | for (;;) |
670 | { |
920 | { |
671 | SV *value; |
921 | SV *value; |
672 | |
922 | |
673 | value = decode_sv (dec); |
923 | value = decode_sv (dec); |
674 | if (!value) |
924 | if (!value) |
675 | goto fail; |
925 | goto fail; |
676 | |
926 | |
677 | av_push (av, value); |
927 | av_push (av, value); |
678 | |
928 | |
679 | WS; |
929 | decode_ws (dec); |
680 | |
930 | |
681 | if (*dec->cur == ']') |
931 | if (*dec->cur == ']') |
682 | { |
932 | { |
683 | ++dec->cur; |
933 | ++dec->cur; |
684 | break; |
934 | break; |
|
|
935 | } |
685 | } |
936 | |
686 | |
|
|
687 | if (*dec->cur != ',') |
937 | if (*dec->cur != ',') |
688 | ERR (", or ] expected while parsing array"); |
938 | ERR (", or ] expected while parsing array"); |
689 | |
939 | |
690 | ++dec->cur; |
940 | ++dec->cur; |
691 | } |
941 | } |
692 | |
942 | |
|
|
943 | DEC_DEC_DEPTH; |
693 | return newRV_noinc ((SV *)av); |
944 | return newRV_noinc ((SV *)av); |
694 | |
945 | |
695 | fail: |
946 | fail: |
696 | SvREFCNT_dec (av); |
947 | SvREFCNT_dec (av); |
|
|
948 | DEC_DEC_DEPTH; |
697 | return 0; |
949 | return 0; |
698 | } |
950 | } |
699 | |
951 | |
700 | static SV * |
952 | static SV * |
701 | decode_hv (dec_t *dec) |
953 | decode_hv (dec_t *dec) |
702 | { |
954 | { |
703 | HV *hv = newHV (); |
955 | HV *hv = newHV (); |
704 | |
956 | |
|
|
957 | DEC_INC_DEPTH; |
|
|
958 | decode_ws (dec); |
|
|
959 | |
|
|
960 | if (*dec->cur == '}') |
|
|
961 | ++dec->cur; |
|
|
962 | else |
705 | for (;;) |
963 | for (;;) |
706 | { |
964 | { |
707 | SV *key, *value; |
965 | SV *key, *value; |
708 | |
966 | |
709 | WS; EXPECT_CH ('"'); |
967 | decode_ws (dec); EXPECT_CH ('"'); |
710 | |
968 | |
711 | key = decode_str (dec); |
969 | key = decode_str (dec); |
712 | if (!key) |
970 | if (!key) |
713 | goto fail; |
|
|
714 | |
|
|
715 | WS; EXPECT_CH (':'); |
|
|
716 | |
|
|
717 | value = decode_sv (dec); |
|
|
718 | if (!value) |
|
|
719 | { |
|
|
720 | SvREFCNT_dec (key); |
|
|
721 | goto fail; |
971 | goto fail; |
|
|
972 | |
|
|
973 | decode_ws (dec); EXPECT_CH (':'); |
|
|
974 | |
|
|
975 | value = decode_sv (dec); |
|
|
976 | if (!value) |
|
|
977 | { |
|
|
978 | SvREFCNT_dec (key); |
|
|
979 | goto fail; |
722 | } |
980 | } |
723 | |
981 | |
724 | //TODO: optimise |
|
|
725 | hv_store_ent (hv, key, value, 0); |
982 | hv_store_ent (hv, key, value, 0); |
|
|
983 | SvREFCNT_dec (key); |
726 | |
984 | |
727 | WS; |
985 | decode_ws (dec); |
728 | |
986 | |
729 | if (*dec->cur == '}') |
987 | if (*dec->cur == '}') |
730 | { |
988 | { |
731 | ++dec->cur; |
989 | ++dec->cur; |
732 | break; |
990 | break; |
733 | } |
991 | } |
734 | |
992 | |
735 | if (*dec->cur != ',') |
993 | if (*dec->cur != ',') |
736 | ERR (", or } expected while parsing object/hash"); |
994 | ERR (", or } expected while parsing object/hash"); |
737 | |
995 | |
738 | ++dec->cur; |
996 | ++dec->cur; |
739 | } |
997 | } |
740 | |
998 | |
|
|
999 | DEC_DEC_DEPTH; |
741 | return newRV_noinc ((SV *)hv); |
1000 | return newRV_noinc ((SV *)hv); |
742 | |
1001 | |
743 | fail: |
1002 | fail: |
744 | SvREFCNT_dec (hv); |
1003 | SvREFCNT_dec (hv); |
|
|
1004 | DEC_DEC_DEPTH; |
745 | return 0; |
1005 | return 0; |
746 | } |
1006 | } |
747 | |
1007 | |
748 | static SV * |
1008 | static SV * |
749 | decode_sv (dec_t *dec) |
1009 | decode_sv (dec_t *dec) |
750 | { |
1010 | { |
751 | WS; |
1011 | decode_ws (dec); |
|
|
1012 | |
|
|
1013 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1014 | // to parse anything. |
752 | switch (*dec->cur) |
1015 | switch (*dec->cur) |
753 | { |
1016 | { |
754 | case '"': ++dec->cur; return decode_str (dec); |
1017 | case '"': ++dec->cur; return decode_str (dec); |
755 | case '[': ++dec->cur; return decode_av (dec); |
1018 | case '[': ++dec->cur; return decode_av (dec); |
756 | case '{': ++dec->cur; return decode_hv (dec); |
1019 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
784 | |
1047 | |
785 | case 'n': |
1048 | case 'n': |
786 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
1049 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
787 | { |
1050 | { |
788 | dec->cur += 4; |
1051 | dec->cur += 4; |
789 | return newSViv (1); |
1052 | return newSVsv (&PL_sv_undef); |
790 | } |
1053 | } |
791 | else |
1054 | else |
792 | ERR ("'null' expected"); |
1055 | ERR ("'null' expected"); |
793 | |
1056 | |
794 | break; |
1057 | break; |
795 | |
1058 | |
796 | default: |
1059 | default: |
797 | ERR ("malformed json string"); |
1060 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
798 | break; |
1061 | break; |
799 | } |
1062 | } |
800 | |
1063 | |
801 | fail: |
1064 | fail: |
802 | return 0; |
1065 | return 0; |
803 | } |
1066 | } |
804 | |
1067 | |
805 | static SV * |
1068 | static SV * |
806 | decode_json (SV *string, UV flags) |
1069 | decode_json (SV *string, U32 flags, UV *offset_return) |
807 | { |
1070 | { |
|
|
1071 | dec_t dec; |
|
|
1072 | UV offset; |
808 | SV *sv; |
1073 | SV *sv; |
809 | |
1074 | |
|
|
1075 | SvGETMAGIC (string); |
|
|
1076 | SvUPGRADE (string, SVt_PV); |
|
|
1077 | |
810 | if (!(flags & F_UTF8)) |
1078 | if (flags & F_UTF8) |
|
|
1079 | sv_utf8_downgrade (string, 0); |
|
|
1080 | else |
811 | sv_utf8_upgrade (string); |
1081 | sv_utf8_upgrade (string); |
812 | |
1082 | |
813 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1083 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
814 | |
1084 | |
815 | dec_t dec; |
|
|
816 | dec.flags = flags; |
1085 | dec.flags = flags; |
817 | dec.cur = SvPVX (string); |
1086 | dec.cur = SvPVX (string); |
818 | dec.end = SvEND (string); |
1087 | dec.end = SvEND (string); |
819 | dec.err = 0; |
1088 | dec.err = 0; |
|
|
1089 | dec.depth = 0; |
|
|
1090 | dec.maxdepth = DEC_DEPTH (dec.flags); |
820 | |
1091 | |
821 | *dec.end = 1; // invalid anywhere |
1092 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
822 | sv = decode_sv (&dec); |
1093 | sv = decode_sv (&dec); |
823 | *dec.end = 0; |
1094 | |
|
|
1095 | if (!(offset_return || !sv)) |
|
|
1096 | { |
|
|
1097 | // check for trailing garbage |
|
|
1098 | decode_ws (&dec); |
|
|
1099 | |
|
|
1100 | if (*dec.cur) |
|
|
1101 | { |
|
|
1102 | dec.err = "garbage after JSON object"; |
|
|
1103 | SvREFCNT_dec (sv); |
|
|
1104 | sv = 0; |
|
|
1105 | } |
|
|
1106 | } |
|
|
1107 | |
|
|
1108 | if (offset_return || !sv) |
|
|
1109 | { |
|
|
1110 | offset = dec.flags & F_UTF8 |
|
|
1111 | ? dec.cur - SvPVX (string) |
|
|
1112 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1113 | |
|
|
1114 | if (offset_return) |
|
|
1115 | *offset_return = offset; |
|
|
1116 | } |
824 | |
1117 | |
825 | if (!sv) |
1118 | if (!sv) |
826 | { |
1119 | { |
827 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
|
|
828 | SV *uni = sv_newmortal (); |
1120 | SV *uni = sv_newmortal (); |
829 | |
1121 | |
|
|
1122 | // horrible hack to silence warning inside pv_uni_display |
|
|
1123 | COP cop = *PL_curcop; |
|
|
1124 | cop.cop_warnings = pWARN_NONE; |
|
|
1125 | ENTER; |
|
|
1126 | SAVEVPTR (PL_curcop); |
|
|
1127 | PL_curcop = &cop; |
830 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1128 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1129 | LEAVE; |
|
|
1130 | |
831 | croak ("%s, at character %d (%s)", |
1131 | croak ("%s, at character offset %d [\"%s\"]", |
832 | dec.err, |
1132 | dec.err, |
833 | (int)offset, |
1133 | (int)offset, |
834 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1134 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
835 | } |
1135 | } |
836 | |
1136 | |
837 | sv = sv_2mortal (sv); |
1137 | sv = sv_2mortal (sv); |
838 | |
1138 | |
839 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1139 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
840 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
1140 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
841 | |
1141 | |
842 | return sv; |
1142 | return sv; |
843 | } |
1143 | } |
844 | |
1144 | |
|
|
1145 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1146 | // XS interface functions |
|
|
1147 | |
845 | MODULE = JSON::XS PACKAGE = JSON::XS |
1148 | MODULE = JSON::XS PACKAGE = JSON::XS |
846 | |
1149 | |
847 | BOOT: |
1150 | BOOT: |
848 | { |
1151 | { |
849 | int i; |
1152 | int i; |
850 | |
1153 | |
851 | memset (decode_hexdigit, 0xff, 256); |
|
|
852 | for (i = 10; i--; ) |
1154 | for (i = 0; i < 256; ++i) |
853 | decode_hexdigit ['0' + i] = i; |
1155 | decode_hexdigit [i] = |
854 | |
1156 | i >= '0' && i <= '9' ? i - '0' |
855 | for (i = 7; i--; ) |
1157 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
856 | { |
1158 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
857 | decode_hexdigit ['a' + i] = 10 + i; |
1159 | : -1; |
858 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
859 | } |
|
|
860 | |
1160 | |
861 | json_stash = gv_stashpv ("JSON::XS", 1); |
1161 | json_stash = gv_stashpv ("JSON::XS", 1); |
862 | } |
1162 | } |
863 | |
1163 | |
864 | PROTOTYPES: DISABLE |
1164 | PROTOTYPES: DISABLE |
… | |
… | |
867 | CODE: |
1167 | CODE: |
868 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1168 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
869 | OUTPUT: |
1169 | OUTPUT: |
870 | RETVAL |
1170 | RETVAL |
871 | |
1171 | |
872 | SV *ascii (SV *self, int enable) |
1172 | SV *ascii (SV *self, int enable = 1) |
873 | ALIAS: |
1173 | ALIAS: |
874 | ascii = F_ASCII |
1174 | ascii = F_ASCII |
|
|
1175 | latin1 = F_LATIN1 |
875 | utf8 = F_UTF8 |
1176 | utf8 = F_UTF8 |
876 | indent = F_INDENT |
1177 | indent = F_INDENT |
877 | canonical = F_CANONICAL |
1178 | canonical = F_CANONICAL |
878 | space_before = F_SPACE_BEFORE |
1179 | space_before = F_SPACE_BEFORE |
879 | space_after = F_SPACE_AFTER |
1180 | space_after = F_SPACE_AFTER |
880 | json_rpc = F_JSON_RPC |
|
|
881 | pretty = F_PRETTY |
1181 | pretty = F_PRETTY |
882 | allow_nonref = F_ALLOW_NONREF |
1182 | allow_nonref = F_ALLOW_NONREF |
|
|
1183 | shrink = F_SHRINK |
883 | CODE: |
1184 | CODE: |
884 | { |
1185 | { |
885 | UV *uv = SvJSON (self); |
1186 | UV *uv = SvJSON (self); |
886 | if (enable) |
1187 | if (enable) |
887 | *uv |= ix; |
1188 | *uv |= ix; |
… | |
… | |
891 | RETVAL = newSVsv (self); |
1192 | RETVAL = newSVsv (self); |
892 | } |
1193 | } |
893 | OUTPUT: |
1194 | OUTPUT: |
894 | RETVAL |
1195 | RETVAL |
895 | |
1196 | |
|
|
1197 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1198 | CODE: |
|
|
1199 | { |
|
|
1200 | UV *uv = SvJSON (self); |
|
|
1201 | UV log2 = 0; |
|
|
1202 | |
|
|
1203 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1204 | |
|
|
1205 | while ((1UL << log2) < max_depth) |
|
|
1206 | ++log2; |
|
|
1207 | |
|
|
1208 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1209 | |
|
|
1210 | RETVAL = newSVsv (self); |
|
|
1211 | } |
|
|
1212 | OUTPUT: |
|
|
1213 | RETVAL |
|
|
1214 | |
896 | void encode (SV *self, SV *scalar) |
1215 | void encode (SV *self, SV *scalar) |
897 | PPCODE: |
1216 | PPCODE: |
898 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1217 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
899 | |
1218 | |
900 | void decode (SV *self, SV *jsonstr) |
1219 | void decode (SV *self, SV *jsonstr) |
901 | PPCODE: |
1220 | PPCODE: |
902 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1221 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1222 | |
|
|
1223 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1224 | PPCODE: |
|
|
1225 | { |
|
|
1226 | UV offset; |
|
|
1227 | EXTEND (SP, 2); |
|
|
1228 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1229 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1230 | } |
903 | |
1231 | |
904 | PROTOTYPES: ENABLE |
1232 | PROTOTYPES: ENABLE |
905 | |
1233 | |
906 | void to_json (SV *scalar) |
1234 | void to_json (SV *scalar) |
|
|
1235 | ALIAS: |
|
|
1236 | objToJson = 0 |
907 | PPCODE: |
1237 | PPCODE: |
908 | XPUSHs (encode_json (scalar, F_UTF8)); |
1238 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
909 | |
1239 | |
910 | void from_json (SV *jsonstr) |
1240 | void from_json (SV *jsonstr) |
|
|
1241 | ALIAS: |
|
|
1242 | jsonToObj = 0 |
911 | PPCODE: |
1243 | PPCODE: |
912 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1244 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
913 | |
1245 | |