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