类 JSON::Ext::Parser

这是作为 C 扩展实现的 JSON 解析器。可以通过设置

JSON.parser = JSON::Ext::Parser

使用 JSON 中的 parser= 方法。

公共类方法

new(source, opts → {}) 点击切换源代码

为字符串 source 创建一个新的 JSON::Ext::Parser 实例。

它将由 opts 哈希配置。opts 可以包含以下键

opts 可以包含以下键

  • max_nesting: 解析的数据结构中允许的最大嵌套深度。使用 :max_nesting => false|nil|0 禁用深度检查,默认值为 100。

  • allow_nan: 如果设置为 true,则允许 NaN、Infinity 和 -Infinity 违反 RFC 4627 被 Parser 解析。此选项默认为 false。

  • symbolize_names: 如果设置为 true,则返回 JSON 对象中名称(键)的符号。否则返回字符串,这也是默认值。无法将此选项与 create_additions 选项一起使用。

  • create_additions: 如果设置为 false,则即使找到匹配的类和 create_id,Parser 也不会创建附加项。此选项默认为 false。

  • object_class: 默认值为 Hash

  • array_class: 默认值为 Array

static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE source, opts;
    GET_PARSER_INIT;

    if (json->Vsource) {
        rb_raise(rb_eTypeError, "already initialized instance");
    }
    rb_scan_args(argc, argv, "1:", &source, &opts);
    if (!NIL_P(opts)) {
        VALUE tmp = ID2SYM(i_max_nesting);
        if (option_given_p(opts, tmp)) {
            VALUE max_nesting = rb_hash_aref(opts, tmp);
            if (RTEST(max_nesting)) {
                Check_Type(max_nesting, T_FIXNUM);
                json->max_nesting = FIX2INT(max_nesting);
            } else {
                json->max_nesting = 0;
            }
        } else {
            json->max_nesting = 100;
        }
        tmp = ID2SYM(i_allow_nan);
        if (option_given_p(opts, tmp)) {
            json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
        } else {
            json->allow_nan = 0;
        }
        tmp = ID2SYM(i_symbolize_names);
        if (option_given_p(opts, tmp)) {
            json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
        } else {
            json->symbolize_names = 0;
        }
        tmp = ID2SYM(i_freeze);
        if (option_given_p(opts, tmp)) {
            json->freeze = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
        } else {
            json->freeze = 0;
        }
        tmp = ID2SYM(i_create_additions);
        if (option_given_p(opts, tmp)) {
            json->create_additions = RTEST(rb_hash_aref(opts, tmp));
        } else {
            json->create_additions = 0;
        }
        if (json->symbolize_names && json->create_additions) {
            rb_raise(rb_eArgError,
                     "options :symbolize_names and :create_additions cannot be "
                     " used in conjunction");
        }
        tmp = ID2SYM(i_create_id);
        if (option_given_p(opts, tmp)) {
            json->create_id = rb_hash_aref(opts, tmp);
        } else {
            json->create_id = rb_funcall(mJSON, i_create_id, 0);
        }
        tmp = ID2SYM(i_object_class);
        if (option_given_p(opts, tmp)) {
            json->object_class = rb_hash_aref(opts, tmp);
        } else {
            json->object_class = Qnil;
        }
        tmp = ID2SYM(i_array_class);
        if (option_given_p(opts, tmp)) {
            json->array_class = rb_hash_aref(opts, tmp);
        } else {
            json->array_class = Qnil;
        }
        tmp = ID2SYM(i_decimal_class);
        if (option_given_p(opts, tmp)) {
            json->decimal_class = rb_hash_aref(opts, tmp);
        } else {
            json->decimal_class = Qnil;
        }
        tmp = ID2SYM(i_match_string);
        if (option_given_p(opts, tmp)) {
            VALUE match_string = rb_hash_aref(opts, tmp);
            json->match_string = RTEST(match_string) ? match_string : Qnil;
        } else {
            json->match_string = Qnil;
        }
    } else {
        json->max_nesting = 100;
        json->allow_nan = 0;
        json->create_additions = 0;
        json->create_id = Qnil;
        json->object_class = Qnil;
        json->array_class = Qnil;
        json->decimal_class = Qnil;
    }
    source = convert_encoding(StringValue(source));
    StringValue(source);
    json->len = RSTRING_LEN(source);
    json->source = RSTRING_PTR(source);;
    json->Vsource = source;
    return self;
}

