Method: Oj::Parser.new

Defined in:
ext/oj/parser.c

.new(mode = nil) ⇒ Object

Creates a new Parser with the specified mode. If no mode is provided validation is assumed. Optional arguments can be provided that match the mode. For example with the :usual mode the call might look like Oj::Parser.new(:usual, cache_keys: true).



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'ext/oj/parser.c', line 967

static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
    ojParser p = OJ_R_ALLOC(struct _ojParser);

#if HAVE_RB_EXT_RACTOR_SAFE
    // This doesn't seem to do anything.
    rb_ext_ractor_safe(true);
#endif
    memset(p, 0, sizeof(struct _ojParser));
    buf_init(&p->key);
    buf_init(&p->buf);
    p->map = value_map;

    if (argc < 1) {
        oj_set_parser_validator(p);
    } else {
        VALUE mode = argv[0];

        if (Qnil == mode) {
            oj_set_parser_validator(p);
        } else {
            const char *ms = NULL;

            switch (rb_type(mode)) {
            case RUBY_T_SYMBOL:
                mode = rb_sym2str(mode);
                // fall through
            case RUBY_T_STRING: ms = RSTRING_PTR(mode); break;
            default: rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
            }
            if (0 == strcmp("usual", ms) || 0 == strcmp("standard", ms) || 0 == strcmp("strict", ms) ||
                0 == strcmp("compat", ms)) {
                oj_set_parser_usual(p);
            } else if (0 == strcmp("object", ms)) {
                // TBD
            } else if (0 == strcmp("saj", ms)) {
                oj_set_parser_saj(p);
            } else if (0 == strcmp("validate", ms)) {
                oj_set_parser_validator(p);
            } else if (0 == strcmp("debug", ms)) {
                oj_set_parser_debug(p);
            } else {
                rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
            }
        }
        if (1 < argc) {
            VALUE ropts = argv[1];

            Check_Type(ropts, T_HASH);
            rb_hash_foreach(ropts, opt_cb, (VALUE)p);
        }
    }
    return TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
}