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