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