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