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