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