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