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