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