公共实例方法

parse() 点击切换源代码

解析当前 JSON 文本 source 并返回完整的数据结构作为结果。如果解析失败,则会引发 JSON::ParserError

static VALUE cParser_parse(VALUE self)
{
  char *p, *pe;
  int cs = EVIL;
  VALUE result = Qnil;
  GET_PARSER;


#line 1946 "parser.c"
        {
        cs = JSON_start;
        }

#line 845 "parser.rl"
  p = json->source;
  pe = p + json->len;

#line 1955 "parser.c"
        {
        if ( p == pe )
                goto _test_eof;
        switch ( cs )
        {
st1:
        if ( ++p == pe )
                goto _test_eof1;
case 1:
        switch( (*p) ) {
                case 13: goto st1;
                case 32: goto st1;
                case 34: goto tr2;
                case 45: goto tr2;
                case 47: goto st6;
                case 73: goto tr2;
                case 78: goto tr2;
                case 91: goto tr2;
                case 102: goto tr2;
                case 110: goto tr2;
                case 116: goto tr2;
                case 123: goto tr2;
        }
        if ( (*p) > 10 ) {
                if ( 48 <= (*p) && (*p) <= 57 )
                        goto tr2;
        } else if ( (*p) >= 9 )
                goto st1;
        goto st0;
st0:
cs = 0;
        goto _out;
tr2:
#line 820 "parser.rl"
        {
        char *np = JSON_parse_value(json, p, pe, &result, 0);
        if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
    }
        goto st10;
st10:
        if ( ++p == pe )
                goto _test_eof10;
case 10:
#line 1999 "parser.c"
        switch( (*p) ) {
                case 13: goto st10;
                case 32: goto st10;
                case 47: goto st2;
        }
        if ( 9 <= (*p) && (*p) <= 10 )
                goto st10;
        goto st0;
st2:
        if ( ++p == pe )
                goto _test_eof2;
case 2:
        switch( (*p) ) {
                case 42: goto st3;
                case 47: goto st5;
        }
        goto st0;
st3:
        if ( ++p == pe )
                goto _test_eof3;
case 3:
        if ( (*p) == 42 )
                goto st4;
        goto st3;
st4:
        if ( ++p == pe )
                goto _test_eof4;
case 4:
        switch( (*p) ) {
                case 42: goto st4;
                case 47: goto st10;
        }
        goto st3;
st5:
        if ( ++p == pe )
                goto _test_eof5;
case 5:
        if ( (*p) == 10 )
                goto st10;
        goto st5;
st6:
        if ( ++p == pe )
                goto _test_eof6;
case 6:
        switch( (*p) ) {
                case 42: goto st7;
                case 47: goto st9;
        }
        goto st0;
st7:
        if ( ++p == pe )
                goto _test_eof7;
case 7:
        if ( (*p) == 42 )
                goto st8;
        goto st7;
st8:
        if ( ++p == pe )
                goto _test_eof8;
case 8:
        switch( (*p) ) {
                case 42: goto st8;
                case 47: goto st1;
        }
        goto st7;
st9:
        if ( ++p == pe )
                goto _test_eof9;
case 9:
        if ( (*p) == 10 )
                goto st1;
        goto st9;
        }
        _test_eof1: cs = 1; goto _test_eof;
        _test_eof10: cs = 10; goto _test_eof;
        _test_eof2: cs = 2; goto _test_eof;
        _test_eof3: cs = 3; goto _test_eof;
        _test_eof4: cs = 4; goto _test_eof;
        _test_eof5: cs = 5; goto _test_eof;
        _test_eof6: cs = 6; goto _test_eof;
        _test_eof7: cs = 7; goto _test_eof;
        _test_eof8: cs = 8; goto _test_eof;
        _test_eof9: cs = 9; goto _test_eof;

        _test_eof: {}
        _out: {}
        }

#line 848 "parser.rl"

  if (cs >= JSON_first_final && p == pe) {
    return result;
  } else {
    rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
    return Qnil;
  }
}
source() 点击切换源代码

返回用于构建此 Parser 的当前 source 字符串的副本。

static VALUE cParser_source(VALUE self)
{
    GET_PARSER;
    return rb_str_dup(json->Vsource);
}