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