… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
9 | #define F_ASCII 0x00000001 |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
22 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
23 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
24 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
25 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
26 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
27 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
|
|
31 | #define F_MAXDEPTH 0xf8000000UL |
|
|
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
|
|
35 | |
|
|
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
17 | |
38 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
41 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
43 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
44 | |
|
|
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
22 | |
46 | |
23 | #define SB do { |
47 | #define SB do { |
24 | #define SE } while (0) |
48 | #define SE } while (0) |
25 | |
49 | |
26 | static HV *json_stash; |
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
|
|
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | ///////////////////////////////////////////////////////////////////////////// |
|
|
65 | // utility functions |
|
|
66 | |
|
|
67 | static UV * |
|
|
68 | SvJSON (SV *sv) |
|
|
69 | { |
|
|
70 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
71 | croak ("object is not of type JSON::XS"); |
|
|
72 | |
|
|
73 | return &SvUVX (SvRV (sv)); |
|
|
74 | } |
|
|
75 | |
|
|
76 | static void |
|
|
77 | shrink (SV *sv) |
|
|
78 | { |
|
|
79 | sv_utf8_downgrade (sv, 1); |
|
|
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
81 | { |
|
|
82 | #ifdef SvPV_shrink_to_cur |
|
|
83 | SvPV_shrink_to_cur (sv); |
|
|
84 | #elif defined (SvPV_renew) |
|
|
85 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
86 | #endif |
|
|
87 | } |
|
|
88 | } |
|
|
89 | |
|
|
90 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
91 | // case of an error. |
|
|
92 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
93 | // but use the very good perl function to parse anything else. |
|
|
94 | // note that we never call this function for a ascii codepoints |
|
|
95 | inline UV |
|
|
96 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
97 | { |
|
|
98 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
99 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
100 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
101 | { |
|
|
102 | *clen = 2; |
|
|
103 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
104 | } |
|
|
105 | else |
|
|
106 | { |
|
|
107 | *clen = (STRLEN)-1; |
|
|
108 | return (UV)-1; |
|
|
109 | } |
|
|
110 | } |
|
|
111 | |
|
|
112 | ///////////////////////////////////////////////////////////////////////////// |
|
|
113 | // encoder |
27 | |
114 | |
28 | // structure used for encoding JSON |
115 | // structure used for encoding JSON |
29 | typedef struct |
116 | typedef struct |
30 | { |
117 | { |
31 | char *cur; |
118 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
119 | char *end; // SvEND (sv) |
34 | SV *sv; |
120 | SV *sv; // result scalar |
35 | UV flags; |
121 | U32 flags; // F_* |
36 | int max_recurse; |
122 | U32 indent; // indentation level |
37 | int indent; |
123 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
124 | } enc_t; |
39 | |
125 | |
40 | // structure used for decoding JSON |
126 | inline void |
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 | static void |
|
|
59 | shrink (SV *sv) |
|
|
60 | { |
|
|
61 | sv_utf8_downgrade (sv, 1); |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #endif |
|
|
65 | } |
|
|
66 | |
|
|
67 | ///////////////////////////////////////////////////////////////////////////// |
|
|
68 | |
|
|
69 | static void |
|
|
70 | need (enc_t *enc, STRLEN len) |
127 | need (enc_t *enc, STRLEN len) |
71 | { |
128 | { |
72 | if (enc->cur + len >= enc->end) |
129 | if (expect_false (enc->cur + len >= enc->end)) |
73 | { |
130 | { |
74 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
131 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
75 | SvGROW (enc->sv, cur + len + 1); |
132 | SvGROW (enc->sv, cur + len + 1); |
76 | enc->cur = SvPVX (enc->sv) + cur; |
133 | enc->cur = SvPVX (enc->sv) + cur; |
77 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
134 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
78 | } |
135 | } |
79 | } |
136 | } |
80 | |
137 | |
81 | static void |
138 | inline void |
82 | encode_ch (enc_t *enc, char ch) |
139 | encode_ch (enc_t *enc, char ch) |
83 | { |
140 | { |
84 | need (enc, 1); |
141 | need (enc, 1); |
85 | *enc->cur++ = ch; |
142 | *enc->cur++ = ch; |
86 | } |
143 | } |
… | |
… | |
94 | |
151 | |
95 | while (str < end) |
152 | while (str < end) |
96 | { |
153 | { |
97 | unsigned char ch = *(unsigned char *)str; |
154 | unsigned char ch = *(unsigned char *)str; |
98 | |
155 | |
99 | if (ch >= 0x20 && ch < 0x80) // most common case |
156 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
100 | { |
157 | { |
101 | if (ch == '"') // but with slow exceptions |
158 | if (expect_false (ch == '"')) // but with slow exceptions |
102 | { |
159 | { |
103 | need (enc, len += 1); |
160 | need (enc, len += 1); |
104 | *enc->cur++ = '\\'; |
161 | *enc->cur++ = '\\'; |
105 | *enc->cur++ = '"'; |
162 | *enc->cur++ = '"'; |
106 | } |
163 | } |
107 | else if (ch == '\\') |
164 | else if (expect_false (ch == '\\')) |
108 | { |
165 | { |
109 | need (enc, len += 1); |
166 | need (enc, len += 1); |
110 | *enc->cur++ = '\\'; |
167 | *enc->cur++ = '\\'; |
111 | *enc->cur++ = '\\'; |
168 | *enc->cur++ = '\\'; |
112 | } |
169 | } |
… | |
… | |
130 | STRLEN clen; |
187 | STRLEN clen; |
131 | UV uch; |
188 | UV uch; |
132 | |
189 | |
133 | if (is_utf8) |
190 | if (is_utf8) |
134 | { |
191 | { |
135 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
192 | uch = decode_utf8 (str, end - str, &clen); |
136 | if (clen == (STRLEN)-1) |
193 | if (clen == (STRLEN)-1) |
137 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
194 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
138 | } |
195 | } |
139 | else |
196 | else |
140 | { |
197 | { |
… | |
… | |
143 | } |
200 | } |
144 | |
201 | |
145 | if (uch > 0x10FFFFUL) |
202 | if (uch > 0x10FFFFUL) |
146 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
147 | |
204 | |
148 | if (uch < 0x80 || enc->flags & F_ASCII) |
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
149 | { |
206 | { |
150 | if (uch > 0xFFFFUL) |
207 | if (uch > 0xFFFFUL) |
151 | { |
208 | { |
152 | need (enc, len += 11); |
209 | need (enc, len += 11); |
153 | sprintf (enc->cur, "\\u%04x\\u%04x", |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
167 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
168 | } |
225 | } |
169 | |
226 | |
170 | str += clen; |
227 | str += clen; |
171 | } |
228 | } |
|
|
229 | else if (enc->flags & F_LATIN1) |
|
|
230 | { |
|
|
231 | *enc->cur++ = uch; |
|
|
232 | str += clen; |
|
|
233 | } |
172 | else if (is_utf8) |
234 | else if (is_utf8) |
173 | { |
235 | { |
174 | need (enc, len += clen); |
236 | need (enc, len += clen); |
175 | do |
237 | do |
176 | { |
238 | { |
… | |
… | |
178 | } |
240 | } |
179 | while (--clen); |
241 | while (--clen); |
180 | } |
242 | } |
181 | else |
243 | else |
182 | { |
244 | { |
183 | need (enc, len += 10); // never more than 11 bytes needed |
245 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
184 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
246 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
185 | ++str; |
247 | ++str; |
186 | } |
248 | } |
187 | } |
249 | } |
188 | } |
250 | } |
… | |
… | |
190 | |
252 | |
191 | --len; |
253 | --len; |
192 | } |
254 | } |
193 | } |
255 | } |
194 | |
256 | |
195 | #define INDENT SB \ |
257 | inline void |
|
|
258 | encode_indent (enc_t *enc) |
|
|
259 | { |
196 | if (enc->flags & F_INDENT) \ |
260 | if (enc->flags & F_INDENT) |
197 | { \ |
261 | { |
198 | int i_; \ |
262 | int spaces = enc->indent * INDENT_STEP; |
199 | need (enc, enc->indent); \ |
|
|
200 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
201 | encode_ch (enc, ' '); \ |
|
|
202 | } \ |
|
|
203 | SE |
|
|
204 | |
263 | |
205 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
264 | need (enc, spaces); |
206 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
265 | memset (enc->cur, ' ', spaces); |
207 | #define COMMA SB \ |
266 | enc->cur += spaces; |
|
|
267 | } |
|
|
268 | } |
|
|
269 | |
|
|
270 | inline void |
|
|
271 | encode_space (enc_t *enc) |
|
|
272 | { |
|
|
273 | need (enc, 1); |
208 | encode_ch (enc, ','); \ |
274 | encode_ch (enc, ' '); |
|
|
275 | } |
|
|
276 | |
|
|
277 | inline void |
|
|
278 | encode_nl (enc_t *enc) |
|
|
279 | { |
209 | if (enc->flags & F_INDENT) \ |
280 | if (enc->flags & F_INDENT) |
210 | NL; \ |
281 | { |
|
|
282 | need (enc, 1); |
|
|
283 | encode_ch (enc, '\n'); |
|
|
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | inline void |
|
|
288 | encode_comma (enc_t *enc) |
|
|
289 | { |
|
|
290 | encode_ch (enc, ','); |
|
|
291 | |
|
|
292 | if (enc->flags & F_INDENT) |
|
|
293 | encode_nl (enc); |
211 | else if (enc->flags & F_SPACE_AFTER) \ |
294 | else if (enc->flags & F_SPACE_AFTER) |
212 | SPACE; \ |
295 | encode_space (enc); |
213 | SE |
296 | } |
214 | |
297 | |
215 | static void encode_sv (enc_t *enc, SV *sv); |
298 | static void encode_sv (enc_t *enc, SV *sv); |
216 | |
299 | |
217 | static void |
300 | static void |
218 | encode_av (enc_t *enc, AV *av) |
301 | encode_av (enc_t *enc, AV *av) |
219 | { |
302 | { |
220 | int i, len = av_len (av); |
303 | int i, len = av_len (av); |
221 | |
304 | |
222 | encode_ch (enc, '['); NL; |
305 | if (enc->indent >= enc->maxdepth) |
|
|
306 | croak ("data structure too deep (hit recursion limit)"); |
|
|
307 | |
|
|
308 | encode_ch (enc, '['); encode_nl (enc); |
223 | ++enc->indent; |
309 | ++enc->indent; |
224 | |
310 | |
225 | for (i = 0; i <= len; ++i) |
311 | for (i = 0; i <= len; ++i) |
226 | { |
312 | { |
227 | INDENT; |
313 | encode_indent (enc); |
228 | encode_sv (enc, *av_fetch (av, i, 0)); |
314 | encode_sv (enc, *av_fetch (av, i, 0)); |
229 | |
315 | |
230 | if (i < len) |
316 | if (i < len) |
231 | COMMA; |
317 | encode_comma (enc); |
232 | } |
318 | } |
233 | |
319 | |
234 | NL; |
320 | encode_nl (enc); |
235 | |
321 | |
236 | --enc->indent; |
322 | --enc->indent; |
237 | INDENT; encode_ch (enc, ']'); |
323 | encode_indent (enc); encode_ch (enc, ']'); |
238 | } |
324 | } |
239 | |
325 | |
240 | static void |
326 | static void |
241 | encode_he (enc_t *enc, HE *he) |
327 | encode_he (enc_t *enc, HE *he) |
242 | { |
328 | { |
… | |
… | |
256 | else |
342 | else |
257 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
258 | |
344 | |
259 | encode_ch (enc, '"'); |
345 | encode_ch (enc, '"'); |
260 | |
346 | |
261 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
262 | encode_ch (enc, ':'); |
348 | encode_ch (enc, ':'); |
263 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
264 | encode_sv (enc, HeVAL (he)); |
350 | encode_sv (enc, HeVAL (he)); |
265 | } |
351 | } |
266 | |
352 | |
267 | // compare hash entries, used when all keys are bytestrings |
353 | // compare hash entries, used when all keys are bytestrings |
268 | static int |
354 | static int |
… | |
… | |
274 | HE *b = *(HE **)b_; |
360 | HE *b = *(HE **)b_; |
275 | |
361 | |
276 | STRLEN la = HeKLEN (a); |
362 | STRLEN la = HeKLEN (a); |
277 | STRLEN lb = HeKLEN (b); |
363 | STRLEN lb = HeKLEN (b); |
278 | |
364 | |
279 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
365 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
280 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
366 | cmp = la - lb; |
281 | |
367 | |
282 | return cmp; |
368 | return cmp; |
283 | } |
369 | } |
284 | |
370 | |
285 | // compare hash entries, used when some keys are sv's or utf-x |
371 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
292 | static void |
378 | static void |
293 | encode_hv (enc_t *enc, HV *hv) |
379 | encode_hv (enc_t *enc, HV *hv) |
294 | { |
380 | { |
295 | int count, i; |
381 | int count, i; |
296 | |
382 | |
|
|
383 | if (enc->indent >= enc->maxdepth) |
|
|
384 | croak ("data structure too deep (hit recursion limit)"); |
|
|
385 | |
297 | encode_ch (enc, '{'); NL; ++enc->indent; |
386 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
298 | |
387 | |
299 | if ((count = hv_iterinit (hv))) |
388 | if ((count = hv_iterinit (hv))) |
300 | { |
389 | { |
301 | // for canonical output we have to sort by keys first |
390 | // for canonical output we have to sort by keys first |
302 | // actually, this is mostly due to the stupid so-called |
391 | // actually, this is mostly due to the stupid so-called |
303 | // security workaround added somewhere in 5.8.x. |
392 | // security workaround added somewhere in 5.8.x. |
304 | // that randomises hash orderings |
393 | // that randomises hash orderings |
305 | if (enc->flags & F_CANONICAL) |
394 | if (enc->flags & F_CANONICAL) |
306 | { |
395 | { |
307 | HE *he, *hes [count]; |
|
|
308 | int fast = 1; |
396 | int fast = 1; |
|
|
397 | HE *he; |
|
|
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
399 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
400 | #else |
|
|
401 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
402 | #endif |
309 | |
403 | |
310 | i = 0; |
404 | i = 0; |
311 | while ((he = hv_iternext (hv))) |
405 | while ((he = hv_iternext (hv))) |
312 | { |
406 | { |
313 | hes [i++] = he; |
407 | hes [i++] = he; |
… | |
… | |
337 | LEAVE; |
431 | LEAVE; |
338 | } |
432 | } |
339 | |
433 | |
340 | for (i = 0; i < count; ++i) |
434 | for (i = 0; i < count; ++i) |
341 | { |
435 | { |
342 | INDENT; |
436 | encode_indent (enc); |
343 | encode_he (enc, hes [i]); |
437 | encode_he (enc, hes [i]); |
344 | |
438 | |
345 | if (i < count - 1) |
439 | if (i < count - 1) |
346 | COMMA; |
440 | encode_comma (enc); |
347 | } |
441 | } |
348 | |
442 | |
349 | NL; |
443 | encode_nl (enc); |
350 | } |
444 | } |
351 | else |
445 | else |
352 | { |
446 | { |
353 | SV *sv; |
|
|
354 | HE *he = hv_iternext (hv); |
447 | HE *he = hv_iternext (hv); |
355 | |
448 | |
356 | for (;;) |
449 | for (;;) |
357 | { |
450 | { |
358 | INDENT; |
451 | encode_indent (enc); |
359 | encode_he (enc, he); |
452 | encode_he (enc, he); |
360 | |
453 | |
361 | if (!(he = hv_iternext (hv))) |
454 | if (!(he = hv_iternext (hv))) |
362 | break; |
455 | break; |
363 | |
456 | |
364 | COMMA; |
457 | encode_comma (enc); |
365 | } |
458 | } |
366 | |
459 | |
367 | NL; |
460 | encode_nl (enc); |
368 | } |
461 | } |
369 | } |
462 | } |
370 | |
463 | |
371 | --enc->indent; INDENT; encode_ch (enc, '}'); |
464 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
465 | } |
|
|
466 | |
|
|
467 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
468 | static void |
|
|
469 | encode_rv (enc_t *enc, SV *sv) |
|
|
470 | { |
|
|
471 | svtype svt; |
|
|
472 | |
|
|
473 | SvGETMAGIC (sv); |
|
|
474 | svt = SvTYPE (sv); |
|
|
475 | |
|
|
476 | if (expect_false (SvOBJECT (sv))) |
|
|
477 | { |
|
|
478 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
479 | { |
|
|
480 | if (SvIV (sv) == 0) |
|
|
481 | encode_str (enc, "false", 5, 0); |
|
|
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
|
|
485 | else |
|
|
486 | { |
|
|
487 | #if 0 |
|
|
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
489 | { |
|
|
490 | // not yet |
|
|
491 | } |
|
|
492 | #endif |
|
|
493 | if (enc->flags & F_CONV_BLESSED) |
|
|
494 | { |
|
|
495 | // we re-bless the reference to get overload and other niceties right |
|
|
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
497 | |
|
|
498 | if (to_json) |
|
|
499 | { |
|
|
500 | dSP; |
|
|
501 | ENTER; |
|
|
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
|
|
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
505 | |
|
|
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
507 | // check anyways. |
|
|
508 | PUTBACK; |
|
|
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
510 | SPAGAIN; |
|
|
511 | |
|
|
512 | encode_sv (enc, POPs); |
|
|
513 | |
|
|
514 | FREETMPS; |
|
|
515 | LEAVE; |
|
|
516 | } |
|
|
517 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
518 | encode_str (enc, "null", 4, 0); |
|
|
519 | else |
|
|
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
522 | } |
|
|
523 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
524 | encode_str (enc, "null", 4, 0); |
|
|
525 | else |
|
|
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
528 | } |
|
|
529 | } |
|
|
530 | else if (svt == SVt_PVHV) |
|
|
531 | encode_hv (enc, (HV *)sv); |
|
|
532 | else if (svt == SVt_PVAV) |
|
|
533 | encode_av (enc, (AV *)sv); |
|
|
534 | else if (svt < SVt_PVAV) |
|
|
535 | { |
|
|
536 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
537 | encode_str (enc, "false", 5, 0); |
|
|
538 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
539 | encode_str (enc, "true", 4, 0); |
|
|
540 | else |
|
|
541 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
543 | } |
|
|
544 | else |
|
|
545 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
372 | } |
547 | } |
373 | |
548 | |
374 | static void |
549 | static void |
375 | encode_sv (enc_t *enc, SV *sv) |
550 | encode_sv (enc_t *enc, SV *sv) |
376 | { |
551 | { |
… | |
… | |
384 | encode_str (enc, str, len, SvUTF8 (sv)); |
559 | encode_str (enc, str, len, SvUTF8 (sv)); |
385 | encode_ch (enc, '"'); |
560 | encode_ch (enc, '"'); |
386 | } |
561 | } |
387 | else if (SvNOKp (sv)) |
562 | else if (SvNOKp (sv)) |
388 | { |
563 | { |
|
|
564 | // trust that perl will do the right thing w.r.t. JSON syntax. |
389 | need (enc, NV_DIG + 32); |
565 | need (enc, NV_DIG + 32); |
390 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
566 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
391 | enc->cur += strlen (enc->cur); |
567 | enc->cur += strlen (enc->cur); |
392 | } |
568 | } |
393 | else if (SvIOKp (sv)) |
569 | else if (SvIOKp (sv)) |
394 | { |
570 | { |
395 | need (enc, 64); |
571 | // we assume we can always read an IV as a UV |
|
|
572 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
573 | { |
|
|
574 | // large integer, use the (rather slow) snprintf way. |
|
|
575 | need (enc, sizeof (UV) * 3); |
396 | enc->cur += |
576 | enc->cur += |
397 | SvIsUV(sv) |
577 | SvIsUV(sv) |
398 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
578 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
399 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
579 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
580 | } |
|
|
581 | else |
|
|
582 | { |
|
|
583 | // optimise the "small number case" |
|
|
584 | // code will likely be branchless and use only a single multiplication |
|
|
585 | I32 i = SvIV (sv); |
|
|
586 | U32 u; |
|
|
587 | char digit, nz = 0; |
|
|
588 | |
|
|
589 | need (enc, 6); |
|
|
590 | |
|
|
591 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
592 | u = i < 0 ? -i : i; |
|
|
593 | |
|
|
594 | // convert to 4.28 fixed-point representation |
|
|
595 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
596 | |
|
|
597 | // now output digit by digit, each time masking out the integer part |
|
|
598 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
599 | // resulting in a net multiplication by 10. |
|
|
600 | // we always write the digit to memory but conditionally increment |
|
|
601 | // the pointer, to ease the usage of conditional move instructions. |
|
|
602 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
603 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
604 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
605 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
606 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
607 | } |
400 | } |
608 | } |
401 | else if (SvROK (sv)) |
609 | else if (SvROK (sv)) |
402 | { |
610 | encode_rv (enc, SvRV (sv)); |
403 | SV *rv = SvRV (sv); |
|
|
404 | |
|
|
405 | if (!--enc->max_recurse) |
|
|
406 | croak ("data structure too deep (hit recursion limit)"); |
|
|
407 | |
|
|
408 | switch (SvTYPE (rv)) |
|
|
409 | { |
|
|
410 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
411 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
412 | |
|
|
413 | default: |
|
|
414 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
415 | SvPV_nolen (sv)); |
|
|
416 | } |
|
|
417 | } |
|
|
418 | else if (!SvOK (sv)) |
611 | else if (!SvOK (sv)) |
419 | encode_str (enc, "null", 4, 0); |
612 | encode_str (enc, "null", 4, 0); |
420 | else |
613 | else |
421 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
422 | SvPV_nolen (sv), SvFLAGS (sv)); |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
423 | } |
616 | } |
424 | |
617 | |
425 | static SV * |
618 | static SV * |
426 | encode_json (SV *scalar, UV flags) |
619 | encode_json (SV *scalar, U32 flags) |
427 | { |
620 | { |
|
|
621 | enc_t enc; |
|
|
622 | |
428 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
430 | |
625 | |
431 | enc_t enc; |
|
|
432 | enc.flags = flags; |
626 | enc.flags = flags; |
433 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
435 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
436 | enc.max_recurse = 0; |
|
|
437 | enc.indent = 0; |
630 | enc.indent = 0; |
|
|
631 | enc.maxdepth = DEC_DEPTH (flags); |
438 | |
632 | |
439 | SvPOK_only (enc.sv); |
633 | SvPOK_only (enc.sv); |
440 | encode_sv (&enc, scalar); |
634 | encode_sv (&enc, scalar); |
441 | |
635 | |
|
|
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
638 | |
442 | if (!(flags & (F_ASCII | F_UTF8))) |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
443 | SvUTF8_on (enc.sv); |
640 | SvUTF8_on (enc.sv); |
444 | |
|
|
445 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
446 | |
641 | |
447 | if (enc.flags & F_SHRINK) |
642 | if (enc.flags & F_SHRINK) |
448 | shrink (enc.sv); |
643 | shrink (enc.sv); |
449 | |
644 | |
450 | return enc.sv; |
645 | return enc.sv; |
451 | } |
646 | } |
452 | |
647 | |
453 | ///////////////////////////////////////////////////////////////////////////// |
648 | ///////////////////////////////////////////////////////////////////////////// |
|
|
649 | // decoder |
454 | |
650 | |
455 | #define WS \ |
651 | // structure used for decoding JSON |
|
|
652 | typedef struct |
|
|
653 | { |
|
|
654 | char *cur; // current parser pointer |
|
|
655 | char *end; // end of input string |
|
|
656 | const char *err; // parse error, if != 0 |
|
|
657 | U32 flags; // F_* |
|
|
658 | U32 depth; // recursion depth |
|
|
659 | U32 maxdepth; // recursion depth limit |
|
|
660 | } dec_t; |
|
|
661 | |
|
|
662 | inline void |
|
|
663 | decode_ws (dec_t *dec) |
|
|
664 | { |
456 | for (;;) \ |
665 | for (;;) |
457 | { \ |
666 | { |
458 | char ch = *dec->cur; \ |
667 | char ch = *dec->cur; |
|
|
668 | |
459 | if (ch > 0x20 \ |
669 | if (ch > 0x20 |
460 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
670 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
461 | break; \ |
671 | break; |
|
|
672 | |
462 | ++dec->cur; \ |
673 | ++dec->cur; |
463 | } |
674 | } |
|
|
675 | } |
464 | |
676 | |
465 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
677 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
678 | |
466 | #define EXPECT_CH(ch) SB \ |
679 | #define EXPECT_CH(ch) SB \ |
467 | if (*dec->cur != ch) \ |
680 | if (*dec->cur != ch) \ |
468 | ERR (# ch " expected"); \ |
681 | ERR (# ch " expected"); \ |
469 | ++dec->cur; \ |
682 | ++dec->cur; \ |
470 | SE |
683 | SE |
471 | |
684 | |
|
|
685 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
686 | #define DEC_DEC_DEPTH --dec->depth |
|
|
687 | |
472 | static SV *decode_sv (dec_t *dec); |
688 | static SV *decode_sv (dec_t *dec); |
473 | |
689 | |
474 | static signed char decode_hexdigit[256]; |
690 | static signed char decode_hexdigit[256]; |
475 | |
691 | |
476 | static UV |
692 | static UV |
477 | decode_4hex (dec_t *dec) |
693 | decode_4hex (dec_t *dec) |
478 | { |
694 | { |
479 | signed char d1, d2, d3, d4; |
695 | signed char d1, d2, d3, d4; |
|
|
696 | unsigned char *cur = (unsigned char *)dec->cur; |
480 | |
697 | |
481 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
698 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
482 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
699 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
483 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
700 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
484 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
701 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
485 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
486 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
|
|
487 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
488 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
|
|
489 | |
702 | |
490 | dec->cur += 4; |
703 | dec->cur += 4; |
491 | |
704 | |
492 | return ((UV)d1) << 12 |
705 | return ((UV)d1) << 12 |
493 | | ((UV)d2) << 8 |
706 | | ((UV)d2) << 8 |
… | |
… | |
496 | |
709 | |
497 | fail: |
710 | fail: |
498 | return (UV)-1; |
711 | return (UV)-1; |
499 | } |
712 | } |
500 | |
713 | |
501 | #define APPEND_GROW(n) SB \ |
|
|
502 | if (cur + (n) >= end) \ |
|
|
503 | { \ |
|
|
504 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
505 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
506 | cur = SvPVX (sv) + ofs; \ |
|
|
507 | end = SvEND (sv); \ |
|
|
508 | } \ |
|
|
509 | SE |
|
|
510 | |
|
|
511 | #define APPEND_CH(ch) SB \ |
|
|
512 | APPEND_GROW (1); \ |
|
|
513 | *cur++ = (ch); \ |
|
|
514 | SE |
|
|
515 | |
|
|
516 | static SV * |
714 | static SV * |
517 | decode_str (dec_t *dec) |
715 | decode_str (dec_t *dec) |
518 | { |
716 | { |
519 | SV *sv = NEWSV (0,2); |
717 | SV *sv = 0; |
520 | int utf8 = 0; |
718 | int utf8 = 0; |
521 | char *cur = SvPVX (sv); |
719 | char *dec_cur = dec->cur; |
522 | char *end = SvEND (sv); |
|
|
523 | |
720 | |
524 | for (;;) |
721 | do |
525 | { |
722 | { |
526 | unsigned char ch = *(unsigned char *)dec->cur; |
723 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
724 | char *cur = buf; |
527 | |
725 | |
528 | if (ch == '"') |
726 | do |
529 | break; |
|
|
530 | else if (ch == '\\') |
|
|
531 | { |
727 | { |
532 | switch (*++dec->cur) |
728 | unsigned char ch = *(unsigned char *)dec_cur++; |
|
|
729 | |
|
|
730 | if (expect_false (ch == '"')) |
533 | { |
731 | { |
534 | case '\\': |
732 | --dec_cur; |
535 | case '/': |
733 | break; |
536 | case '"': APPEND_CH (*dec->cur++); break; |
734 | } |
537 | |
735 | else if (expect_false (ch == '\\')) |
538 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
736 | { |
539 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
737 | switch (*dec_cur) |
540 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
541 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
542 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
543 | |
|
|
544 | case 'u': |
|
|
545 | { |
738 | { |
546 | UV lo, hi; |
739 | case '\\': |
547 | ++dec->cur; |
740 | case '/': |
|
|
741 | case '"': *cur++ = *dec_cur++; break; |
548 | |
742 | |
549 | hi = decode_4hex (dec); |
743 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
550 | if (hi == (UV)-1) |
744 | case 't': ++dec_cur; *cur++ = '\011'; break; |
551 | goto fail; |
745 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
|
|
746 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
|
|
747 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
552 | |
748 | |
553 | // possibly a surrogate pair |
749 | case 'u': |
554 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
555 | { |
750 | { |
556 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
751 | UV lo, hi; |
557 | ERR ("missing low surrogate character in surrogate pair"); |
752 | ++dec_cur; |
558 | |
753 | |
559 | dec->cur += 2; |
754 | dec->cur = dec_cur; |
560 | |
|
|
561 | lo = decode_4hex (dec); |
755 | hi = decode_4hex (dec); |
|
|
756 | dec_cur = dec->cur; |
562 | if (lo == (UV)-1) |
757 | if (hi == (UV)-1) |
563 | goto fail; |
758 | goto fail; |
564 | |
759 | |
|
|
760 | // possibly a surrogate pair |
|
|
761 | if (hi >= 0xd800) |
|
|
762 | if (hi < 0xdc00) |
|
|
763 | { |
|
|
764 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
|
|
765 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
766 | |
|
|
767 | dec_cur += 2; |
|
|
768 | |
|
|
769 | dec->cur = dec_cur; |
|
|
770 | lo = decode_4hex (dec); |
|
|
771 | dec_cur = dec->cur; |
|
|
772 | if (lo == (UV)-1) |
|
|
773 | goto fail; |
|
|
774 | |
565 | if (lo < 0xdc00 || lo >= 0xe000) |
775 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | ERR ("surrogate pair expected"); |
776 | ERR ("surrogate pair expected"); |
567 | |
777 | |
568 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
778 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
779 | } |
|
|
780 | else if (hi < 0xe000) |
|
|
781 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
782 | |
|
|
783 | if (hi >= 0x80) |
|
|
784 | { |
|
|
785 | utf8 = 1; |
|
|
786 | |
|
|
787 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
788 | } |
|
|
789 | else |
|
|
790 | *cur++ = hi; |
569 | } |
791 | } |
570 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
571 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
572 | |
|
|
573 | if (hi >= 0x80) |
|
|
574 | { |
792 | break; |
575 | utf8 = 1; |
|
|
576 | |
793 | |
577 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
578 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
579 | } |
|
|
580 | else |
794 | default: |
581 | APPEND_CH (hi); |
795 | --dec_cur; |
|
|
796 | ERR ("illegal backslash escape sequence in string"); |
582 | } |
797 | } |
583 | break; |
798 | } |
|
|
799 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
|
|
800 | *cur++ = ch; |
|
|
801 | else if (ch >= 0x80) |
|
|
802 | { |
|
|
803 | STRLEN clen; |
|
|
804 | UV uch; |
584 | |
805 | |
585 | default: |
|
|
586 | --dec->cur; |
806 | --dec_cur; |
587 | ERR ("illegal backslash escape sequence in string"); |
807 | |
|
|
808 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
|
|
809 | if (clen == (STRLEN)-1) |
|
|
810 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
811 | |
|
|
812 | do |
|
|
813 | *cur++ = *dec_cur++; |
|
|
814 | while (--clen); |
|
|
815 | |
|
|
816 | utf8 = 1; |
|
|
817 | } |
|
|
818 | else |
|
|
819 | { |
|
|
820 | --dec_cur; |
|
|
821 | |
|
|
822 | if (!ch) |
|
|
823 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
824 | else |
|
|
825 | ERR ("invalid character encountered while parsing JSON string"); |
588 | } |
826 | } |
589 | } |
827 | } |
590 | else if (ch >= 0x20 && ch <= 0x7f) |
828 | while (cur < buf + SHORT_STRING_LEN); |
591 | APPEND_CH (*dec->cur++); |
829 | |
592 | else if (ch >= 0x80) |
|
|
593 | { |
830 | { |
594 | STRLEN clen; |
831 | STRLEN len = cur - buf; |
595 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
596 | if (clen == (STRLEN)-1) |
|
|
597 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
598 | |
832 | |
599 | APPEND_GROW (clen); |
833 | if (sv) |
600 | do |
|
|
601 | { |
834 | { |
602 | *cur++ = *dec->cur++; |
835 | SvGROW (sv, SvCUR (sv) + len + 1); |
|
|
836 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
|
|
837 | SvCUR_set (sv, SvCUR (sv) + len); |
603 | } |
838 | } |
604 | while (--clen); |
|
|
605 | |
|
|
606 | utf8 = 1; |
|
|
607 | } |
|
|
608 | else if (dec->cur == dec->end) |
|
|
609 | ERR ("unexpected end of string while parsing json string"); |
|
|
610 | else |
839 | else |
611 | ERR ("invalid character encountered"); |
840 | sv = newSVpvn (buf, len); |
612 | } |
841 | } |
|
|
842 | } |
|
|
843 | while (*dec_cur != '"'); |
613 | |
844 | |
614 | ++dec->cur; |
845 | ++dec_cur; |
615 | |
846 | |
616 | SvCUR_set (sv, cur - SvPVX (sv)); |
847 | if (sv) |
617 | |
848 | { |
618 | SvPOK_only (sv); |
849 | SvPOK_only (sv); |
619 | *SvEND (sv) = 0; |
850 | *SvEND (sv) = 0; |
620 | |
851 | |
621 | if (utf8) |
852 | if (utf8) |
622 | SvUTF8_on (sv); |
853 | SvUTF8_on (sv); |
|
|
854 | } |
|
|
855 | else |
|
|
856 | sv = newSVpvn ("", 0); |
623 | |
857 | |
624 | if (dec->flags & F_SHRINK) |
858 | dec->cur = dec_cur; |
625 | shrink (sv); |
|
|
626 | |
|
|
627 | return sv; |
859 | return sv; |
628 | |
860 | |
629 | fail: |
861 | fail: |
630 | SvREFCNT_dec (sv); |
862 | dec->cur = dec_cur; |
631 | return 0; |
863 | return 0; |
632 | } |
864 | } |
633 | |
865 | |
634 | static SV * |
866 | static SV * |
635 | decode_num (dec_t *dec) |
867 | decode_num (dec_t *dec) |
… | |
… | |
693 | is_nv = 1; |
925 | is_nv = 1; |
694 | } |
926 | } |
695 | |
927 | |
696 | if (!is_nv) |
928 | if (!is_nv) |
697 | { |
929 | { |
698 | UV uv; |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
699 | int numtype = grok_number (start, dec->cur - start, &uv); |
931 | if (*start == '-') |
700 | if (numtype & IS_NUMBER_IN_UV) |
932 | switch (dec->cur - start) |
701 | if (numtype & IS_NUMBER_NEG) |
|
|
702 | { |
933 | { |
703 | if (uv < (UV)IV_MIN) |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
704 | return newSViv (-(IV)uv); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
705 | } |
938 | } |
|
|
939 | else |
|
|
940 | switch (dec->cur - start) |
|
|
941 | { |
|
|
942 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
946 | } |
|
|
947 | |
|
|
948 | { |
|
|
949 | UV uv; |
|
|
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
951 | if (numtype & IS_NUMBER_IN_UV) |
|
|
952 | if (numtype & IS_NUMBER_NEG) |
|
|
953 | { |
|
|
954 | if (uv < (UV)IV_MIN) |
|
|
955 | return newSViv (-(IV)uv); |
|
|
956 | } |
706 | else |
957 | else |
707 | return newSVuv (uv); |
958 | return newSVuv (uv); |
|
|
959 | |
|
|
960 | // here would likely be the place for bigint support |
708 | } |
961 | } |
|
|
962 | } |
709 | |
963 | |
|
|
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
710 | return newSVnv (Atof (start)); |
965 | return newSVnv (Atof (start)); |
711 | |
966 | |
712 | fail: |
967 | fail: |
713 | return 0; |
968 | return 0; |
714 | } |
969 | } |
… | |
… | |
716 | static SV * |
971 | static SV * |
717 | decode_av (dec_t *dec) |
972 | decode_av (dec_t *dec) |
718 | { |
973 | { |
719 | AV *av = newAV (); |
974 | AV *av = newAV (); |
720 | |
975 | |
721 | WS; |
976 | DEC_INC_DEPTH; |
|
|
977 | decode_ws (dec); |
|
|
978 | |
722 | if (*dec->cur == ']') |
979 | if (*dec->cur == ']') |
723 | ++dec->cur; |
980 | ++dec->cur; |
724 | else |
981 | else |
725 | for (;;) |
982 | for (;;) |
726 | { |
983 | { |
… | |
… | |
730 | if (!value) |
987 | if (!value) |
731 | goto fail; |
988 | goto fail; |
732 | |
989 | |
733 | av_push (av, value); |
990 | av_push (av, value); |
734 | |
991 | |
735 | WS; |
992 | decode_ws (dec); |
736 | |
993 | |
737 | if (*dec->cur == ']') |
994 | if (*dec->cur == ']') |
738 | { |
995 | { |
739 | ++dec->cur; |
996 | ++dec->cur; |
740 | break; |
997 | break; |
… | |
… | |
744 | ERR (", or ] expected while parsing array"); |
1001 | ERR (", or ] expected while parsing array"); |
745 | |
1002 | |
746 | ++dec->cur; |
1003 | ++dec->cur; |
747 | } |
1004 | } |
748 | |
1005 | |
|
|
1006 | DEC_DEC_DEPTH; |
749 | return newRV_noinc ((SV *)av); |
1007 | return newRV_noinc ((SV *)av); |
750 | |
1008 | |
751 | fail: |
1009 | fail: |
752 | SvREFCNT_dec (av); |
1010 | SvREFCNT_dec (av); |
|
|
1011 | DEC_DEC_DEPTH; |
753 | return 0; |
1012 | return 0; |
754 | } |
1013 | } |
755 | |
1014 | |
756 | static SV * |
1015 | static SV * |
757 | decode_hv (dec_t *dec) |
1016 | decode_hv (dec_t *dec) |
758 | { |
1017 | { |
759 | HV *hv = newHV (); |
1018 | HV *hv = newHV (); |
760 | |
1019 | |
761 | WS; |
1020 | DEC_INC_DEPTH; |
|
|
1021 | decode_ws (dec); |
|
|
1022 | |
762 | if (*dec->cur == '}') |
1023 | if (*dec->cur == '}') |
763 | ++dec->cur; |
1024 | ++dec->cur; |
764 | else |
1025 | else |
765 | for (;;) |
1026 | for (;;) |
766 | { |
1027 | { |
767 | SV *key, *value; |
1028 | SV *key, *value; |
768 | |
1029 | |
769 | WS; EXPECT_CH ('"'); |
1030 | decode_ws (dec); EXPECT_CH ('"'); |
770 | |
1031 | |
771 | key = decode_str (dec); |
1032 | key = decode_str (dec); |
772 | if (!key) |
1033 | if (!key) |
773 | goto fail; |
1034 | goto fail; |
774 | |
1035 | |
775 | WS; EXPECT_CH (':'); |
1036 | decode_ws (dec); EXPECT_CH (':'); |
776 | |
1037 | |
777 | value = decode_sv (dec); |
1038 | value = decode_sv (dec); |
778 | if (!value) |
1039 | if (!value) |
779 | { |
1040 | { |
780 | SvREFCNT_dec (key); |
1041 | SvREFCNT_dec (key); |
781 | goto fail; |
1042 | goto fail; |
782 | } |
1043 | } |
783 | |
1044 | |
784 | //TODO: optimise |
|
|
785 | hv_store_ent (hv, key, value, 0); |
1045 | hv_store_ent (hv, key, value, 0); |
|
|
1046 | SvREFCNT_dec (key); |
786 | |
1047 | |
787 | WS; |
1048 | decode_ws (dec); |
788 | |
1049 | |
789 | if (*dec->cur == '}') |
1050 | if (*dec->cur == '}') |
790 | { |
1051 | { |
791 | ++dec->cur; |
1052 | ++dec->cur; |
792 | break; |
1053 | break; |
… | |
… | |
796 | ERR (", or } expected while parsing object/hash"); |
1057 | ERR (", or } expected while parsing object/hash"); |
797 | |
1058 | |
798 | ++dec->cur; |
1059 | ++dec->cur; |
799 | } |
1060 | } |
800 | |
1061 | |
|
|
1062 | DEC_DEC_DEPTH; |
801 | return newRV_noinc ((SV *)hv); |
1063 | return newRV_noinc ((SV *)hv); |
802 | |
1064 | |
803 | fail: |
1065 | fail: |
804 | SvREFCNT_dec (hv); |
1066 | SvREFCNT_dec (hv); |
|
|
1067 | DEC_DEC_DEPTH; |
805 | return 0; |
1068 | return 0; |
806 | } |
1069 | } |
807 | |
1070 | |
808 | static SV * |
1071 | static SV * |
809 | decode_sv (dec_t *dec) |
1072 | decode_sv (dec_t *dec) |
810 | { |
1073 | { |
811 | WS; |
1074 | decode_ws (dec); |
|
|
1075 | |
|
|
1076 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1077 | // to parse anything. |
812 | switch (*dec->cur) |
1078 | switch (*dec->cur) |
813 | { |
1079 | { |
814 | case '"': ++dec->cur; return decode_str (dec); |
1080 | case '"': ++dec->cur; return decode_str (dec); |
815 | case '[': ++dec->cur; return decode_av (dec); |
1081 | case '[': ++dec->cur; return decode_av (dec); |
816 | case '{': ++dec->cur; return decode_hv (dec); |
1082 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
822 | |
1088 | |
823 | case 't': |
1089 | case 't': |
824 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1090 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
825 | { |
1091 | { |
826 | dec->cur += 4; |
1092 | dec->cur += 4; |
827 | return newSViv (1); |
1093 | return SvREFCNT_inc (json_true); |
828 | } |
1094 | } |
829 | else |
1095 | else |
830 | ERR ("'true' expected"); |
1096 | ERR ("'true' expected"); |
831 | |
1097 | |
832 | break; |
1098 | break; |
833 | |
1099 | |
834 | case 'f': |
1100 | case 'f': |
835 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1101 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
836 | { |
1102 | { |
837 | dec->cur += 5; |
1103 | dec->cur += 5; |
838 | return newSViv (0); |
1104 | return SvREFCNT_inc (json_false); |
839 | } |
1105 | } |
840 | else |
1106 | else |
841 | ERR ("'false' expected"); |
1107 | ERR ("'false' expected"); |
842 | |
1108 | |
843 | break; |
1109 | break; |
… | |
… | |
852 | ERR ("'null' expected"); |
1118 | ERR ("'null' expected"); |
853 | |
1119 | |
854 | break; |
1120 | break; |
855 | |
1121 | |
856 | default: |
1122 | default: |
857 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1123 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
858 | break; |
1124 | break; |
859 | } |
1125 | } |
860 | |
1126 | |
861 | fail: |
1127 | fail: |
862 | return 0; |
1128 | return 0; |
863 | } |
1129 | } |
864 | |
1130 | |
865 | static SV * |
1131 | static SV * |
866 | decode_json (SV *string, UV flags) |
1132 | decode_json (SV *string, U32 flags, UV *offset_return) |
867 | { |
1133 | { |
|
|
1134 | dec_t dec; |
|
|
1135 | UV offset; |
868 | SV *sv; |
1136 | SV *sv; |
|
|
1137 | |
|
|
1138 | SvGETMAGIC (string); |
|
|
1139 | SvUPGRADE (string, SVt_PV); |
|
|
1140 | |
|
|
1141 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
|
|
1142 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1143 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
869 | |
1144 | |
870 | if (flags & F_UTF8) |
1145 | if (flags & F_UTF8) |
871 | sv_utf8_downgrade (string, 0); |
1146 | sv_utf8_downgrade (string, 0); |
872 | else |
1147 | else |
873 | sv_utf8_upgrade (string); |
1148 | sv_utf8_upgrade (string); |
874 | |
1149 | |
875 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1150 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
876 | |
1151 | |
877 | dec_t dec; |
|
|
878 | dec.flags = flags; |
1152 | dec.flags = flags; |
879 | dec.cur = SvPVX (string); |
1153 | dec.cur = SvPVX (string); |
880 | dec.end = SvEND (string); |
1154 | dec.end = SvEND (string); |
881 | dec.err = 0; |
1155 | dec.err = 0; |
|
|
1156 | dec.depth = 0; |
|
|
1157 | dec.maxdepth = DEC_DEPTH (dec.flags); |
882 | |
1158 | |
|
|
1159 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
883 | sv = decode_sv (&dec); |
1160 | sv = decode_sv (&dec); |
884 | |
1161 | |
|
|
1162 | if (!(offset_return || !sv)) |
|
|
1163 | { |
|
|
1164 | // check for trailing garbage |
|
|
1165 | decode_ws (&dec); |
|
|
1166 | |
|
|
1167 | if (*dec.cur) |
|
|
1168 | { |
|
|
1169 | dec.err = "garbage after JSON object"; |
|
|
1170 | SvREFCNT_dec (sv); |
|
|
1171 | sv = 0; |
|
|
1172 | } |
|
|
1173 | } |
|
|
1174 | |
|
|
1175 | if (offset_return || !sv) |
|
|
1176 | { |
|
|
1177 | offset = dec.flags & F_UTF8 |
|
|
1178 | ? dec.cur - SvPVX (string) |
|
|
1179 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1180 | |
|
|
1181 | if (offset_return) |
|
|
1182 | *offset_return = offset; |
|
|
1183 | } |
|
|
1184 | |
885 | if (!sv) |
1185 | if (!sv) |
886 | { |
1186 | { |
887 | IV offset = dec.flags & F_UTF8 |
|
|
888 | ? dec.cur - SvPVX (string) |
|
|
889 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
890 | SV *uni = sv_newmortal (); |
1187 | SV *uni = sv_newmortal (); |
891 | |
1188 | |
892 | // horrible hack to silence warning inside pv_uni_display |
1189 | // horrible hack to silence warning inside pv_uni_display |
893 | COP cop = *PL_curcop; |
1190 | COP cop = *PL_curcop; |
894 | cop.cop_warnings = pWARN_NONE; |
1191 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
896 | SAVEVPTR (PL_curcop); |
1193 | SAVEVPTR (PL_curcop); |
897 | PL_curcop = &cop; |
1194 | PL_curcop = &cop; |
898 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1195 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
899 | LEAVE; |
1196 | LEAVE; |
900 | |
1197 | |
901 | croak ("%s, at character offset %d (%s)", |
1198 | croak ("%s, at character offset %d [\"%s\"]", |
902 | dec.err, |
1199 | dec.err, |
903 | (int)offset, |
1200 | (int)offset, |
904 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1201 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | } |
1202 | } |
906 | |
1203 | |
… | |
… | |
910 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1207 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
911 | |
1208 | |
912 | return sv; |
1209 | return sv; |
913 | } |
1210 | } |
914 | |
1211 | |
|
|
1212 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1213 | // XS interface functions |
|
|
1214 | |
915 | MODULE = JSON::XS PACKAGE = JSON::XS |
1215 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | |
1216 | |
917 | BOOT: |
1217 | BOOT: |
918 | { |
1218 | { |
919 | int i; |
1219 | int i; |
920 | |
1220 | |
921 | memset (decode_hexdigit, 0xff, 256); |
|
|
922 | for (i = 10; i--; ) |
1221 | for (i = 0; i < 256; ++i) |
923 | decode_hexdigit ['0' + i] = i; |
1222 | decode_hexdigit [i] = |
|
|
1223 | i >= '0' && i <= '9' ? i - '0' |
|
|
1224 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
|
|
1225 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
|
|
1226 | : -1; |
924 | |
1227 | |
925 | for (i = 7; i--; ) |
|
|
926 | { |
|
|
927 | decode_hexdigit ['a' + i] = 10 + i; |
|
|
928 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
929 | } |
|
|
930 | |
|
|
931 | json_stash = gv_stashpv ("JSON::XS", 1); |
1228 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1229 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1230 | |
|
|
1231 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1232 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
932 | } |
1233 | } |
933 | |
1234 | |
934 | PROTOTYPES: DISABLE |
1235 | PROTOTYPES: DISABLE |
935 | |
1236 | |
936 | SV *new (char *dummy) |
1237 | SV *new (char *dummy) |
… | |
… | |
939 | OUTPUT: |
1240 | OUTPUT: |
940 | RETVAL |
1241 | RETVAL |
941 | |
1242 | |
942 | SV *ascii (SV *self, int enable = 1) |
1243 | SV *ascii (SV *self, int enable = 1) |
943 | ALIAS: |
1244 | ALIAS: |
944 | ascii = F_ASCII |
1245 | ascii = F_ASCII |
|
|
1246 | latin1 = F_LATIN1 |
945 | utf8 = F_UTF8 |
1247 | utf8 = F_UTF8 |
946 | indent = F_INDENT |
1248 | indent = F_INDENT |
947 | canonical = F_CANONICAL |
1249 | canonical = F_CANONICAL |
948 | space_before = F_SPACE_BEFORE |
1250 | space_before = F_SPACE_BEFORE |
949 | space_after = F_SPACE_AFTER |
1251 | space_after = F_SPACE_AFTER |
950 | pretty = F_PRETTY |
1252 | pretty = F_PRETTY |
951 | allow_nonref = F_ALLOW_NONREF |
1253 | allow_nonref = F_ALLOW_NONREF |
952 | shrink = F_SHRINK |
1254 | shrink = F_SHRINK |
|
|
1255 | allow_blessed = F_ALLOW_BLESSED |
|
|
1256 | convert_blessed = F_CONV_BLESSED |
953 | CODE: |
1257 | CODE: |
954 | { |
1258 | { |
955 | UV *uv = SvJSON (self); |
1259 | UV *uv = SvJSON (self); |
956 | if (enable) |
1260 | if (enable) |
957 | *uv |= ix; |
1261 | *uv |= ix; |
… | |
… | |
961 | RETVAL = newSVsv (self); |
1265 | RETVAL = newSVsv (self); |
962 | } |
1266 | } |
963 | OUTPUT: |
1267 | OUTPUT: |
964 | RETVAL |
1268 | RETVAL |
965 | |
1269 | |
|
|
1270 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1271 | CODE: |
|
|
1272 | { |
|
|
1273 | UV *uv = SvJSON (self); |
|
|
1274 | UV log2 = 0; |
|
|
1275 | |
|
|
1276 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1277 | |
|
|
1278 | while ((1UL << log2) < max_depth) |
|
|
1279 | ++log2; |
|
|
1280 | |
|
|
1281 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1282 | |
|
|
1283 | RETVAL = newSVsv (self); |
|
|
1284 | } |
|
|
1285 | OUTPUT: |
|
|
1286 | RETVAL |
|
|
1287 | |
|
|
1288 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1289 | CODE: |
|
|
1290 | { |
|
|
1291 | UV *uv = SvJSON (self); |
|
|
1292 | UV log2 = 0; |
|
|
1293 | |
|
|
1294 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1295 | if (max_size == 1) max_size = 2; |
|
|
1296 | |
|
|
1297 | while ((1UL << log2) < max_size) |
|
|
1298 | ++log2; |
|
|
1299 | |
|
|
1300 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1301 | |
|
|
1302 | RETVAL = newSVsv (self); |
|
|
1303 | } |
|
|
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
966 | void encode (SV *self, SV *scalar) |
1307 | void encode (SV *self, SV *scalar) |
967 | PPCODE: |
1308 | PPCODE: |
968 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1309 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
969 | |
1310 | |
970 | void decode (SV *self, SV *jsonstr) |
1311 | void decode (SV *self, SV *jsonstr) |
971 | PPCODE: |
1312 | PPCODE: |
972 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1313 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1314 | |
|
|
1315 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1316 | PPCODE: |
|
|
1317 | { |
|
|
1318 | UV offset; |
|
|
1319 | EXTEND (SP, 2); |
|
|
1320 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1321 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1322 | } |
973 | |
1323 | |
974 | PROTOTYPES: ENABLE |
1324 | PROTOTYPES: ENABLE |
975 | |
1325 | |
976 | void to_json (SV *scalar) |
1326 | void to_json (SV *scalar) |
|
|
1327 | ALIAS: |
|
|
1328 | objToJson = 0 |
977 | PPCODE: |
1329 | PPCODE: |
978 | XPUSHs (encode_json (scalar, F_UTF8)); |
1330 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
979 | |
1331 | |
980 | void from_json (SV *jsonstr) |
1332 | void from_json (SV *jsonstr) |
|
|
1333 | ALIAS: |
|
|
1334 | jsonToObj = 0 |
981 | PPCODE: |
1335 | PPCODE: |
982 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1336 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
983 | |
1337 | |