Class: JSON::Ext::Parser (Ruby 2.3.2)
Description: Class : JSON::Ext::Parser - Ruby 2.3.2

- Home
- Core 2.3.2
- Std-lib 2.3.2
- Downloads

Support for the Ruby 2.3 series has ended. See here for reference.

### Home Classes Methods

### In Files

- json/generator/generator.c

### Parent

Object

### Methods

- ::new
- #parse
- #quirks_mode?
- #source

### Class/Module Index

Quicksearch

- BigDecimal
- Class
- Complex
- Date
- DateTime
- Exception
- JSON
- JSON::Ext
- JSON::Ext::Generator
- JSON::Ext::Generator::GeneratorMethods
- JSON::Ext::Generator::GeneratorMethods::Array
- JSON::Ext::Generator::GeneratorMethods::Bignum
- JSON::Ext::Generator::GeneratorMethods::FalseClass
- JSON::Ext::Generator::GeneratorMethods::Fixnum
- JSON::Ext::Generator::GeneratorMethods::Float
- JSON::Ext::Generator::GeneratorMethods::Hash
- JSON::Ext::Generator::GeneratorMethods::NilClass
- JSON::Ext::Generator::GeneratorMethods::Object
- JSON::Ext::Generator::GeneratorMethods::String
- JSON::Ext::Generator::GeneratorMethods::String::Extend
- JSON::Ext::Generator::GeneratorMethods::TrueClass
- JSON::Ext::Generator::State
- JSON::Ext::Parser
- JSON::GeneratorError
- JSON::GenericObject
- JSON::JSONError
- JSON::MissingUnicodeSupport
- JSON::NestingError
- JSON::ParserError
- JSON::UnparserError
- Kernel
- OpenStruct
- Range
- Rational
- Regexp
- Struct
- Symbol
- Time

No matching classes.

# JSON::Ext::Parser

This is the JSON parser implemented as a C extension. It can be configured to be used by setting

    JSON.parser = JSON::Ext::Parser

with the method parser= in JSON.

### Public Class Methods

new(source, opts => {}) click to toggle source

Creates a new JSON::Ext::Parser instance for the string source.

Creates a new JSON::Ext::Parser instance for the string source.

It will be configured by the opts hash. opts can have the following keys:

opts can have the following keys:

- max_nesting: The maximum depth of nesting allowed in the parsed data structures. Disable depth checking with :max_nesting => false|nil|0, it defaults to 100.

- allow_nan: If set to true, allow NaN, Infinity and -Infinity in defiance of RFC 4627 to be parsed by the Parser. This option defaults to false.

- symbolize_names: If set to true, returns symbols for the names (keys) in a JSON object. Otherwise strings are returned, which is also the default.

- create_additions: If set to false, the Parser doesn't create additions even if a matching class and create_id was found. This option defaults to false.

- object_class: Defaults to Hash

- array_class: Defaults to 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");
        }
    #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
        rb_scan_args(argc, argv, "1:", &source, &opts);
    #else
        rb_scan_args(argc, argv, "11", &source, &opts);
    #endif
        if (!NIL_P(opts)) {
    #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
            opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
            if (NIL_P(opts)) {
                rb_raise(rb_eArgError, "opts needs to be like a hash");
            } else {
    #endif
                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_quirks_mode);
                if (option_given_p(opts, tmp)) {
                    VALUE quirks_mode = rb_hash_aref(opts, tmp);
                    json->quirks_mode = RTEST(quirks_mode) ? 1 : 0;
                } else {
                    json->quirks_mode = 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;
                }
                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_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;
                }
    #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
            }
    #endif
        } else {
            json->max_nesting = 100;
            json->allow_nan = 0;
            json->create_additions = 1;
            json->create_id = rb_funcall(mJSON, i_create_id, 0);
            json->object_class = Qnil;
            json->array_class = Qnil;
        }
        StringValue(source);
        if (!json->quirks_mode) {
            source = convert_encoding(source);
        }
        json->current_nesting = 0;
        json->len = RSTRING_LEN(source);
        json->source = RSTRING_PTR(source);;
        json->Vsource = source;
        return self;
    }

### Public Instance Methods

parse() click to toggle source

Parses the current JSON text source and returns the complete data structure as a result.

                   static VALUE cParser_parse(VALUE self)
    {
      GET_PARSER;

      if (json->quirks_mode) {
        return cParser_parse_quirks_mode(self);
      } else {
        return cParser_parse_strict(self);
      }
    }

quirks_mode?() click to toggle source

Returns a true, if this parser is in quirks_mode, false otherwise.

                   static VALUE cParser_quirks_mode_p(VALUE self)
    {
        GET_PARSER;
        return json->quirks_mode ? Qtrue : Qfalse;
    }

source() click to toggle source

Returns a copy of the current source string, that was used to construct this Parser.

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