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