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