--- JSON-XS/XS.xs 2007/08/26 22:27:32 1.62 +++ JSON-XS/XS.xs 2008/03/25 23:00:31 1.79 @@ -6,6 +6,7 @@ #include #include #include +#include #include #if defined(__BORLANDC__) || defined(_MSC_VER) @@ -18,6 +19,8 @@ # define UTF8_MAXBYTES 13 #endif +#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) + #define F_ASCII 0x00000001UL #define F_LATIN1 0x00000002UL #define F_UTF8 0x00000004UL @@ -29,6 +32,8 @@ #define F_SHRINK 0x00000200UL #define F_ALLOW_BLESSED 0x00000400UL #define F_CONV_BLESSED 0x00000800UL +#define F_RELAXED 0x00001000UL + #define F_MAXDEPTH 0xf8000000UL #define S_MAXDEPTH 27 #define F_MAXSIZE 0x01f00000UL @@ -50,16 +55,20 @@ #define SE } while (0) #if __GNUC__ >= 3 -# define expect(expr,value) __builtin_expect ((expr),(value)) -# define inline inline +# define expect(expr,value) __builtin_expect ((expr), (value)) +# define INLINE static inline #else # define expect(expr,value) (expr) -# define inline static +# define INLINE static #endif #define expect_false(expr) expect ((expr) != 0, 0) #define expect_true(expr) expect ((expr) != 0, 1) +#define IN_RANGE_INC(type,val,beg,end) \ + ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ + <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) + #ifdef USE_ITHREADS # define JSON_SLOW 1 # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) @@ -71,16 +80,31 @@ static HV *json_stash, *json_boolean_stash; // JSON::XS:: static SV *json_true, *json_false; +enum { + INCR_M_WS = 0, // initial whitespace skipping, must be 0 + INCR_M_STR, // inside string + INCR_M_BS, // inside backslash + INCR_M_JSON // outside anything, count nesting +}; + +#define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) + typedef struct { U32 flags; SV *cb_object; HV *cb_sk_object; + + // for the incremental parser + SV *incr_text; // the source text so far + STRLEN incr_pos; // the current offset into the text + int incr_nest; // {[]}-nesting level + int incr_mode; } JSON; ///////////////////////////////////////////////////////////////////////////// // utility functions -inline void +INLINE void shrink (SV *sv) { sv_utf8_downgrade (sv, 1); @@ -99,21 +123,42 @@ // we special-case "safe" characters from U+80 .. U+7FF, // but use the very good perl function to parse anything else. // note that we never call this function for a ascii codepoints -inline UV +INLINE UV decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) { - if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) - return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); - else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) + if (expect_true (len >= 2 + && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) + && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) { *clen = 2; return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); } else - { - *clen = (STRLEN)-1; - return (UV)-1; - } + return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); +} + +// likewise for encoding, also never called for ascii codepoints +// this function takes advantage of this fact, although current gccs +// seem to optimise the check for >= 0x80 away anyways +INLINE unsigned char * +encode_utf8 (unsigned char *s, UV ch) +{ + if (expect_false (ch < 0x000080)) + *s++ = ch; + else if (expect_true (ch < 0x000800)) + *s++ = 0xc0 | ( ch >> 6), + *s++ = 0x80 | ( ch & 0x3f); + else if ( ch < 0x010000) + *s++ = 0xe0 | ( ch >> 12), + *s++ = 0x80 | ((ch >> 6) & 0x3f), + *s++ = 0x80 | ( ch & 0x3f); + else if ( ch < 0x110000) + *s++ = 0xf0 | ( ch >> 18), + *s++ = 0x80 | ((ch >> 12) & 0x3f), + *s++ = 0x80 | ((ch >> 6) & 0x3f), + *s++ = 0x80 | ( ch & 0x3f); + + return s; } ///////////////////////////////////////////////////////////////////////////// @@ -128,9 +173,10 @@ JSON json; U32 indent; // indentation level U32 maxdepth; // max. indentation/recursion level + UV limit; // escape character values >= this value when encoding } enc_t; -inline void +INLINE void need (enc_t *enc, STRLEN len) { if (expect_false (enc->cur + len >= enc->end)) @@ -142,7 +188,7 @@ } } -inline void +INLINE void encode_ch (enc_t *enc, char ch) { need (enc, 1); @@ -206,13 +252,13 @@ clen = 1; } - if (uch > 0x10FFFFUL) - croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); - - if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) + if (uch < 0x80/*0x20*/ || uch >= enc->limit) { - if (uch > 0xFFFFUL) + if (uch >= 0x10000UL) { + if (uch >= 0x110000UL) + croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); + need (enc, len += 11); sprintf (enc->cur, "\\u%04x\\u%04x", (int)((uch - 0x10000) / 0x400 + 0xD800), @@ -250,7 +296,7 @@ else { need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed - enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); + enc->cur = encode_utf8 (enc->cur, uch); ++str; } } @@ -261,7 +307,7 @@ } } -inline void +INLINE void encode_indent (enc_t *enc) { if (enc->json.flags & F_INDENT) @@ -274,14 +320,14 @@ } } -inline void +INLINE void encode_space (enc_t *enc) { need (enc, 1); encode_ch (enc, ' '); } -inline void +INLINE void encode_nl (enc_t *enc) { if (enc->json.flags & F_INDENT) @@ -291,7 +337,7 @@ } } -inline void +INLINE void encode_comma (enc_t *enc) { encode_ch (enc, ','); @@ -312,28 +358,31 @@ if (enc->indent >= enc->maxdepth) croak ("data structure too deep (hit recursion limit)"); - encode_ch (enc, '['); encode_nl (enc); - ++enc->indent; - - for (i = 0; i <= len; ++i) + encode_ch (enc, '['); + + if (len >= 0) { - SV **svp = av_fetch (av, i, 0); + encode_nl (enc); ++enc->indent; - encode_indent (enc); + for (i = 0; i <= len; ++i) + { + SV **svp = av_fetch (av, i, 0); - if (svp) - encode_sv (enc, *svp); - else - encode_str (enc, "null", 4, 0); + encode_indent (enc); - if (i < len) - encode_comma (enc); - } + if (svp) + encode_sv (enc, *svp); + else + encode_str (enc, "null", 4, 0); - encode_nl (enc); + if (i < len) + encode_comma (enc); + } - --enc->indent; - encode_indent (enc); encode_ch (enc, ']'); + encode_nl (enc); --enc->indent; encode_indent (enc); + } + + encode_ch (enc, ']'); } static void @@ -391,12 +440,11 @@ encode_hv (enc_t *enc, HV *hv) { HE *he; - int count; if (enc->indent >= enc->maxdepth) croak ("data structure too deep (hit recursion limit)"); - encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; + encode_ch (enc, '{'); // for canonical output we have to sort by keys first // actually, this is mostly due to the stupid so-called @@ -459,6 +507,8 @@ LEAVE; } + encode_nl (enc); ++enc->indent; + while (count--) { encode_indent (enc); @@ -469,28 +519,34 @@ if (count) encode_comma (enc); } + + encode_nl (enc); --enc->indent; encode_indent (enc); } } else { if (hv_iterinit (hv) || SvMAGICAL (hv)) if ((he = hv_iternext (hv))) - for (;;) - { - encode_indent (enc); - encode_hk (enc, he); - encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); + { + encode_nl (enc); ++enc->indent; - if (!(he = hv_iternext (hv))) - break; + for (;;) + { + encode_indent (enc); + encode_hk (enc, he); + encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); - encode_comma (enc); - } - } + if (!(he = hv_iternext (hv))) + break; + + encode_comma (enc); + } - encode_nl (enc); + encode_nl (enc); --enc->indent; encode_indent (enc); + } + } - --enc->indent; encode_indent (enc); encode_ch (enc, '}'); + encode_ch (enc, '}'); } // encode objects, arrays and special \0=false and \1=true values. @@ -608,21 +664,16 @@ } else if (SvIOKp (sv)) { - // we assume we can always read an IV as a UV - if (SvUV (sv) & ~(UV)0x7fff) - { - // large integer, use the (rather slow) snprintf way. - need (enc, sizeof (UV) * 3); - enc->cur += - SvIsUV(sv) - ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) - : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); - } - else + // we assume we can always read an IV as a UV and vice versa + // we assume two's complement + // we assume no aliasing issues in the union + if (SvIsUV (sv) ? SvUVX (sv) <= 59000 + : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) { // optimise the "small number case" // code will likely be branchless and use only a single multiplication - I32 i = SvIV (sv); + // works for numbers up to 59074 + I32 i = SvIVX (sv); U32 u; char digit, nz = 0; @@ -638,13 +689,22 @@ // and multiplying by 5 while moving the decimal point one to the right, // resulting in a net multiplication by 10. // we always write the digit to memory but conditionally increment - // the pointer, to ease the usage of conditional move instructions. - digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; - digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; - digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; - digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; + // the pointer, to enable the use of conditional move instructions. + digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; + digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; + digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; + digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' } + else + { + // large integer, use the (rather slow) snprintf way. + need (enc, IVUV_MAXCHARS); + enc->cur += + SvIsUV(sv) + ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) + : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); + } } else if (SvROK (sv)) encode_rv (enc, SvRV (sv)); @@ -669,6 +729,9 @@ enc.end = SvEND (enc.sv); enc.indent = 0; enc.maxdepth = DEC_DEPTH (enc.json.flags); + enc.limit = enc.json.flags & F_ASCII ? 0x000080UL + : enc.json.flags & F_LATIN1 ? 0x000100UL + : 0x110000UL; SvPOK_only (enc.sv); encode_sv (&enc, scalar); @@ -699,16 +762,36 @@ U32 maxdepth; // recursion depth limit } dec_t; -inline void +INLINE void +decode_comment (dec_t *dec) +{ + // only '#'-style comments allowed a.t.m. + + while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) + ++dec->cur; +} + +INLINE void decode_ws (dec_t *dec) { for (;;) { char ch = *dec->cur; - if (ch > 0x20 - || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) - break; + if (ch > 0x20) + { + if (expect_false (ch == '#')) + { + if (dec->json.flags & F_RELAXED) + decode_comment (dec); + else + break; + } + else + break; + } + else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) + break; // parse error, but let higher level handle it, gives better error messages ++dec->cur; } @@ -824,7 +907,7 @@ { utf8 = 1; - cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); + cur = encode_utf8 (cur, hi); } else *cur++ = hi; @@ -836,7 +919,7 @@ ERR ("illegal backslash escape sequence in string"); } } - else if (expect_true (ch >= 0x20 && ch <= 0x7f)) + else if (expect_true (ch >= 0x20 && ch < 0x80)) *cur++ = ch; else if (ch >= 0x80) { @@ -969,22 +1052,24 @@ { int len = dec->cur - start; - // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so + // special case the rather common 1..5-digit-int case if (*start == '-') switch (len) { - case 2: return newSViv (-( start [1] - '0' * 1)); - case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); - case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); - case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); + case 2: return newSViv (-( start [1] - '0' * 1)); + case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); + case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); + case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); + case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); } else switch (len) { - case 1: return newSViv ( start [0] - '0' * 1); - case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); - case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); - case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); + case 1: return newSViv ( start [0] - '0' * 1); + case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); + case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); + case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); + case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); } { @@ -1055,6 +1140,14 @@ ERR (", or ] expected while parsing array"); ++dec->cur; + + decode_ws (dec); + + if (*dec->cur == ']' && dec->json.flags & F_RELAXED) + { + ++dec->cur; + break; + } } DEC_DEC_DEPTH; @@ -1080,7 +1173,7 @@ else for (;;) { - decode_ws (dec); EXPECT_CH ('"'); + EXPECT_CH ('"'); // heuristic: assume that // a) decode_str + hv_store_ent are abysmally slow. @@ -1094,7 +1187,7 @@ for (;;) { - // the >= 0x80 is true on most architectures + // the >= 0x80 is false on most architectures if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') { // slow path, back up and use decode_str @@ -1104,6 +1197,7 @@ decode_ws (dec); EXPECT_CH (':'); + decode_ws (dec); value = decode_sv (dec); if (!value) { @@ -1125,6 +1219,7 @@ decode_ws (dec); EXPECT_CH (':'); + decode_ws (dec); value = decode_sv (dec); if (!value) goto fail; @@ -1150,6 +1245,14 @@ ERR (", or } expected while parsing object/hash"); ++dec->cur; + + decode_ws (dec); + + if (*dec->cur == '}' && dec->json.flags & F_RELAXED) + { + ++dec->cur; + break; + } } DEC_DEC_DEPTH; @@ -1224,15 +1327,13 @@ static SV * decode_sv (dec_t *dec) { - decode_ws (dec); - // the beauty of JSON: you need exactly one character lookahead - // to parse anything. + // to parse everything. switch (*dec->cur) { case '"': ++dec->cur; return decode_str (dec); - case '[': ++dec->cur; return decode_av (dec); - case '{': ++dec->cur; return decode_hv (dec); + case '[': ++dec->cur; return decode_av (dec); + case '{': ++dec->cur; return decode_hv (dec); case '-': case '0': case '1': case '2': case '3': case '4': @@ -1288,10 +1389,10 @@ } static SV * -decode_json (SV *string, JSON *json, UV *offset_return) +decode_json (SV *string, JSON *json, STRLEN *offset_return) { dec_t dec; - UV offset; + STRLEN offset; SV *sv; SvGETMAGIC (string); @@ -1319,6 +1420,8 @@ dec.json.flags |= F_HOOK; *dec.end = 0; // this should basically be a nop, too, but make sure it's there + + decode_ws (&dec); sv = decode_sv (&dec); if (!(offset_return || !sv)) @@ -1372,6 +1475,122 @@ } ///////////////////////////////////////////////////////////////////////////// +// incremental parser + +static void +incr_parse (JSON *self) +{ + const char *p = SvPVX (self->incr_text) + self->incr_pos; + + for (;;) + { + //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D + switch (self->incr_mode) + { + // only used for intiial whitespace skipping + case INCR_M_WS: + for (;;) + { + if (*p > 0x20) + { + self->incr_mode = INCR_M_JSON; + goto incr_m_json; + } + else if (!*p) + goto interrupt; + + ++p; + } + + // skip a single char inside a string (for \\-processing) + case INCR_M_BS: + if (!*p) + goto interrupt; + + ++p; + self->incr_mode = INCR_M_STR; + goto incr_m_str; + + // inside a string + case INCR_M_STR: + incr_m_str: + for (;;) + { + if (*p == '"') + { + ++p; + self->incr_mode = INCR_M_JSON; + + if (!self->incr_nest) + goto interrupt; + + goto incr_m_json; + } + else if (*p == '\\') + { + ++p; // "virtually" consumes character after \ + + if (!*p) // if at end of string we have to switch modes + { + self->incr_mode = INCR_M_BS; + goto interrupt; + } + } + else if (!*p) + goto interrupt; + + ++p; + } + + // after initial ws, outside string + case INCR_M_JSON: + incr_m_json: + for (;;) + { + switch (*p++) + { + case 0: + --p; + goto interrupt; + + case 0x09: + case 0x0a: + case 0x0d: + case 0x20: + if (!self->incr_nest) + { + --p; // do not eat the whitespace, let the next round do it + goto interrupt; + } + break; + + case '"': + self->incr_mode = INCR_M_STR; + goto incr_m_str; + + case '[': + case '{': + ++self->incr_nest; + break; + + case ']': + case '}': + if (!--self->incr_nest) + goto interrupt; + } + } + } + + modechange: + ; + } + +interrupt: + self->incr_pos = p - SvPVX (self->incr_text); + //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D +} + +///////////////////////////////////////////////////////////////////////////// // XS interface functions MODULE = JSON::XS PACKAGE = JSON::XS @@ -1408,7 +1627,10 @@ SvPOK_only (pv); Zero (SvPVX (pv), 1, JSON); ((JSON *)SvPVX (pv))->flags = F_DEFAULT; - XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); + XPUSHs (sv_2mortal (sv_bless ( + newRV_noinc (pv), + strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) + ))); } void ascii (JSON *self, int enable = 1) @@ -1425,6 +1647,7 @@ shrink = F_SHRINK allow_blessed = F_ALLOW_BLESSED convert_blessed = F_CONV_BLESSED + relaxed = F_RELAXED PPCODE: { if (enable) @@ -1435,6 +1658,23 @@ XPUSHs (ST (0)); } +void get_ascii (JSON *self) + ALIAS: + get_ascii = F_ASCII + get_latin1 = F_LATIN1 + get_utf8 = F_UTF8 + get_indent = F_INDENT + get_canonical = F_CANONICAL + get_space_before = F_SPACE_BEFORE + get_space_after = F_SPACE_AFTER + get_allow_nonref = F_ALLOW_NONREF + get_shrink = F_SHRINK + get_allow_blessed = F_ALLOW_BLESSED + get_convert_blessed = F_CONV_BLESSED + get_relaxed = F_RELAXED + PPCODE: + XPUSHs (boolSV (self->flags & ix)); + void max_depth (JSON *self, UV max_depth = 0x80000000UL) PPCODE: { @@ -1450,6 +1690,12 @@ XPUSHs (ST (0)); } +U32 get_max_depth (JSON *self) + CODE: + RETVAL = DEC_DEPTH (self->flags); + OUTPUT: + RETVAL + void max_size (JSON *self, UV max_size = 0) PPCODE: { @@ -1466,6 +1712,12 @@ XPUSHs (ST (0)); } +int get_max_size (JSON *self) + CODE: + RETVAL = DEC_SIZE (self->flags); + OUTPUT: + RETVAL + void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) PPCODE: { @@ -1508,30 +1760,111 @@ void decode_prefix (JSON *self, SV *jsonstr) PPCODE: { - UV offset; + STRLEN offset; EXTEND (SP, 2); PUSHs (decode_json (jsonstr, self, &offset)); PUSHs (sv_2mortal (newSVuv (offset))); } +void incr_parse (JSON *self, SV *jsonstr = 0) + PPCODE: +{ + if (!self->incr_text) + self->incr_text = newSVpvn ("", 0); + + // append data, if any + if (jsonstr) + { + if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) + { + /* utf-8-ness differs, need to upgrade */ + sv_utf8_upgrade (self->incr_text); + + if (self->incr_pos) + self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) + - (U8 *)SvPVX (self->incr_text); + } + + { + STRLEN len; + const char *str = SvPV (jsonstr, len); + SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); + Move (str, SvEND (self->incr_text), len, char); + SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); + *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there + } + } + + if (GIMME_V != G_VOID) + do + { + STRLEN offset; + + incr_parse (self); + + if (!INCR_DONE (self)) + break; + + XPUSHs (decode_json (self->incr_text, self, &offset)); + + sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); + self->incr_pos -= offset; + self->incr_nest = 0; + self->incr_mode = 0; + } + while (GIMME_V == G_ARRAY); +} + +SV *incr_text (JSON *self) + ATTRS: lvalue + CODE: +{ + if (self->incr_pos) + croak ("incr_text can only be called after a successful incr_parse call in scalar context"); + + RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; +} + OUTPUT: + RETVAL + +void incr_skip (JSON *self) + CODE: +{ + if (!self->incr_pos || !INCR_DONE (self)) + croak ("incr_text can only be called after an unsuccessful incr_parse call in scalar context");//D + + sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); + self->incr_pos = 0; + self->incr_nest = 0; + self->incr_mode = 0; +} + void DESTROY (JSON *self) CODE: SvREFCNT_dec (self->cb_sk_object); SvREFCNT_dec (self->cb_object); + SvREFCNT_dec (self->incr_text); PROTOTYPES: ENABLE -void to_json (SV *scalar) +void encode_json (SV *scalar) + ALIAS: + to_json_ = 0 + encode_json = F_UTF8 PPCODE: { - JSON json = { F_DEFAULT | F_UTF8 }; + JSON json = { F_DEFAULT | ix }; XPUSHs (encode_json (scalar, &json)); } -void from_json (SV *jsonstr) +void decode_json (SV *jsonstr) + ALIAS: + from_json_ = 0 + decode_json = F_UTF8 PPCODE: { - JSON json = { F_DEFAULT | F_UTF8 }; + JSON json = { F_DEFAULT | ix }; XPUSHs (decode_json (jsonstr, &json, 0)); } +