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