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