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