… | |
… | |
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_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080 |
|
|
16 | #define F_SHRINK 0x00000100 |
|
|
17 | |
|
|
18 | // F_SKIPINVALID? |
|
|
19 | // F_EXECCODEREF? |
|
|
20 | // F_SELFCONVERT? <=> { &__class__ => } |
17 | |
21 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
23 | #define F_DEFAULT 0 |
20 | |
24 | |
21 | #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 |
22 | |
30 | |
23 | #define SB do { |
31 | #define SB do { |
24 | #define SE } while (0) |
32 | #define SE } while (0) |
25 | |
33 | |
26 | 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 |
27 | |
83 | |
28 | // structure used for encoding JSON |
84 | // structure used for encoding JSON |
29 | typedef struct |
85 | typedef struct |
30 | { |
86 | { |
31 | char *cur; |
87 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
88 | char *end; // SvEND (sv) |
34 | SV *sv; |
89 | SV *sv; // result scalar |
35 | UV flags; |
90 | UV flags; // F_* |
36 | int max_recurse; |
91 | int indent; // indentation level |
37 | int indent; |
92 | int max_depth; // max. recursion level |
38 | } enc_t; |
93 | } 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 | |
94 | |
60 | static void |
95 | static void |
61 | need (enc_t *enc, STRLEN len) |
96 | need (enc_t *enc, STRLEN len) |
62 | { |
97 | { |
63 | if (enc->cur + len >= enc->end) |
98 | if (enc->cur + len >= enc->end) |
… | |
… | |
85 | |
120 | |
86 | while (str < end) |
121 | while (str < end) |
87 | { |
122 | { |
88 | unsigned char ch = *(unsigned char *)str; |
123 | unsigned char ch = *(unsigned char *)str; |
89 | |
124 | |
90 | if (ch == '"') |
125 | if (ch >= 0x20 && ch < 0x80) // most common case |
91 | { |
126 | { |
|
|
127 | if (ch == '"') // but with slow exceptions |
|
|
128 | { |
92 | need (enc, len += 1); |
129 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
130 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
131 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
132 | } |
97 | else if (ch == '\\') |
133 | else if (ch == '\\') |
98 | { |
134 | { |
99 | need (enc, len += 1); |
135 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
136 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
137 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
138 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
139 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
140 | *enc->cur++ = ch; |
107 | ++str; |
141 | |
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; |
142 | ++str; |
122 | } |
143 | } |
123 | else |
144 | else |
124 | { |
145 | { |
125 | STRLEN clen; |
146 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
147 | { |
130 | 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; |
131 | if (clen < 0) |
149 | 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"); |
150 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
151 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
152 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
153 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
154 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
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; |
|
|
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 | { |
144 | need (enc, len += 11); |
179 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
181 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
182 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
148 | 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; |
|
|
198 | } |
|
|
199 | else if (is_utf8) |
|
|
200 | { |
|
|
201 | need (enc, len += clen); |
|
|
202 | do |
|
|
203 | { |
|
|
204 | *enc->cur++ = *str++; |
|
|
205 | } |
|
|
206 | while (--clen); |
|
|
207 | } |
|
|
208 | else |
|
|
209 | { |
|
|
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
|
|
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
212 | ++str; |
|
|
213 | } |
149 | } |
214 | } |
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 | } |
215 | } |
176 | } |
216 | } |
177 | |
217 | |
178 | --len; |
218 | --len; |
179 | } |
219 | } |
180 | } |
220 | } |
181 | |
221 | |
182 | #define INDENT SB \ |
222 | static void |
|
|
223 | encode_indent (enc_t *enc) |
|
|
224 | { |
183 | if (enc->flags & F_INDENT) \ |
225 | if (enc->flags & F_INDENT) |
184 | { \ |
226 | { |
185 | int i_; \ |
227 | 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 | |
228 | |
192 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
229 | need (enc, spaces); |
193 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
230 | memset (enc->cur, ' ', spaces); |
194 | #define COMMA SB \ |
231 | enc->cur += spaces; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | encode_space (enc_t *enc) |
|
|
237 | { |
|
|
238 | need (enc, 1); |
195 | encode_ch (enc, ','); \ |
239 | encode_ch (enc, ' '); |
|
|
240 | } |
|
|
241 | |
|
|
242 | static void |
|
|
243 | encode_nl (enc_t *enc) |
|
|
244 | { |
196 | if (enc->flags & F_INDENT) \ |
245 | if (enc->flags & F_INDENT) |
197 | 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); |
198 | else if (enc->flags & F_SPACE_AFTER) \ |
259 | else if (enc->flags & F_SPACE_AFTER) |
199 | SPACE; \ |
260 | encode_space (enc); |
200 | SE |
261 | } |
201 | |
262 | |
202 | static void encode_sv (enc_t *enc, SV *sv); |
263 | static void encode_sv (enc_t *enc, SV *sv); |
203 | |
264 | |
204 | static void |
265 | static void |
205 | encode_av (enc_t *enc, AV *av) |
266 | encode_av (enc_t *enc, AV *av) |
206 | { |
267 | { |
207 | int i, len = av_len (av); |
268 | int i, len = av_len (av); |
208 | |
269 | |
209 | encode_ch (enc, '['); NL; |
270 | encode_ch (enc, '['); encode_nl (enc); |
210 | ++enc->indent; |
271 | ++enc->indent; |
211 | |
272 | |
212 | for (i = 0; i <= len; ++i) |
273 | for (i = 0; i <= len; ++i) |
213 | { |
274 | { |
214 | INDENT; |
275 | encode_indent (enc); |
215 | encode_sv (enc, *av_fetch (av, i, 0)); |
276 | encode_sv (enc, *av_fetch (av, i, 0)); |
216 | |
277 | |
217 | if (i < len) |
278 | if (i < len) |
218 | COMMA; |
279 | encode_comma (enc); |
219 | } |
280 | } |
220 | |
281 | |
221 | NL; |
282 | encode_nl (enc); |
222 | |
283 | |
223 | --enc->indent; |
284 | --enc->indent; |
224 | INDENT; encode_ch (enc, ']'); |
285 | encode_indent (enc); encode_ch (enc, ']'); |
225 | } |
286 | } |
226 | |
287 | |
227 | static void |
288 | static void |
228 | encode_he (enc_t *enc, HE *he) |
289 | encode_he (enc_t *enc, HE *he) |
229 | { |
290 | { |
… | |
… | |
243 | else |
304 | else |
244 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
305 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
245 | |
306 | |
246 | encode_ch (enc, '"'); |
307 | encode_ch (enc, '"'); |
247 | |
308 | |
248 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
309 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
249 | encode_ch (enc, ':'); |
310 | encode_ch (enc, ':'); |
250 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
311 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
251 | encode_sv (enc, HeVAL (he)); |
312 | encode_sv (enc, HeVAL (he)); |
252 | } |
313 | } |
253 | |
314 | |
254 | // compare hash entries, used when all keys are bytestrings |
315 | // compare hash entries, used when all keys are bytestrings |
255 | static int |
316 | static int |
… | |
… | |
261 | HE *b = *(HE **)b_; |
322 | HE *b = *(HE **)b_; |
262 | |
323 | |
263 | STRLEN la = HeKLEN (a); |
324 | STRLEN la = HeKLEN (a); |
264 | STRLEN lb = HeKLEN (b); |
325 | STRLEN lb = HeKLEN (b); |
265 | |
326 | |
266 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
327 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
267 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
328 | cmp = la - lb; |
268 | |
329 | |
269 | return cmp; |
330 | return cmp; |
270 | } |
331 | } |
271 | |
332 | |
272 | // 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 |
… | |
… | |
279 | static void |
340 | static void |
280 | encode_hv (enc_t *enc, HV *hv) |
341 | encode_hv (enc_t *enc, HV *hv) |
281 | { |
342 | { |
282 | int count, i; |
343 | int count, i; |
283 | |
344 | |
284 | encode_ch (enc, '{'); NL; ++enc->indent; |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
285 | |
346 | |
286 | if ((count = hv_iterinit (hv))) |
347 | if ((count = hv_iterinit (hv))) |
287 | { |
348 | { |
288 | // for canonical output we have to sort by keys first |
349 | // for canonical output we have to sort by keys first |
289 | // actually, this is mostly due to the stupid so-called |
350 | // actually, this is mostly due to the stupid so-called |
290 | // security workaround added somewhere in 5.8.x. |
351 | // security workaround added somewhere in 5.8.x. |
291 | // that randomises hash orderings |
352 | // that randomises hash orderings |
292 | if (enc->flags & F_CANONICAL) |
353 | if (enc->flags & F_CANONICAL) |
293 | { |
354 | { |
294 | HE *he, *hes [count]; |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
295 | int fast = 1; |
356 | int fast = 1; |
296 | |
357 | |
297 | i = 0; |
358 | i = 0; |
298 | while ((he = hv_iternext (hv))) |
359 | while ((he = hv_iternext (hv))) |
299 | { |
360 | { |
… | |
… | |
306 | |
367 | |
307 | if (fast) |
368 | if (fast) |
308 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
369 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
309 | else |
370 | else |
310 | { |
371 | { |
311 | // hack to disable "use bytes" |
372 | // hack to forcefully disable "use bytes" |
312 | COP *oldcop = PL_curcop, cop; |
373 | COP cop = *PL_curcop; |
313 | cop.op_private = 0; |
374 | cop.op_private = 0; |
|
|
375 | |
|
|
376 | ENTER; |
|
|
377 | SAVETMPS; |
|
|
378 | |
|
|
379 | SAVEVPTR (PL_curcop); |
314 | PL_curcop = &cop; |
380 | PL_curcop = &cop; |
315 | |
381 | |
316 | SAVETMPS; |
|
|
317 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
382 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
383 | |
318 | FREETMPS; |
384 | FREETMPS; |
319 | |
385 | LEAVE; |
320 | PL_curcop = oldcop; |
|
|
321 | } |
386 | } |
322 | |
387 | |
323 | for (i = 0; i < count; ++i) |
388 | for (i = 0; i < count; ++i) |
324 | { |
389 | { |
325 | INDENT; |
390 | encode_indent (enc); |
326 | encode_he (enc, hes [i]); |
391 | encode_he (enc, hes [i]); |
327 | |
392 | |
328 | if (i < count - 1) |
393 | if (i < count - 1) |
329 | COMMA; |
394 | encode_comma (enc); |
330 | } |
395 | } |
331 | |
396 | |
332 | NL; |
397 | encode_nl (enc); |
333 | } |
398 | } |
334 | else |
399 | else |
335 | { |
400 | { |
336 | SV *sv; |
401 | SV *sv; |
337 | HE *he = hv_iternext (hv); |
402 | HE *he = hv_iternext (hv); |
338 | |
403 | |
339 | for (;;) |
404 | for (;;) |
340 | { |
405 | { |
341 | INDENT; |
406 | encode_indent (enc); |
342 | encode_he (enc, he); |
407 | encode_he (enc, he); |
343 | |
408 | |
344 | if (!(he = hv_iternext (hv))) |
409 | if (!(he = hv_iternext (hv))) |
345 | break; |
410 | break; |
346 | |
411 | |
347 | COMMA; |
412 | encode_comma (enc); |
348 | } |
413 | } |
349 | |
414 | |
350 | NL; |
415 | encode_nl (enc); |
351 | } |
416 | } |
352 | } |
417 | } |
353 | |
418 | |
354 | --enc->indent; INDENT; encode_ch (enc, '}'); |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
355 | } |
420 | } |
356 | |
421 | |
357 | static void |
422 | static void |
358 | encode_sv (enc_t *enc, SV *sv) |
423 | encode_sv (enc_t *enc, SV *sv) |
359 | { |
424 | { |
… | |
… | |
381 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
382 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
383 | } |
448 | } |
384 | else if (SvROK (sv)) |
449 | else if (SvROK (sv)) |
385 | { |
450 | { |
386 | if (!--enc->max_recurse) |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
387 | croak ("data structure too deep (hit recursion limit)"); |
454 | croak ("data structure too deep (hit recursion limit)"); |
388 | |
455 | |
389 | sv = SvRV (sv); |
|
|
390 | |
|
|
391 | switch (SvTYPE (sv)) |
456 | switch (SvTYPE (rv)) |
392 | { |
457 | { |
393 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
394 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
395 | |
460 | |
396 | default: |
461 | default: |
397 | 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)); |
398 | } |
464 | } |
399 | } |
465 | } |
400 | else if (!SvOK (sv)) |
466 | else if (!SvOK (sv)) |
401 | encode_str (enc, "null", 4, 0); |
467 | encode_str (enc, "null", 4, 0); |
402 | else |
468 | else |
403 | 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)); |
404 | } |
471 | } |
405 | |
472 | |
406 | static SV * |
473 | static SV * |
407 | encode_json (SV *scalar, UV flags) |
474 | encode_json (SV *scalar, UV flags) |
408 | { |
475 | { |
409 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
410 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
411 | |
478 | |
412 | enc_t enc; |
479 | enc_t enc; |
413 | enc.flags = flags; |
480 | enc.flags = flags; |
414 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
415 | enc.cur = SvPVX (enc.sv); |
482 | enc.cur = SvPVX (enc.sv); |
416 | enc.end = SvEND (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
417 | enc.max_recurse = 0; |
|
|
418 | enc.indent = 0; |
484 | enc.indent = 0; |
|
|
485 | enc.max_depth = 0x7fffffffUL; |
419 | |
486 | |
420 | SvPOK_only (enc.sv); |
487 | SvPOK_only (enc.sv); |
421 | encode_sv (&enc, scalar); |
488 | encode_sv (&enc, scalar); |
422 | |
489 | |
423 | if (!(flags & (F_ASCII | F_UTF8))) |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
424 | SvUTF8_on (enc.sv); |
491 | SvUTF8_on (enc.sv); |
425 | |
492 | |
426 | 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 | |
427 | return enc.sv; |
498 | return enc.sv; |
428 | } |
499 | } |
429 | |
500 | |
430 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
502 | // decoder |
431 | |
503 | |
432 | #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 | { |
433 | for (;;) \ |
516 | for (;;) |
434 | { \ |
517 | { |
435 | char ch = *dec->cur; \ |
518 | char ch = *dec->cur; |
|
|
519 | |
436 | if (ch > 0x20 \ |
520 | if (ch > 0x20 |
437 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
521 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
438 | break; \ |
522 | break; |
|
|
523 | |
439 | ++dec->cur; \ |
524 | ++dec->cur; |
440 | } |
525 | } |
|
|
526 | } |
441 | |
527 | |
442 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
443 | #define EXPECT_CH(ch) SB \ |
529 | #define EXPECT_CH(ch) SB \ |
444 | if (*dec->cur != ch) \ |
530 | if (*dec->cur != ch) \ |
445 | ERR (# ch " expected"); \ |
531 | ERR (# ch " expected"); \ |
… | |
… | |
452 | |
538 | |
453 | static UV |
539 | static UV |
454 | decode_4hex (dec_t *dec) |
540 | decode_4hex (dec_t *dec) |
455 | { |
541 | { |
456 | signed char d1, d2, d3, d4; |
542 | signed char d1, d2, d3, d4; |
|
|
543 | unsigned char *cur = (unsigned char *)dec->cur; |
457 | |
544 | |
458 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
459 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
545 | 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"); |
546 | 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"); |
547 | 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"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
466 | |
549 | |
467 | dec->cur += 4; |
550 | dec->cur += 4; |
468 | |
551 | |
469 | return ((UV)d1) << 12 |
552 | return ((UV)d1) << 12 |
470 | | ((UV)d2) << 8 |
553 | | ((UV)d2) << 8 |
… | |
… | |
473 | |
556 | |
474 | fail: |
557 | fail: |
475 | return (UV)-1; |
558 | return (UV)-1; |
476 | } |
559 | } |
477 | |
560 | |
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 * |
561 | static SV * |
494 | decode_str (dec_t *dec) |
562 | decode_str (dec_t *dec) |
495 | { |
563 | { |
496 | SV *sv = NEWSV (0,2); |
564 | SV *sv = 0; |
497 | int utf8 = 0; |
565 | int utf8 = 0; |
498 | char *cur = SvPVX (sv); |
|
|
499 | char *end = SvEND (sv); |
|
|
500 | |
566 | |
501 | for (;;) |
567 | do |
502 | { |
568 | { |
503 | unsigned char ch = *(unsigned char *)dec->cur; |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
570 | char *cur = buf; |
504 | |
571 | |
505 | if (ch == '"') |
572 | do |
506 | break; |
|
|
507 | else if (ch == '\\') |
|
|
508 | { |
573 | { |
509 | switch (*++dec->cur) |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
575 | |
|
|
576 | if (ch == '"') |
510 | { |
577 | { |
511 | case '\\': |
578 | --dec->cur; |
512 | case '/': |
579 | break; |
513 | case '"': APPEND_CH (*dec->cur++); break; |
580 | } |
514 | |
581 | else if (ch == '\\') |
515 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
582 | { |
516 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
583 | 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 | { |
584 | { |
523 | UV lo, hi; |
585 | case '\\': |
524 | ++dec->cur; |
586 | case '/': |
|
|
587 | case '"': *cur++ = *dec->cur++; break; |
525 | |
588 | |
526 | hi = decode_4hex (dec); |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
527 | if (hi == (UV)-1) |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
528 | 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; |
529 | |
594 | |
530 | // possibly a surrogate pair |
595 | case 'u': |
531 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
532 | { |
596 | { |
533 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
597 | UV lo, hi; |
534 | ERR ("illegal surrogate character"); |
|
|
535 | |
|
|
536 | dec->cur += 2; |
598 | ++dec->cur; |
537 | |
599 | |
538 | lo = decode_4hex (dec); |
600 | hi = decode_4hex (dec); |
539 | if (lo == (UV)-1) |
601 | if (hi == (UV)-1) |
540 | goto fail; |
602 | goto fail; |
541 | |
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 | |
542 | if (lo < 0xdc00 || lo >= 0xe000) |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
543 | ERR ("surrogate pair expected"); |
618 | ERR ("surrogate pair expected"); |
544 | |
619 | |
545 | 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; |
546 | } |
633 | } |
547 | else if (lo >= 0xdc00 && lo < 0xe000) |
|
|
548 | ERR ("illegal surrogate character"); |
|
|
549 | |
|
|
550 | if (hi >= 0x80) |
|
|
551 | { |
634 | break; |
552 | utf8 = 1; |
|
|
553 | |
635 | |
554 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
555 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
556 | } |
|
|
557 | else |
636 | default: |
558 | APPEND_CH (hi); |
637 | --dec->cur; |
|
|
638 | ERR ("illegal backslash escape sequence in string"); |
559 | } |
639 | } |
560 | break; |
|
|
561 | } |
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 | |
562 | } |
665 | } |
563 | else if (ch >= 0x20 && ch <= 0x7f) |
666 | while (cur < buf + SHORT_STRING_LEN); |
564 | APPEND_CH (*dec->cur++); |
667 | |
565 | else if (ch >= 0x80) |
668 | STRLEN len = cur - buf; |
|
|
669 | |
|
|
670 | if (sv) |
566 | { |
671 | { |
567 | STRLEN clen; |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
568 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
569 | if (clen < 0) |
674 | 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 | } |
675 | } |
577 | else |
676 | else |
578 | ERR ("invalid character encountered"); |
677 | sv = newSVpvn (buf, len); |
579 | } |
678 | } |
|
|
679 | while (*dec->cur != '"'); |
580 | |
680 | |
581 | ++dec->cur; |
681 | ++dec->cur; |
582 | |
682 | |
583 | SvCUR_set (sv, cur - SvPVX (sv)); |
683 | if (sv) |
584 | |
684 | { |
585 | SvPOK_only (sv); |
685 | SvPOK_only (sv); |
586 | *SvEND (sv) = 0; |
686 | *SvEND (sv) = 0; |
587 | |
687 | |
588 | if (utf8) |
688 | if (utf8) |
589 | SvUTF8_on (sv); |
689 | SvUTF8_on (sv); |
|
|
690 | } |
|
|
691 | else |
|
|
692 | sv = newSVpvn ("", 0); |
590 | |
693 | |
591 | return sv; |
694 | return sv; |
592 | |
695 | |
593 | fail: |
696 | fail: |
594 | SvREFCNT_dec (sv); |
|
|
595 | return 0; |
697 | return 0; |
596 | } |
698 | } |
597 | |
699 | |
598 | static SV * |
700 | static SV * |
599 | decode_num (dec_t *dec) |
701 | decode_num (dec_t *dec) |
… | |
… | |
609 | { |
711 | { |
610 | ++dec->cur; |
712 | ++dec->cur; |
611 | if (*dec->cur >= '0' && *dec->cur <= '9') |
713 | if (*dec->cur >= '0' && *dec->cur <= '9') |
612 | 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)"); |
613 | } |
715 | } |
614 | |
716 | else if (*dec->cur < '0' || *dec->cur > '9') |
615 | // int |
717 | ERR ("malformed number (no digits after initial minus)"); |
|
|
718 | else |
|
|
719 | do |
|
|
720 | { |
|
|
721 | ++dec->cur; |
|
|
722 | } |
616 | while (*dec->cur >= '0' && *dec->cur <= '9') |
723 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
617 | ++dec->cur; |
|
|
618 | |
724 | |
619 | // [frac] |
725 | // [frac] |
620 | if (*dec->cur == '.') |
726 | if (*dec->cur == '.') |
621 | { |
727 | { |
622 | is_nv = 1; |
728 | ++dec->cur; |
|
|
729 | |
|
|
730 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
731 | ERR ("malformed number (no digits after decimal point)"); |
623 | |
732 | |
624 | do |
733 | do |
625 | { |
734 | { |
626 | ++dec->cur; |
735 | ++dec->cur; |
627 | } |
736 | } |
628 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
737 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
738 | |
|
|
739 | is_nv = 1; |
629 | } |
740 | } |
630 | |
741 | |
631 | // [exp] |
742 | // [exp] |
632 | if (*dec->cur == 'e' || *dec->cur == 'E') |
743 | if (*dec->cur == 'e' || *dec->cur == 'E') |
633 | { |
744 | { |
634 | is_nv = 1; |
|
|
635 | |
|
|
636 | ++dec->cur; |
745 | ++dec->cur; |
|
|
746 | |
637 | if (*dec->cur == '-' || *dec->cur == '+') |
747 | if (*dec->cur == '-' || *dec->cur == '+') |
638 | ++dec->cur; |
748 | ++dec->cur; |
639 | |
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 | } |
640 | while (*dec->cur >= '0' && *dec->cur <= '9') |
757 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
641 | ++dec->cur; |
758 | |
|
|
759 | is_nv = 1; |
642 | } |
760 | } |
643 | |
761 | |
644 | if (!is_nv) |
762 | if (!is_nv) |
645 | { |
763 | { |
646 | UV uv; |
764 | UV uv; |
… | |
… | |
664 | static SV * |
782 | static SV * |
665 | decode_av (dec_t *dec) |
783 | decode_av (dec_t *dec) |
666 | { |
784 | { |
667 | AV *av = newAV (); |
785 | AV *av = newAV (); |
668 | |
786 | |
|
|
787 | decode_ws (dec); |
|
|
788 | if (*dec->cur == ']') |
|
|
789 | ++dec->cur; |
|
|
790 | else |
669 | for (;;) |
791 | for (;;) |
670 | { |
792 | { |
671 | SV *value; |
793 | SV *value; |
672 | |
794 | |
673 | value = decode_sv (dec); |
795 | value = decode_sv (dec); |
674 | if (!value) |
796 | if (!value) |
675 | goto fail; |
797 | goto fail; |
676 | |
798 | |
677 | av_push (av, value); |
799 | av_push (av, value); |
678 | |
800 | |
679 | WS; |
801 | decode_ws (dec); |
680 | |
802 | |
681 | if (*dec->cur == ']') |
803 | if (*dec->cur == ']') |
682 | { |
804 | { |
683 | ++dec->cur; |
805 | ++dec->cur; |
684 | break; |
806 | break; |
|
|
807 | } |
685 | } |
808 | |
686 | |
|
|
687 | if (*dec->cur != ',') |
809 | if (*dec->cur != ',') |
688 | ERR (", or ] expected while parsing array"); |
810 | ERR (", or ] expected while parsing array"); |
689 | |
811 | |
690 | ++dec->cur; |
812 | ++dec->cur; |
691 | } |
813 | } |
692 | |
814 | |
693 | return newRV_noinc ((SV *)av); |
815 | return newRV_noinc ((SV *)av); |
694 | |
816 | |
695 | fail: |
817 | fail: |
696 | SvREFCNT_dec (av); |
818 | SvREFCNT_dec (av); |
… | |
… | |
700 | static SV * |
822 | static SV * |
701 | decode_hv (dec_t *dec) |
823 | decode_hv (dec_t *dec) |
702 | { |
824 | { |
703 | HV *hv = newHV (); |
825 | HV *hv = newHV (); |
704 | |
826 | |
|
|
827 | decode_ws (dec); |
|
|
828 | if (*dec->cur == '}') |
|
|
829 | ++dec->cur; |
|
|
830 | else |
705 | for (;;) |
831 | for (;;) |
706 | { |
832 | { |
707 | SV *key, *value; |
833 | SV *key, *value; |
708 | |
834 | |
709 | WS; EXPECT_CH ('"'); |
835 | decode_ws (dec); EXPECT_CH ('"'); |
710 | |
836 | |
711 | key = decode_str (dec); |
837 | key = decode_str (dec); |
712 | if (!key) |
838 | 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; |
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; |
722 | } |
848 | } |
723 | |
849 | |
724 | //TODO: optimise |
850 | //TODO: optimise |
725 | hv_store_ent (hv, key, value, 0); |
851 | hv_store_ent (hv, key, value, 0); |
726 | |
852 | |
727 | WS; |
853 | decode_ws (dec); |
728 | |
854 | |
729 | if (*dec->cur == '}') |
855 | if (*dec->cur == '}') |
730 | { |
856 | { |
731 | ++dec->cur; |
857 | ++dec->cur; |
732 | break; |
858 | break; |
733 | } |
859 | } |
734 | |
860 | |
735 | if (*dec->cur != ',') |
861 | if (*dec->cur != ',') |
736 | ERR (", or } expected while parsing object/hash"); |
862 | ERR (", or } expected while parsing object/hash"); |
737 | |
863 | |
738 | ++dec->cur; |
864 | ++dec->cur; |
739 | } |
865 | } |
740 | |
866 | |
741 | return newRV_noinc ((SV *)hv); |
867 | return newRV_noinc ((SV *)hv); |
742 | |
868 | |
743 | fail: |
869 | fail: |
744 | SvREFCNT_dec (hv); |
870 | SvREFCNT_dec (hv); |
… | |
… | |
746 | } |
872 | } |
747 | |
873 | |
748 | static SV * |
874 | static SV * |
749 | decode_sv (dec_t *dec) |
875 | decode_sv (dec_t *dec) |
750 | { |
876 | { |
751 | WS; |
877 | decode_ws (dec); |
752 | switch (*dec->cur) |
878 | switch (*dec->cur) |
753 | { |
879 | { |
754 | case '"': ++dec->cur; return decode_str (dec); |
880 | case '"': ++dec->cur; return decode_str (dec); |
755 | case '[': ++dec->cur; return decode_av (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
756 | case '{': ++dec->cur; return decode_hv (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
784 | |
910 | |
785 | case 'n': |
911 | case 'n': |
786 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
912 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
787 | { |
913 | { |
788 | dec->cur += 4; |
914 | dec->cur += 4; |
789 | return newSViv (1); |
915 | return newSVsv (&PL_sv_undef); |
790 | } |
916 | } |
791 | else |
917 | else |
792 | ERR ("'null' expected"); |
918 | ERR ("'null' expected"); |
793 | |
919 | |
794 | break; |
920 | break; |
795 | |
921 | |
796 | default: |
922 | default: |
797 | ERR ("malformed json string"); |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
798 | break; |
924 | break; |
799 | } |
925 | } |
800 | |
926 | |
801 | fail: |
927 | fail: |
802 | return 0; |
928 | return 0; |
… | |
… | |
805 | static SV * |
931 | static SV * |
806 | decode_json (SV *string, UV flags) |
932 | decode_json (SV *string, UV flags) |
807 | { |
933 | { |
808 | SV *sv; |
934 | SV *sv; |
809 | |
935 | |
810 | if (!(flags & F_UTF8)) |
936 | if (flags & F_UTF8) |
|
|
937 | sv_utf8_downgrade (string, 0); |
|
|
938 | else |
811 | sv_utf8_upgrade (string); |
939 | sv_utf8_upgrade (string); |
812 | |
940 | |
813 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
814 | |
942 | |
815 | dec_t dec; |
943 | dec_t dec; |
816 | dec.flags = flags; |
944 | dec.flags = flags; |
817 | dec.cur = SvPVX (string); |
945 | dec.cur = SvPVX (string); |
818 | dec.end = SvEND (string); |
946 | dec.end = SvEND (string); |
819 | dec.err = 0; |
947 | dec.err = 0; |
820 | |
948 | |
821 | *dec.end = 1; // invalid anywhere |
|
|
822 | sv = decode_sv (&dec); |
949 | sv = decode_sv (&dec); |
823 | *dec.end = 0; |
|
|
824 | |
950 | |
825 | if (!sv) |
951 | if (!sv) |
826 | { |
952 | { |
|
|
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
827 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
955 | : utf8_distance (dec.cur, SvPVX (string)); |
828 | SV *uni = sv_newmortal (); |
956 | SV *uni = sv_newmortal (); |
829 | |
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; |
830 | 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 | |
831 | croak ("%s, at character %d (%s)", |
967 | croak ("%s, at character offset %d (%s)", |
832 | dec.err, |
968 | dec.err, |
833 | (int)offset, |
969 | (int)offset, |
834 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
835 | } |
971 | } |
836 | |
972 | |
837 | sv = sv_2mortal (sv); |
973 | sv = sv_2mortal (sv); |
838 | |
974 | |
839 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
975 | 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)"); |
976 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
841 | |
977 | |
842 | return sv; |
978 | return sv; |
843 | } |
979 | } |
|
|
980 | |
|
|
981 | ///////////////////////////////////////////////////////////////////////////// |
|
|
982 | // XS interface functions |
844 | |
983 | |
845 | MODULE = JSON::XS PACKAGE = JSON::XS |
984 | MODULE = JSON::XS PACKAGE = JSON::XS |
846 | |
985 | |
847 | BOOT: |
986 | BOOT: |
848 | { |
987 | { |
… | |
… | |
867 | CODE: |
1006 | CODE: |
868 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1007 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
869 | OUTPUT: |
1008 | OUTPUT: |
870 | RETVAL |
1009 | RETVAL |
871 | |
1010 | |
872 | SV *ascii (SV *self, int enable) |
1011 | SV *ascii (SV *self, int enable = 1) |
873 | ALIAS: |
1012 | ALIAS: |
874 | ascii = F_ASCII |
1013 | ascii = F_ASCII |
875 | utf8 = F_UTF8 |
1014 | utf8 = F_UTF8 |
876 | indent = F_INDENT |
1015 | indent = F_INDENT |
877 | canonical = F_CANONICAL |
1016 | canonical = F_CANONICAL |
878 | space_before = F_SPACE_BEFORE |
1017 | space_before = F_SPACE_BEFORE |
879 | space_after = F_SPACE_AFTER |
1018 | space_after = F_SPACE_AFTER |
880 | json_rpc = F_JSON_RPC |
|
|
881 | pretty = F_PRETTY |
1019 | pretty = F_PRETTY |
882 | allow_nonref = F_ALLOW_NONREF |
1020 | allow_nonref = F_ALLOW_NONREF |
|
|
1021 | shrink = F_SHRINK |
883 | CODE: |
1022 | CODE: |
884 | { |
1023 | { |
885 | UV *uv = SvJSON (self); |
1024 | UV *uv = SvJSON (self); |
886 | if (enable) |
1025 | if (enable) |
887 | *uv |= ix; |
1026 | *uv |= ix; |
… | |
… | |
902 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
903 | |
1042 | |
904 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
905 | |
1044 | |
906 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
907 | PPCODE: |
1048 | PPCODE: |
908 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
909 | |
1050 | |
910 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
911 | PPCODE: |
1054 | PPCODE: |
912 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
913 | |
1056 | |