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