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