… | |
… | |
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 | ///////////////////////////////////////////////////////////////////////////// |
|
|
60 | |
104 | |
61 | static void |
105 | static void |
62 | need (enc_t *enc, STRLEN len) |
106 | need (enc_t *enc, STRLEN len) |
63 | { |
107 | { |
64 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
65 | { |
109 | { |
66 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
67 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
68 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
69 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
70 | } |
114 | } |
71 | } |
115 | } |
72 | |
116 | |
73 | static void |
117 | static void |
74 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
122 | STRLEN clen; |
166 | STRLEN clen; |
123 | UV uch; |
167 | UV uch; |
124 | |
168 | |
125 | if (is_utf8) |
169 | if (is_utf8) |
126 | { |
170 | { |
127 | 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); |
128 | if (clen == (STRLEN)-1) |
173 | if (clen == (STRLEN)-1) |
129 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
174 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
130 | } |
175 | } |
131 | else |
176 | else |
132 | { |
177 | { |
133 | uch = ch; |
178 | uch = ch; |
134 | clen = 1; |
179 | clen = 1; |
135 | } |
180 | } |
136 | |
181 | |
|
|
182 | if (uch > 0x10FFFFUL) |
|
|
183 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
184 | |
137 | if (uch < 0x80 || enc->flags & F_ASCII) |
185 | if (uch < 0x80 || enc->flags & F_ASCII) |
138 | { |
186 | { |
139 | if (uch > 0xFFFFUL) |
187 | if (uch > 0xFFFFUL) |
140 | { |
188 | { |
141 | need (enc, len += 11); |
189 | need (enc, len += 11); |
142 | sprintf (enc->cur, "\\u%04x\\u%04x", |
190 | sprintf (enc->cur, "\\u%04x\\u%04x", |
143 | (uch - 0x10000) / 0x400 + 0xD800, |
191 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
144 | (uch - 0x10000) % 0x400 + 0xDC00); |
192 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
145 | enc->cur += 12; |
193 | enc->cur += 12; |
146 | } |
194 | } |
147 | else |
195 | else |
148 | { |
196 | { |
149 | static char hexdigit [16] = "0123456789abcdef"; |
197 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
167 | } |
215 | } |
168 | while (--clen); |
216 | while (--clen); |
169 | } |
217 | } |
170 | else |
218 | else |
171 | { |
219 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
174 | ++str; |
222 | ++str; |
175 | } |
223 | } |
176 | } |
224 | } |
177 | } |
225 | } |
… | |
… | |
179 | |
227 | |
180 | --len; |
228 | --len; |
181 | } |
229 | } |
182 | } |
230 | } |
183 | |
231 | |
184 | #define INDENT SB \ |
232 | static void |
|
|
233 | encode_indent (enc_t *enc) |
|
|
234 | { |
185 | if (enc->flags & F_INDENT) \ |
235 | if (enc->flags & F_INDENT) |
186 | { \ |
236 | { |
187 | int i_; \ |
237 | 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 | |
238 | |
194 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
239 | need (enc, spaces); |
195 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
240 | memset (enc->cur, ' ', spaces); |
196 | #define COMMA SB \ |
241 | enc->cur += spaces; |
|
|
242 | } |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | encode_space (enc_t *enc) |
|
|
247 | { |
|
|
248 | need (enc, 1); |
197 | encode_ch (enc, ','); \ |
249 | encode_ch (enc, ' '); |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | encode_nl (enc_t *enc) |
|
|
254 | { |
198 | if (enc->flags & F_INDENT) \ |
255 | if (enc->flags & F_INDENT) |
199 | 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); |
200 | else if (enc->flags & F_SPACE_AFTER) \ |
269 | else if (enc->flags & F_SPACE_AFTER) |
201 | SPACE; \ |
270 | encode_space (enc); |
202 | SE |
271 | } |
203 | |
272 | |
204 | static void encode_sv (enc_t *enc, SV *sv); |
273 | static void encode_sv (enc_t *enc, SV *sv); |
205 | |
274 | |
206 | static void |
275 | static void |
207 | encode_av (enc_t *enc, AV *av) |
276 | encode_av (enc_t *enc, AV *av) |
208 | { |
277 | { |
209 | int i, len = av_len (av); |
278 | int i, len = av_len (av); |
210 | |
279 | |
211 | 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); |
212 | ++enc->indent; |
284 | ++enc->indent; |
213 | |
285 | |
214 | for (i = 0; i <= len; ++i) |
286 | for (i = 0; i <= len; ++i) |
215 | { |
287 | { |
216 | INDENT; |
288 | encode_indent (enc); |
217 | encode_sv (enc, *av_fetch (av, i, 0)); |
289 | encode_sv (enc, *av_fetch (av, i, 0)); |
218 | |
290 | |
219 | if (i < len) |
291 | if (i < len) |
220 | COMMA; |
292 | encode_comma (enc); |
221 | } |
293 | } |
222 | |
294 | |
223 | NL; |
295 | encode_nl (enc); |
224 | |
296 | |
225 | --enc->indent; |
297 | --enc->indent; |
226 | INDENT; encode_ch (enc, ']'); |
298 | encode_indent (enc); encode_ch (enc, ']'); |
227 | } |
299 | } |
228 | |
300 | |
229 | static void |
301 | static void |
230 | encode_he (enc_t *enc, HE *he) |
302 | encode_he (enc_t *enc, HE *he) |
231 | { |
303 | { |
… | |
… | |
245 | else |
317 | else |
246 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
318 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | |
319 | |
248 | encode_ch (enc, '"'); |
320 | encode_ch (enc, '"'); |
249 | |
321 | |
250 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
322 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
251 | encode_ch (enc, ':'); |
323 | encode_ch (enc, ':'); |
252 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
324 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
253 | encode_sv (enc, HeVAL (he)); |
325 | encode_sv (enc, HeVAL (he)); |
254 | } |
326 | } |
255 | |
327 | |
256 | // compare hash entries, used when all keys are bytestrings |
328 | // compare hash entries, used when all keys are bytestrings |
257 | static int |
329 | static int |
… | |
… | |
263 | HE *b = *(HE **)b_; |
335 | HE *b = *(HE **)b_; |
264 | |
336 | |
265 | STRLEN la = HeKLEN (a); |
337 | STRLEN la = HeKLEN (a); |
266 | STRLEN lb = HeKLEN (b); |
338 | STRLEN lb = HeKLEN (b); |
267 | |
339 | |
268 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
340 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
269 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
341 | cmp = la - lb; |
270 | |
342 | |
271 | return cmp; |
343 | return cmp; |
272 | } |
344 | } |
273 | |
345 | |
274 | // 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 |
… | |
… | |
281 | static void |
353 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
354 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
355 | { |
284 | int count, i; |
356 | int count, i; |
285 | |
357 | |
|
|
358 | if (enc->indent >= enc->maxdepth) |
|
|
359 | croak ("data structure too deep (hit recursion limit)"); |
|
|
360 | |
286 | encode_ch (enc, '{'); NL; ++enc->indent; |
361 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
287 | |
362 | |
288 | if ((count = hv_iterinit (hv))) |
363 | if ((count = hv_iterinit (hv))) |
289 | { |
364 | { |
290 | // for canonical output we have to sort by keys first |
365 | // for canonical output we have to sort by keys first |
291 | // actually, this is mostly due to the stupid so-called |
366 | // actually, this is mostly due to the stupid so-called |
292 | // security workaround added somewhere in 5.8.x. |
367 | // security workaround added somewhere in 5.8.x. |
293 | // that randomises hash orderings |
368 | // that randomises hash orderings |
294 | if (enc->flags & F_CANONICAL) |
369 | if (enc->flags & F_CANONICAL) |
295 | { |
370 | { |
296 | HE *he, *hes [count]; |
371 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
297 | int fast = 1; |
372 | int fast = 1; |
298 | |
373 | |
299 | i = 0; |
374 | i = 0; |
300 | while ((he = hv_iternext (hv))) |
375 | while ((he = hv_iternext (hv))) |
301 | { |
376 | { |
… | |
… | |
308 | |
383 | |
309 | if (fast) |
384 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
385 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
386 | else |
312 | { |
387 | { |
313 | // hack to disable "use bytes" |
388 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
389 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
390 | cop.op_private = 0; |
|
|
391 | |
|
|
392 | ENTER; |
|
|
393 | SAVETMPS; |
|
|
394 | |
|
|
395 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
396 | PL_curcop = &cop; |
317 | |
397 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
398 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
399 | |
320 | FREETMPS; |
400 | FREETMPS; |
321 | |
401 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
402 | } |
324 | |
403 | |
325 | for (i = 0; i < count; ++i) |
404 | for (i = 0; i < count; ++i) |
326 | { |
405 | { |
327 | INDENT; |
406 | encode_indent (enc); |
328 | encode_he (enc, hes [i]); |
407 | encode_he (enc, hes [i]); |
329 | |
408 | |
330 | if (i < count - 1) |
409 | if (i < count - 1) |
331 | COMMA; |
410 | encode_comma (enc); |
332 | } |
411 | } |
333 | |
412 | |
334 | NL; |
413 | encode_nl (enc); |
335 | } |
414 | } |
336 | else |
415 | else |
337 | { |
416 | { |
338 | SV *sv; |
|
|
339 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
340 | |
418 | |
341 | for (;;) |
419 | for (;;) |
342 | { |
420 | { |
343 | INDENT; |
421 | encode_indent (enc); |
344 | encode_he (enc, he); |
422 | encode_he (enc, he); |
345 | |
423 | |
346 | if (!(he = hv_iternext (hv))) |
424 | if (!(he = hv_iternext (hv))) |
347 | break; |
425 | break; |
348 | |
426 | |
349 | COMMA; |
427 | encode_comma (enc); |
350 | } |
428 | } |
351 | |
429 | |
352 | NL; |
430 | encode_nl (enc); |
353 | } |
431 | } |
354 | } |
432 | } |
355 | |
433 | |
356 | --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)))); |
357 | } |
463 | } |
358 | |
464 | |
359 | static void |
465 | static void |
360 | encode_sv (enc_t *enc, SV *sv) |
466 | encode_sv (enc_t *enc, SV *sv) |
361 | { |
467 | { |
… | |
… | |
382 | SvIsUV(sv) |
488 | SvIsUV(sv) |
383 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
384 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
385 | } |
491 | } |
386 | else if (SvROK (sv)) |
492 | else if (SvROK (sv)) |
387 | { |
493 | 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)) |
494 | else if (!SvOK (sv)) |
403 | encode_str (enc, "null", 4, 0); |
495 | encode_str (enc, "null", 4, 0); |
404 | else |
496 | else |
405 | croak ("encountered perl type that JSON cannot handle"); |
497 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
498 | SvPV_nolen (sv), SvFLAGS (sv)); |
406 | } |
499 | } |
407 | |
500 | |
408 | static SV * |
501 | static SV * |
409 | encode_json (SV *scalar, UV flags) |
502 | encode_json (SV *scalar, U32 flags) |
410 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
411 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
412 | croak ("hash- or arraref required (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)"); |
413 | |
508 | |
414 | enc_t enc; |
|
|
415 | enc.flags = flags; |
509 | enc.flags = flags; |
416 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
417 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
418 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
419 | enc.max_recurse = 0; |
|
|
420 | enc.indent = 0; |
513 | enc.indent = 0; |
|
|
514 | enc.maxdepth = DEC_DEPTH (flags); |
421 | |
515 | |
422 | SvPOK_only (enc.sv); |
516 | SvPOK_only (enc.sv); |
423 | encode_sv (&enc, scalar); |
517 | encode_sv (&enc, scalar); |
424 | |
518 | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
427 | |
521 | |
428 | 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 |
429 | |
524 | |
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
526 | shrink (enc.sv); |
433 | #endif |
527 | |
434 | return enc.sv; |
528 | return enc.sv; |
435 | } |
529 | } |
436 | |
530 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
531 | ///////////////////////////////////////////////////////////////////////////// |
|
|
532 | // decoder |
438 | |
533 | |
439 | #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 | { |
440 | for (;;) \ |
548 | for (;;) |
441 | { \ |
549 | { |
442 | char ch = *dec->cur; \ |
550 | char ch = *dec->cur; |
|
|
551 | |
443 | if (ch > 0x20 \ |
552 | if (ch > 0x20 |
444 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
553 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
445 | break; \ |
554 | break; |
|
|
555 | |
446 | ++dec->cur; \ |
556 | ++dec->cur; |
447 | } |
557 | } |
|
|
558 | } |
448 | |
559 | |
449 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
560 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
561 | |
450 | #define EXPECT_CH(ch) SB \ |
562 | #define EXPECT_CH(ch) SB \ |
451 | if (*dec->cur != ch) \ |
563 | if (*dec->cur != ch) \ |
452 | ERR (# ch " expected"); \ |
564 | ERR (# ch " expected"); \ |
453 | ++dec->cur; \ |
565 | ++dec->cur; \ |
454 | SE |
566 | SE |
455 | |
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 | |
456 | static SV *decode_sv (dec_t *dec); |
571 | static SV *decode_sv (dec_t *dec); |
457 | |
572 | |
458 | static signed char decode_hexdigit[256]; |
573 | static signed char decode_hexdigit[256]; |
459 | |
574 | |
460 | static UV |
575 | static UV |
461 | decode_4hex (dec_t *dec) |
576 | decode_4hex (dec_t *dec) |
462 | { |
577 | { |
463 | signed char d1, d2, d3, d4; |
578 | signed char d1, d2, d3, d4; |
|
|
579 | unsigned char *cur = (unsigned char *)dec->cur; |
464 | |
580 | |
465 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
466 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
581 | 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"); |
582 | 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"); |
583 | 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"); |
584 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
473 | |
585 | |
474 | dec->cur += 4; |
586 | dec->cur += 4; |
475 | |
587 | |
476 | return ((UV)d1) << 12 |
588 | return ((UV)d1) << 12 |
477 | | ((UV)d2) << 8 |
589 | | ((UV)d2) << 8 |
… | |
… | |
480 | |
592 | |
481 | fail: |
593 | fail: |
482 | return (UV)-1; |
594 | return (UV)-1; |
483 | } |
595 | } |
484 | |
596 | |
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 * |
597 | static SV * |
501 | decode_str (dec_t *dec) |
598 | decode_str (dec_t *dec) |
502 | { |
599 | { |
503 | SV *sv = NEWSV (0,2); |
600 | SV *sv = 0; |
504 | int utf8 = 0; |
601 | int utf8 = 0; |
505 | char *cur = SvPVX (sv); |
|
|
506 | char *end = SvEND (sv); |
|
|
507 | |
602 | |
508 | for (;;) |
603 | do |
509 | { |
604 | { |
510 | unsigned char ch = *(unsigned char *)dec->cur; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
606 | char *cur = buf; |
511 | |
607 | |
512 | if (ch == '"') |
608 | do |
513 | break; |
|
|
514 | else if (ch == '\\') |
|
|
515 | { |
609 | { |
516 | switch (*++dec->cur) |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
611 | |
|
|
612 | if (ch == '"') |
517 | { |
613 | { |
518 | case '\\': |
614 | --dec->cur; |
519 | case '/': |
615 | break; |
520 | case '"': APPEND_CH (*dec->cur++); break; |
616 | } |
521 | |
617 | else if (ch == '\\') |
522 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
618 | { |
523 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
619 | 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 | { |
620 | { |
530 | UV lo, hi; |
621 | case '\\': |
531 | ++dec->cur; |
622 | case '/': |
|
|
623 | case '"': *cur++ = *dec->cur++; break; |
532 | |
624 | |
533 | hi = decode_4hex (dec); |
625 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
534 | if (hi == (UV)-1) |
626 | case 't': ++dec->cur; *cur++ = '\011'; break; |
535 | 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; |
536 | |
630 | |
537 | // possibly a surrogate pair |
631 | case 'u': |
538 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
539 | { |
632 | { |
540 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
633 | UV lo, hi; |
541 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
542 | |
|
|
543 | dec->cur += 2; |
634 | ++dec->cur; |
544 | |
635 | |
545 | lo = decode_4hex (dec); |
636 | hi = decode_4hex (dec); |
546 | if (lo == (UV)-1) |
637 | if (hi == (UV)-1) |
547 | goto fail; |
638 | goto fail; |
548 | |
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 | |
549 | if (lo < 0xdc00 || lo >= 0xe000) |
653 | if (lo < 0xdc00 || lo >= 0xe000) |
550 | ERR ("surrogate pair expected"); |
654 | ERR ("surrogate pair expected"); |
551 | |
655 | |
552 | 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; |
553 | } |
669 | } |
554 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
555 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
556 | |
|
|
557 | if (hi >= 0x80) |
|
|
558 | { |
670 | break; |
559 | utf8 = 1; |
|
|
560 | |
671 | |
561 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
562 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
563 | } |
|
|
564 | else |
672 | default: |
565 | APPEND_CH (hi); |
673 | --dec->cur; |
|
|
674 | ERR ("illegal backslash escape sequence in string"); |
566 | } |
675 | } |
567 | break; |
676 | } |
|
|
677 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
678 | *cur++ = ch; |
|
|
679 | else if (ch >= 0x80) |
|
|
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
568 | |
683 | |
569 | default: |
|
|
570 | --dec->cur; |
684 | --dec->cur; |
571 | 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"); |
572 | } |
704 | } |
573 | } |
705 | } |
574 | else if (ch >= 0x20 && ch <= 0x7f) |
706 | while (cur < buf + SHORT_STRING_LEN); |
575 | APPEND_CH (*dec->cur++); |
707 | |
576 | else if (ch >= 0x80) |
|
|
577 | { |
708 | { |
578 | STRLEN clen; |
709 | 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 | |
710 | |
583 | APPEND_GROW (clen); |
711 | if (sv) |
584 | do |
|
|
585 | { |
712 | { |
586 | *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); |
587 | } |
716 | } |
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 |
717 | else |
595 | ERR ("invalid character encountered"); |
718 | sv = newSVpvn (buf, len); |
596 | } |
719 | } |
|
|
720 | } |
|
|
721 | while (*dec->cur != '"'); |
597 | |
722 | |
598 | ++dec->cur; |
723 | ++dec->cur; |
599 | |
724 | |
600 | SvCUR_set (sv, cur - SvPVX (sv)); |
725 | if (sv) |
601 | |
726 | { |
602 | SvPOK_only (sv); |
727 | SvPOK_only (sv); |
603 | *SvEND (sv) = 0; |
728 | *SvEND (sv) = 0; |
604 | |
729 | |
605 | if (utf8) |
730 | if (utf8) |
606 | SvUTF8_on (sv); |
731 | SvUTF8_on (sv); |
607 | |
732 | } |
608 | #ifdef SvPV_shrink_to_cur |
733 | else |
609 | if (dec->flags & F_SHRINK) |
734 | sv = newSVpvn ("", 0); |
610 | SvPV_shrink_to_cur (sv); |
|
|
611 | #endif |
|
|
612 | |
735 | |
613 | return sv; |
736 | return sv; |
614 | |
737 | |
615 | fail: |
738 | fail: |
616 | SvREFCNT_dec (sv); |
|
|
617 | return 0; |
739 | return 0; |
618 | } |
740 | } |
619 | |
741 | |
620 | static SV * |
742 | static SV * |
621 | decode_num (dec_t *dec) |
743 | decode_num (dec_t *dec) |
… | |
… | |
702 | static SV * |
824 | static SV * |
703 | decode_av (dec_t *dec) |
825 | decode_av (dec_t *dec) |
704 | { |
826 | { |
705 | AV *av = newAV (); |
827 | AV *av = newAV (); |
706 | |
828 | |
707 | WS; |
829 | DEC_INC_DEPTH; |
|
|
830 | decode_ws (dec); |
|
|
831 | |
708 | if (*dec->cur == ']') |
832 | if (*dec->cur == ']') |
709 | ++dec->cur; |
833 | ++dec->cur; |
710 | else |
834 | else |
711 | for (;;) |
835 | for (;;) |
712 | { |
836 | { |
… | |
… | |
716 | if (!value) |
840 | if (!value) |
717 | goto fail; |
841 | goto fail; |
718 | |
842 | |
719 | av_push (av, value); |
843 | av_push (av, value); |
720 | |
844 | |
721 | WS; |
845 | decode_ws (dec); |
722 | |
846 | |
723 | if (*dec->cur == ']') |
847 | if (*dec->cur == ']') |
724 | { |
848 | { |
725 | ++dec->cur; |
849 | ++dec->cur; |
726 | break; |
850 | break; |
… | |
… | |
730 | ERR (", or ] expected while parsing array"); |
854 | ERR (", or ] expected while parsing array"); |
731 | |
855 | |
732 | ++dec->cur; |
856 | ++dec->cur; |
733 | } |
857 | } |
734 | |
858 | |
|
|
859 | DEC_DEC_DEPTH; |
735 | return newRV_noinc ((SV *)av); |
860 | return newRV_noinc ((SV *)av); |
736 | |
861 | |
737 | fail: |
862 | fail: |
738 | SvREFCNT_dec (av); |
863 | SvREFCNT_dec (av); |
|
|
864 | DEC_DEC_DEPTH; |
739 | return 0; |
865 | return 0; |
740 | } |
866 | } |
741 | |
867 | |
742 | static SV * |
868 | static SV * |
743 | decode_hv (dec_t *dec) |
869 | decode_hv (dec_t *dec) |
744 | { |
870 | { |
745 | HV *hv = newHV (); |
871 | HV *hv = newHV (); |
746 | |
872 | |
747 | WS; |
873 | DEC_INC_DEPTH; |
|
|
874 | decode_ws (dec); |
|
|
875 | |
748 | if (*dec->cur == '}') |
876 | if (*dec->cur == '}') |
749 | ++dec->cur; |
877 | ++dec->cur; |
750 | else |
878 | else |
751 | for (;;) |
879 | for (;;) |
752 | { |
880 | { |
753 | SV *key, *value; |
881 | SV *key, *value; |
754 | |
882 | |
755 | WS; EXPECT_CH ('"'); |
883 | decode_ws (dec); EXPECT_CH ('"'); |
756 | |
884 | |
757 | key = decode_str (dec); |
885 | key = decode_str (dec); |
758 | if (!key) |
886 | if (!key) |
759 | goto fail; |
887 | goto fail; |
760 | |
888 | |
761 | WS; EXPECT_CH (':'); |
889 | decode_ws (dec); EXPECT_CH (':'); |
762 | |
890 | |
763 | value = decode_sv (dec); |
891 | value = decode_sv (dec); |
764 | if (!value) |
892 | if (!value) |
765 | { |
893 | { |
766 | SvREFCNT_dec (key); |
894 | SvREFCNT_dec (key); |
767 | goto fail; |
895 | goto fail; |
768 | } |
896 | } |
769 | |
897 | |
770 | //TODO: optimise |
|
|
771 | hv_store_ent (hv, key, value, 0); |
898 | hv_store_ent (hv, key, value, 0); |
|
|
899 | SvREFCNT_dec (key); |
772 | |
900 | |
773 | WS; |
901 | decode_ws (dec); |
774 | |
902 | |
775 | if (*dec->cur == '}') |
903 | if (*dec->cur == '}') |
776 | { |
904 | { |
777 | ++dec->cur; |
905 | ++dec->cur; |
778 | break; |
906 | break; |
… | |
… | |
782 | ERR (", or } expected while parsing object/hash"); |
910 | ERR (", or } expected while parsing object/hash"); |
783 | |
911 | |
784 | ++dec->cur; |
912 | ++dec->cur; |
785 | } |
913 | } |
786 | |
914 | |
|
|
915 | DEC_DEC_DEPTH; |
787 | return newRV_noinc ((SV *)hv); |
916 | return newRV_noinc ((SV *)hv); |
788 | |
917 | |
789 | fail: |
918 | fail: |
790 | SvREFCNT_dec (hv); |
919 | SvREFCNT_dec (hv); |
|
|
920 | DEC_DEC_DEPTH; |
791 | return 0; |
921 | return 0; |
792 | } |
922 | } |
793 | |
923 | |
794 | static SV * |
924 | static SV * |
795 | decode_sv (dec_t *dec) |
925 | decode_sv (dec_t *dec) |
796 | { |
926 | { |
797 | WS; |
927 | decode_ws (dec); |
798 | switch (*dec->cur) |
928 | switch (*dec->cur) |
799 | { |
929 | { |
800 | case '"': ++dec->cur; return decode_str (dec); |
930 | case '"': ++dec->cur; return decode_str (dec); |
801 | case '[': ++dec->cur; return decode_av (dec); |
931 | case '[': ++dec->cur; return decode_av (dec); |
802 | case '{': ++dec->cur; return decode_hv (dec); |
932 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
838 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
839 | |
969 | |
840 | break; |
970 | break; |
841 | |
971 | |
842 | default: |
972 | default: |
843 | ERR ("malformed json string"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
844 | break; |
974 | break; |
845 | } |
975 | } |
846 | |
976 | |
847 | fail: |
977 | fail: |
848 | return 0; |
978 | return 0; |
849 | } |
979 | } |
850 | |
980 | |
851 | static SV * |
981 | static SV * |
852 | decode_json (SV *string, UV flags) |
982 | decode_json (SV *string, U32 flags) |
853 | { |
983 | { |
|
|
984 | dec_t dec; |
854 | SV *sv; |
985 | SV *sv; |
|
|
986 | |
|
|
987 | SvUPGRADE (string, SVt_PV); |
855 | |
988 | |
856 | if (flags & F_UTF8) |
989 | if (flags & F_UTF8) |
857 | sv_utf8_downgrade (string, 0); |
990 | sv_utf8_downgrade (string, 0); |
858 | else |
991 | else |
859 | sv_utf8_upgrade (string); |
992 | sv_utf8_upgrade (string); |
860 | |
993 | |
861 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
994 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
862 | |
995 | |
863 | dec_t dec; |
|
|
864 | dec.flags = flags; |
996 | dec.flags = flags; |
865 | dec.cur = SvPVX (string); |
997 | dec.cur = SvPVX (string); |
866 | dec.end = SvEND (string); |
998 | dec.end = SvEND (string); |
867 | dec.err = 0; |
999 | dec.err = 0; |
|
|
1000 | dec.depth = 0; |
|
|
1001 | dec.maxdepth = DEC_DEPTH (dec.flags); |
868 | |
1002 | |
|
|
1003 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
869 | sv = decode_sv (&dec); |
1004 | sv = decode_sv (&dec); |
870 | |
1005 | |
871 | if (!sv) |
1006 | if (!sv) |
872 | { |
1007 | { |
|
|
1008 | IV offset = dec.flags & F_UTF8 |
|
|
1009 | ? dec.cur - SvPVX (string) |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
1010 | : utf8_distance (dec.cur, SvPVX (string)); |
874 | SV *uni = sv_newmortal (); |
1011 | SV *uni = sv_newmortal (); |
|
|
1012 | |
875 | // horrible hack to silence warning inside pv_uni_display |
1013 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
1014 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
1015 | cop.cop_warnings = pWARN_NONE; |
|
|
1016 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
1017 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
1018 | PL_curcop = &cop; |
881 | |
|
|
882 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1019 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1020 | LEAVE; |
|
|
1021 | |
883 | croak ("%s, at character offset %d (%s)", |
1022 | croak ("%s, at character offset %d [\"%s\"]", |
884 | dec.err, |
1023 | dec.err, |
885 | (int)offset, |
1024 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1025 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
1026 | } |
888 | |
1027 | |
889 | sv = sv_2mortal (sv); |
1028 | sv = sv_2mortal (sv); |
890 | |
1029 | |
891 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1030 | 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)"); |
1031 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
893 | |
1032 | |
894 | return sv; |
1033 | return sv; |
895 | } |
1034 | } |
896 | |
1035 | |
|
|
1036 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1037 | // XS interface functions |
|
|
1038 | |
897 | MODULE = JSON::XS PACKAGE = JSON::XS |
1039 | MODULE = JSON::XS PACKAGE = JSON::XS |
898 | |
1040 | |
899 | BOOT: |
1041 | BOOT: |
900 | { |
1042 | { |
901 | int i; |
1043 | int i; |
902 | |
1044 | |
903 | memset (decode_hexdigit, 0xff, 256); |
1045 | memset (decode_hexdigit, 0xff, 256); |
|
|
1046 | |
904 | for (i = 10; i--; ) |
1047 | for (i = 0; i < 256; ++i) |
905 | decode_hexdigit ['0' + i] = i; |
1048 | decode_hexdigit [i] = |
906 | |
1049 | i >= '0' && i <= '9' ? i - '0' |
907 | for (i = 7; i--; ) |
1050 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
908 | { |
1051 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
909 | decode_hexdigit ['a' + i] = 10 + i; |
1052 | : -1; |
910 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
911 | } |
|
|
912 | |
1053 | |
913 | json_stash = gv_stashpv ("JSON::XS", 1); |
1054 | json_stash = gv_stashpv ("JSON::XS", 1); |
914 | } |
1055 | } |
915 | |
1056 | |
916 | PROTOTYPES: DISABLE |
1057 | PROTOTYPES: DISABLE |
… | |
… | |
927 | utf8 = F_UTF8 |
1068 | utf8 = F_UTF8 |
928 | indent = F_INDENT |
1069 | indent = F_INDENT |
929 | canonical = F_CANONICAL |
1070 | canonical = F_CANONICAL |
930 | space_before = F_SPACE_BEFORE |
1071 | space_before = F_SPACE_BEFORE |
931 | space_after = F_SPACE_AFTER |
1072 | space_after = F_SPACE_AFTER |
932 | json_rpc = F_JSON_RPC |
|
|
933 | pretty = F_PRETTY |
1073 | pretty = F_PRETTY |
934 | allow_nonref = F_ALLOW_NONREF |
1074 | allow_nonref = F_ALLOW_NONREF |
935 | shrink = F_SHRINK |
1075 | shrink = F_SHRINK |
936 | CODE: |
1076 | CODE: |
937 | { |
1077 | { |
… | |
… | |
944 | RETVAL = newSVsv (self); |
1084 | RETVAL = newSVsv (self); |
945 | } |
1085 | } |
946 | OUTPUT: |
1086 | OUTPUT: |
947 | RETVAL |
1087 | RETVAL |
948 | |
1088 | |
|
|
1089 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1090 | CODE: |
|
|
1091 | { |
|
|
1092 | UV *uv = SvJSON (self); |
|
|
1093 | UV log2 = 0; |
|
|
1094 | |
|
|
1095 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1096 | |
|
|
1097 | while ((1UL << log2) < max_depth) |
|
|
1098 | ++log2; |
|
|
1099 | |
|
|
1100 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1101 | |
|
|
1102 | RETVAL = newSVsv (self); |
|
|
1103 | } |
|
|
1104 | OUTPUT: |
|
|
1105 | RETVAL |
|
|
1106 | |
949 | void encode (SV *self, SV *scalar) |
1107 | void encode (SV *self, SV *scalar) |
950 | PPCODE: |
1108 | PPCODE: |
951 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1109 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
952 | |
1110 | |
953 | void decode (SV *self, SV *jsonstr) |
1111 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
955 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1113 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
956 | |
1114 | |
957 | PROTOTYPES: ENABLE |
1115 | PROTOTYPES: ENABLE |
958 | |
1116 | |
959 | void to_json (SV *scalar) |
1117 | void to_json (SV *scalar) |
|
|
1118 | ALIAS: |
|
|
1119 | objToJson = 0 |
960 | PPCODE: |
1120 | PPCODE: |
961 | XPUSHs (encode_json (scalar, F_UTF8)); |
1121 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
962 | |
1122 | |
963 | void from_json (SV *jsonstr) |
1123 | void from_json (SV *jsonstr) |
|
|
1124 | ALIAS: |
|
|
1125 | jsonToObj = 0 |
964 | PPCODE: |
1126 | PPCODE: |
965 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1127 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
966 | |
1128 | |