… | |
… | |
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? |
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 == (STRLEN)-1) |
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 | 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 | } |
215 | } |
179 | } |
216 | } |
180 | |
217 | |
181 | --len; |
218 | --len; |
182 | } |
219 | } |
183 | } |
220 | } |
184 | |
221 | |
185 | #define INDENT SB \ |
222 | static void |
|
|
223 | encode_indent (enc_t *enc) |
|
|
224 | { |
186 | if (enc->flags & F_INDENT) \ |
225 | if (enc->flags & F_INDENT) |
187 | { \ |
226 | { |
188 | int i_; \ |
227 | 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 | |
228 | |
195 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
229 | need (enc, spaces); |
196 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
230 | memset (enc->cur, ' ', spaces); |
197 | #define COMMA SB \ |
231 | enc->cur += spaces; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | encode_space (enc_t *enc) |
|
|
237 | { |
|
|
238 | need (enc, 1); |
198 | encode_ch (enc, ','); \ |
239 | encode_ch (enc, ' '); |
|
|
240 | } |
|
|
241 | |
|
|
242 | static void |
|
|
243 | encode_nl (enc_t *enc) |
|
|
244 | { |
199 | if (enc->flags & F_INDENT) \ |
245 | if (enc->flags & F_INDENT) |
200 | 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); |
201 | else if (enc->flags & F_SPACE_AFTER) \ |
259 | else if (enc->flags & F_SPACE_AFTER) |
202 | SPACE; \ |
260 | encode_space (enc); |
203 | SE |
261 | } |
204 | |
262 | |
205 | static void encode_sv (enc_t *enc, SV *sv); |
263 | static void encode_sv (enc_t *enc, SV *sv); |
206 | |
264 | |
207 | static void |
265 | static void |
208 | encode_av (enc_t *enc, AV *av) |
266 | encode_av (enc_t *enc, AV *av) |
209 | { |
267 | { |
210 | int i, len = av_len (av); |
268 | int i, len = av_len (av); |
211 | |
269 | |
212 | encode_ch (enc, '['); NL; |
270 | encode_ch (enc, '['); encode_nl (enc); |
213 | ++enc->indent; |
271 | ++enc->indent; |
214 | |
272 | |
215 | for (i = 0; i <= len; ++i) |
273 | for (i = 0; i <= len; ++i) |
216 | { |
274 | { |
217 | INDENT; |
275 | encode_indent (enc); |
218 | encode_sv (enc, *av_fetch (av, i, 0)); |
276 | encode_sv (enc, *av_fetch (av, i, 0)); |
219 | |
277 | |
220 | if (i < len) |
278 | if (i < len) |
221 | COMMA; |
279 | encode_comma (enc); |
222 | } |
280 | } |
223 | |
281 | |
224 | NL; |
282 | encode_nl (enc); |
225 | |
283 | |
226 | --enc->indent; |
284 | --enc->indent; |
227 | INDENT; encode_ch (enc, ']'); |
285 | encode_indent (enc); encode_ch (enc, ']'); |
228 | } |
286 | } |
229 | |
287 | |
230 | static void |
288 | static void |
231 | encode_he (enc_t *enc, HE *he) |
289 | encode_he (enc_t *enc, HE *he) |
232 | { |
290 | { |
… | |
… | |
246 | else |
304 | else |
247 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
305 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
248 | |
306 | |
249 | encode_ch (enc, '"'); |
307 | encode_ch (enc, '"'); |
250 | |
308 | |
251 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
309 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
252 | encode_ch (enc, ':'); |
310 | encode_ch (enc, ':'); |
253 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
311 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
254 | encode_sv (enc, HeVAL (he)); |
312 | encode_sv (enc, HeVAL (he)); |
255 | } |
313 | } |
256 | |
314 | |
257 | // compare hash entries, used when all keys are bytestrings |
315 | // compare hash entries, used when all keys are bytestrings |
258 | static int |
316 | static int |
… | |
… | |
264 | HE *b = *(HE **)b_; |
322 | HE *b = *(HE **)b_; |
265 | |
323 | |
266 | STRLEN la = HeKLEN (a); |
324 | STRLEN la = HeKLEN (a); |
267 | STRLEN lb = HeKLEN (b); |
325 | STRLEN lb = HeKLEN (b); |
268 | |
326 | |
269 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
327 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
270 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
328 | cmp = la - lb; |
271 | |
329 | |
272 | return cmp; |
330 | return cmp; |
273 | } |
331 | } |
274 | |
332 | |
275 | // 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 |
… | |
… | |
282 | static void |
340 | static void |
283 | encode_hv (enc_t *enc, HV *hv) |
341 | encode_hv (enc_t *enc, HV *hv) |
284 | { |
342 | { |
285 | int count, i; |
343 | int count, i; |
286 | |
344 | |
287 | encode_ch (enc, '{'); NL; ++enc->indent; |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
288 | |
346 | |
289 | if ((count = hv_iterinit (hv))) |
347 | if ((count = hv_iterinit (hv))) |
290 | { |
348 | { |
291 | // for canonical output we have to sort by keys first |
349 | // for canonical output we have to sort by keys first |
292 | // actually, this is mostly due to the stupid so-called |
350 | // actually, this is mostly due to the stupid so-called |
293 | // security workaround added somewhere in 5.8.x. |
351 | // security workaround added somewhere in 5.8.x. |
294 | // that randomises hash orderings |
352 | // that randomises hash orderings |
295 | if (enc->flags & F_CANONICAL) |
353 | if (enc->flags & F_CANONICAL) |
296 | { |
354 | { |
297 | HE *he, *hes [count]; |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
298 | int fast = 1; |
356 | int fast = 1; |
299 | |
357 | |
300 | i = 0; |
358 | i = 0; |
301 | while ((he = hv_iternext (hv))) |
359 | while ((he = hv_iternext (hv))) |
302 | { |
360 | { |
… | |
… | |
309 | |
367 | |
310 | if (fast) |
368 | if (fast) |
311 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
369 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
312 | else |
370 | else |
313 | { |
371 | { |
314 | // hack to disable "use bytes" |
372 | // hack to forcefully disable "use bytes" |
315 | COP *oldcop = PL_curcop, cop; |
373 | COP cop = *PL_curcop; |
316 | cop.op_private = 0; |
374 | cop.op_private = 0; |
|
|
375 | |
|
|
376 | ENTER; |
|
|
377 | SAVETMPS; |
|
|
378 | |
|
|
379 | SAVEVPTR (PL_curcop); |
317 | PL_curcop = &cop; |
380 | PL_curcop = &cop; |
318 | |
381 | |
319 | SAVETMPS; |
|
|
320 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
382 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
383 | |
321 | FREETMPS; |
384 | FREETMPS; |
322 | |
385 | LEAVE; |
323 | PL_curcop = oldcop; |
|
|
324 | } |
386 | } |
325 | |
387 | |
326 | for (i = 0; i < count; ++i) |
388 | for (i = 0; i < count; ++i) |
327 | { |
389 | { |
328 | INDENT; |
390 | encode_indent (enc); |
329 | encode_he (enc, hes [i]); |
391 | encode_he (enc, hes [i]); |
330 | |
392 | |
331 | if (i < count - 1) |
393 | if (i < count - 1) |
332 | COMMA; |
394 | encode_comma (enc); |
333 | } |
395 | } |
334 | |
396 | |
335 | NL; |
397 | encode_nl (enc); |
336 | } |
398 | } |
337 | else |
399 | else |
338 | { |
400 | { |
339 | SV *sv; |
401 | SV *sv; |
340 | HE *he = hv_iternext (hv); |
402 | HE *he = hv_iternext (hv); |
341 | |
403 | |
342 | for (;;) |
404 | for (;;) |
343 | { |
405 | { |
344 | INDENT; |
406 | encode_indent (enc); |
345 | encode_he (enc, he); |
407 | encode_he (enc, he); |
346 | |
408 | |
347 | if (!(he = hv_iternext (hv))) |
409 | if (!(he = hv_iternext (hv))) |
348 | break; |
410 | break; |
349 | |
411 | |
350 | COMMA; |
412 | encode_comma (enc); |
351 | } |
413 | } |
352 | |
414 | |
353 | NL; |
415 | encode_nl (enc); |
354 | } |
416 | } |
355 | } |
417 | } |
356 | |
418 | |
357 | --enc->indent; INDENT; encode_ch (enc, '}'); |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
358 | } |
420 | } |
359 | |
421 | |
360 | static void |
422 | static void |
361 | encode_sv (enc_t *enc, SV *sv) |
423 | encode_sv (enc_t *enc, SV *sv) |
362 | { |
424 | { |
… | |
… | |
384 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
385 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
386 | } |
448 | } |
387 | else if (SvROK (sv)) |
449 | else if (SvROK (sv)) |
388 | { |
450 | { |
389 | if (!--enc->max_recurse) |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
390 | croak ("data structure too deep (hit recursion limit)"); |
454 | croak ("data structure too deep (hit recursion limit)"); |
391 | |
455 | |
392 | sv = SvRV (sv); |
|
|
393 | |
|
|
394 | switch (SvTYPE (sv)) |
456 | switch (SvTYPE (rv)) |
395 | { |
457 | { |
396 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
397 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
398 | |
460 | |
399 | default: |
461 | default: |
400 | 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)); |
401 | } |
464 | } |
402 | } |
465 | } |
403 | else if (!SvOK (sv)) |
466 | else if (!SvOK (sv)) |
404 | encode_str (enc, "null", 4, 0); |
467 | encode_str (enc, "null", 4, 0); |
405 | else |
468 | else |
406 | 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)); |
407 | } |
471 | } |
408 | |
472 | |
409 | static SV * |
473 | static SV * |
410 | encode_json (SV *scalar, UV flags) |
474 | encode_json (SV *scalar, UV flags) |
411 | { |
475 | { |
412 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
413 | 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)"); |
414 | |
478 | |
415 | enc_t enc; |
479 | enc_t enc; |
416 | enc.flags = flags; |
480 | enc.flags = flags; |
417 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
418 | enc.cur = SvPVX (enc.sv); |
482 | enc.cur = SvPVX (enc.sv); |
419 | enc.end = SvEND (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
420 | enc.max_recurse = 0; |
|
|
421 | enc.indent = 0; |
484 | enc.indent = 0; |
|
|
485 | enc.max_depth = 0x7fffffffUL; |
422 | |
486 | |
423 | SvPOK_only (enc.sv); |
487 | SvPOK_only (enc.sv); |
424 | encode_sv (&enc, scalar); |
488 | encode_sv (&enc, scalar); |
425 | |
489 | |
426 | if (!(flags & (F_ASCII | F_UTF8))) |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
427 | SvUTF8_on (enc.sv); |
491 | SvUTF8_on (enc.sv); |
428 | |
492 | |
429 | 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 | |
430 | return enc.sv; |
498 | return enc.sv; |
431 | } |
499 | } |
432 | |
500 | |
433 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
502 | // decoder |
434 | |
503 | |
435 | #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 | { |
436 | for (;;) \ |
516 | for (;;) |
437 | { \ |
517 | { |
438 | char ch = *dec->cur; \ |
518 | char ch = *dec->cur; |
|
|
519 | |
439 | if (ch > 0x20 \ |
520 | if (ch > 0x20 |
440 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
521 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
441 | break; \ |
522 | break; |
|
|
523 | |
442 | ++dec->cur; \ |
524 | ++dec->cur; |
443 | } |
525 | } |
|
|
526 | } |
444 | |
527 | |
445 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
446 | #define EXPECT_CH(ch) SB \ |
529 | #define EXPECT_CH(ch) SB \ |
447 | if (*dec->cur != ch) \ |
530 | if (*dec->cur != ch) \ |
448 | ERR (# ch " expected"); \ |
531 | ERR (# ch " expected"); \ |
… | |
… | |
455 | |
538 | |
456 | static UV |
539 | static UV |
457 | decode_4hex (dec_t *dec) |
540 | decode_4hex (dec_t *dec) |
458 | { |
541 | { |
459 | signed char d1, d2, d3, d4; |
542 | signed char d1, d2, d3, d4; |
|
|
543 | unsigned char *cur = (unsigned char *)dec->cur; |
460 | |
544 | |
461 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
462 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
545 | 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"); |
546 | 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"); |
547 | 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"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
469 | |
549 | |
470 | dec->cur += 4; |
550 | dec->cur += 4; |
471 | |
551 | |
472 | return ((UV)d1) << 12 |
552 | return ((UV)d1) << 12 |
473 | | ((UV)d2) << 8 |
553 | | ((UV)d2) << 8 |
… | |
… | |
476 | |
556 | |
477 | fail: |
557 | fail: |
478 | return (UV)-1; |
558 | return (UV)-1; |
479 | } |
559 | } |
480 | |
560 | |
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 * |
561 | static SV * |
497 | decode_str (dec_t *dec) |
562 | decode_str (dec_t *dec) |
498 | { |
563 | { |
499 | SV *sv = NEWSV (0,2); |
564 | SV *sv = 0; |
500 | int utf8 = 0; |
565 | int utf8 = 0; |
501 | char *cur = SvPVX (sv); |
|
|
502 | char *end = SvEND (sv); |
|
|
503 | |
566 | |
504 | for (;;) |
567 | do |
505 | { |
568 | { |
506 | unsigned char ch = *(unsigned char *)dec->cur; |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
570 | char *cur = buf; |
507 | |
571 | |
508 | if (ch == '"') |
572 | do |
509 | break; |
|
|
510 | else if (ch == '\\') |
|
|
511 | { |
573 | { |
512 | switch (*++dec->cur) |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
575 | |
|
|
576 | if (ch == '"') |
513 | { |
577 | { |
514 | case '\\': |
578 | --dec->cur; |
515 | case '/': |
579 | break; |
516 | case '"': APPEND_CH (*dec->cur++); break; |
580 | } |
517 | |
581 | else if (ch == '\\') |
518 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
582 | { |
519 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
583 | 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 | { |
584 | { |
526 | UV lo, hi; |
585 | case '\\': |
527 | ++dec->cur; |
586 | case '/': |
|
|
587 | case '"': *cur++ = *dec->cur++; break; |
528 | |
588 | |
529 | hi = decode_4hex (dec); |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
530 | if (hi == (UV)-1) |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
531 | 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; |
532 | |
594 | |
533 | // possibly a surrogate pair |
595 | case 'u': |
534 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
535 | { |
596 | { |
536 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
597 | UV lo, hi; |
537 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
538 | |
|
|
539 | dec->cur += 2; |
598 | ++dec->cur; |
540 | |
599 | |
541 | lo = decode_4hex (dec); |
600 | hi = decode_4hex (dec); |
542 | if (lo == (UV)-1) |
601 | if (hi == (UV)-1) |
543 | goto fail; |
602 | goto fail; |
544 | |
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 | |
545 | if (lo < 0xdc00 || lo >= 0xe000) |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
546 | ERR ("surrogate pair expected"); |
618 | ERR ("surrogate pair expected"); |
547 | |
619 | |
548 | 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; |
549 | } |
633 | } |
550 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
551 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
552 | |
|
|
553 | if (hi >= 0x80) |
|
|
554 | { |
634 | break; |
555 | utf8 = 1; |
|
|
556 | |
635 | |
557 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
558 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
559 | } |
|
|
560 | else |
636 | default: |
561 | APPEND_CH (hi); |
637 | --dec->cur; |
|
|
638 | ERR ("illegal backslash escape sequence in string"); |
562 | } |
639 | } |
563 | break; |
|
|
564 | |
|
|
565 | default: |
|
|
566 | --dec->cur; |
|
|
567 | ERR ("illegal backslash escape sequence in string"); |
|
|
568 | } |
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 | |
569 | } |
665 | } |
570 | else if (ch >= 0x20 && ch <= 0x7f) |
666 | while (cur < buf + SHORT_STRING_LEN); |
571 | APPEND_CH (*dec->cur++); |
667 | |
572 | else if (ch >= 0x80) |
668 | STRLEN len = cur - buf; |
|
|
669 | |
|
|
670 | if (sv) |
573 | { |
671 | { |
574 | STRLEN clen; |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
575 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
576 | if (clen == (STRLEN)-1) |
674 | 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 | } |
675 | } |
588 | else if (dec->cur == dec->end) |
|
|
589 | ERR ("unexpected end of string while parsing json string"); |
|
|
590 | else |
676 | else |
591 | ERR ("invalid character encountered"); |
677 | sv = newSVpvn (buf, len); |
592 | } |
678 | } |
|
|
679 | while (*dec->cur != '"'); |
593 | |
680 | |
594 | ++dec->cur; |
681 | ++dec->cur; |
595 | |
682 | |
596 | SvCUR_set (sv, cur - SvPVX (sv)); |
683 | if (sv) |
597 | |
684 | { |
598 | SvPOK_only (sv); |
685 | SvPOK_only (sv); |
599 | *SvEND (sv) = 0; |
686 | *SvEND (sv) = 0; |
600 | |
687 | |
601 | if (utf8) |
688 | if (utf8) |
602 | SvUTF8_on (sv); |
689 | SvUTF8_on (sv); |
|
|
690 | } |
|
|
691 | else |
|
|
692 | sv = newSVpvn ("", 0); |
603 | |
693 | |
604 | return sv; |
694 | return sv; |
605 | |
695 | |
606 | fail: |
696 | fail: |
607 | SvREFCNT_dec (sv); |
|
|
608 | return 0; |
697 | return 0; |
609 | } |
698 | } |
610 | |
699 | |
611 | static SV * |
700 | static SV * |
612 | decode_num (dec_t *dec) |
701 | decode_num (dec_t *dec) |
… | |
… | |
693 | static SV * |
782 | static SV * |
694 | decode_av (dec_t *dec) |
783 | decode_av (dec_t *dec) |
695 | { |
784 | { |
696 | AV *av = newAV (); |
785 | AV *av = newAV (); |
697 | |
786 | |
698 | WS; |
787 | decode_ws (dec); |
699 | if (*dec->cur == ']') |
788 | if (*dec->cur == ']') |
700 | ++dec->cur; |
789 | ++dec->cur; |
701 | else |
790 | else |
702 | for (;;) |
791 | for (;;) |
703 | { |
792 | { |
… | |
… | |
707 | if (!value) |
796 | if (!value) |
708 | goto fail; |
797 | goto fail; |
709 | |
798 | |
710 | av_push (av, value); |
799 | av_push (av, value); |
711 | |
800 | |
712 | WS; |
801 | decode_ws (dec); |
713 | |
802 | |
714 | if (*dec->cur == ']') |
803 | if (*dec->cur == ']') |
715 | { |
804 | { |
716 | ++dec->cur; |
805 | ++dec->cur; |
717 | break; |
806 | break; |
… | |
… | |
733 | static SV * |
822 | static SV * |
734 | decode_hv (dec_t *dec) |
823 | decode_hv (dec_t *dec) |
735 | { |
824 | { |
736 | HV *hv = newHV (); |
825 | HV *hv = newHV (); |
737 | |
826 | |
738 | WS; |
827 | decode_ws (dec); |
739 | if (*dec->cur == '}') |
828 | if (*dec->cur == '}') |
740 | ++dec->cur; |
829 | ++dec->cur; |
741 | else |
830 | else |
742 | for (;;) |
831 | for (;;) |
743 | { |
832 | { |
744 | SV *key, *value; |
833 | SV *key, *value; |
745 | |
834 | |
746 | WS; EXPECT_CH ('"'); |
835 | decode_ws (dec); EXPECT_CH ('"'); |
747 | |
836 | |
748 | key = decode_str (dec); |
837 | key = decode_str (dec); |
749 | if (!key) |
838 | if (!key) |
750 | goto fail; |
839 | goto fail; |
751 | |
840 | |
752 | WS; EXPECT_CH (':'); |
841 | decode_ws (dec); EXPECT_CH (':'); |
753 | |
842 | |
754 | value = decode_sv (dec); |
843 | value = decode_sv (dec); |
755 | if (!value) |
844 | if (!value) |
756 | { |
845 | { |
757 | SvREFCNT_dec (key); |
846 | SvREFCNT_dec (key); |
… | |
… | |
759 | } |
848 | } |
760 | |
849 | |
761 | //TODO: optimise |
850 | //TODO: optimise |
762 | hv_store_ent (hv, key, value, 0); |
851 | hv_store_ent (hv, key, value, 0); |
763 | |
852 | |
764 | WS; |
853 | decode_ws (dec); |
765 | |
854 | |
766 | if (*dec->cur == '}') |
855 | if (*dec->cur == '}') |
767 | { |
856 | { |
768 | ++dec->cur; |
857 | ++dec->cur; |
769 | break; |
858 | break; |
… | |
… | |
783 | } |
872 | } |
784 | |
873 | |
785 | static SV * |
874 | static SV * |
786 | decode_sv (dec_t *dec) |
875 | decode_sv (dec_t *dec) |
787 | { |
876 | { |
788 | WS; |
877 | decode_ws (dec); |
789 | switch (*dec->cur) |
878 | switch (*dec->cur) |
790 | { |
879 | { |
791 | case '"': ++dec->cur; return decode_str (dec); |
880 | case '"': ++dec->cur; return decode_str (dec); |
792 | case '[': ++dec->cur; return decode_av (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
793 | case '{': ++dec->cur; return decode_hv (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
829 | ERR ("'null' expected"); |
918 | ERR ("'null' expected"); |
830 | |
919 | |
831 | break; |
920 | break; |
832 | |
921 | |
833 | default: |
922 | default: |
834 | ERR ("malformed json string"); |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
835 | break; |
924 | break; |
836 | } |
925 | } |
837 | |
926 | |
838 | fail: |
927 | fail: |
839 | return 0; |
928 | return 0; |
… | |
… | |
859 | |
948 | |
860 | sv = decode_sv (&dec); |
949 | sv = decode_sv (&dec); |
861 | |
950 | |
862 | if (!sv) |
951 | if (!sv) |
863 | { |
952 | { |
|
|
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
955 | : utf8_distance (dec.cur, SvPVX (string)); |
865 | SV *uni = sv_newmortal (); |
956 | SV *uni = sv_newmortal (); |
|
|
957 | |
866 | // horrible hack to silence warning inside pv_uni_display |
958 | // horrible hack to silence warning inside pv_uni_display |
867 | COP cop; |
959 | COP cop = *PL_curcop; |
868 | memset (&cop, 0, sizeof (cop)); |
|
|
869 | cop.cop_warnings = pWARN_NONE; |
960 | cop.cop_warnings = pWARN_NONE; |
|
|
961 | ENTER; |
870 | SAVEVPTR (PL_curcop); |
962 | SAVEVPTR (PL_curcop); |
871 | PL_curcop = &cop; |
963 | PL_curcop = &cop; |
872 | |
|
|
873 | 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 | |
874 | croak ("%s, at character offset %d (%s)", |
967 | croak ("%s, at character offset %d (%s)", |
875 | dec.err, |
968 | dec.err, |
876 | (int)offset, |
969 | (int)offset, |
877 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
878 | } |
971 | } |
879 | |
972 | |
880 | sv = sv_2mortal (sv); |
973 | sv = sv_2mortal (sv); |
881 | |
974 | |
882 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
975 | 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)"); |
976 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
884 | |
977 | |
885 | return sv; |
978 | return sv; |
886 | } |
979 | } |
|
|
980 | |
|
|
981 | ///////////////////////////////////////////////////////////////////////////// |
|
|
982 | // XS interface functions |
887 | |
983 | |
888 | MODULE = JSON::XS PACKAGE = JSON::XS |
984 | MODULE = JSON::XS PACKAGE = JSON::XS |
889 | |
985 | |
890 | BOOT: |
986 | BOOT: |
891 | { |
987 | { |
… | |
… | |
910 | CODE: |
1006 | CODE: |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1007 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
912 | OUTPUT: |
1008 | OUTPUT: |
913 | RETVAL |
1009 | RETVAL |
914 | |
1010 | |
915 | SV *ascii (SV *self, int enable) |
1011 | SV *ascii (SV *self, int enable = 1) |
916 | ALIAS: |
1012 | ALIAS: |
917 | ascii = F_ASCII |
1013 | ascii = F_ASCII |
918 | utf8 = F_UTF8 |
1014 | utf8 = F_UTF8 |
919 | indent = F_INDENT |
1015 | indent = F_INDENT |
920 | canonical = F_CANONICAL |
1016 | canonical = F_CANONICAL |
921 | space_before = F_SPACE_BEFORE |
1017 | space_before = F_SPACE_BEFORE |
922 | space_after = F_SPACE_AFTER |
1018 | space_after = F_SPACE_AFTER |
923 | json_rpc = F_JSON_RPC |
|
|
924 | pretty = F_PRETTY |
1019 | pretty = F_PRETTY |
925 | allow_nonref = F_ALLOW_NONREF |
1020 | allow_nonref = F_ALLOW_NONREF |
|
|
1021 | shrink = F_SHRINK |
926 | CODE: |
1022 | CODE: |
927 | { |
1023 | { |
928 | UV *uv = SvJSON (self); |
1024 | UV *uv = SvJSON (self); |
929 | if (enable) |
1025 | if (enable) |
930 | *uv |= ix; |
1026 | *uv |= ix; |
… | |
… | |
945 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
946 | |
1042 | |
947 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
948 | |
1044 | |
949 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
950 | PPCODE: |
1048 | PPCODE: |
951 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
952 | |
1050 | |
953 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
954 | PPCODE: |
1054 | PPCODE: |
955 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
956 | |
1056 | |