Class: JSON::Pure::Parser

### Home Classes Methods

### In Files

- lib/json/pure/parser.rb

### Parent

StringScanner

### Methods

- ::new
- #parse
- #quirks_mode?
- #reset

### Files

- README.rdoc

### Class/Module Index

Quicksearch

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

No matching classes.

# JSON::Pure::Parser

This class implements the JSON parser that is used to parse a JSON string into a Ruby data structure.

### Constants

ARRAY_CLOSE
ARRAY_OPEN
COLLECTION_DELIMITER
EMPTY_8BIT_STRING
FALSE
FLOAT
IGNORE
INFINITY
INTEGER
MINUS_INFINITY
NAN
NULL
OBJECT_CLOSE
OBJECT_OPEN
PAIR_DELIMITER
STRING
TRUE
UNESCAPE_MAP

  Unescape characters in strings.

UNPARSED

### Public Class Methods

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

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

It will be configured by the opts hash. 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 true, the Parser creates additions when if a matching class and create_id was found. This option defaults to false.

- object_class: Defaults to Hash

- array_class: Defaults to Array

- quirks_mode: Enables quirks_mode for parser, that is for example parsing single JSON values instead of documents is possible.

    # File lib/json/pure/parser.rb, line 73
    def initialize(source, opts = {})
      opts ||= {}
      unless @quirks_mode = opts[:quirks_mode]
        source = convert_encoding source
      end
      super source
      if !opts.key?(:max_nesting) # defaults to 100
        @max_nesting = 100
      elsif opts[:max_nesting]
        @max_nesting = opts[:max_nesting]
      else
        @max_nesting = 0
      end
      @allow_nan = !!opts[:allow_nan]
      @symbolize_names = !!opts[:symbolize_names]
      if opts.key?(:create_additions)
        @create_additions = !!opts[:create_additions]
      else
        @create_additions = false
      end
      @create_id = @create_additions ? JSON.create_id : nil
      @object_class = opts[:object_class] || Hash
      @array_class  = opts[:array_class] || Array
      @match_string = opts[:match_string]
    end

### Public Instance Methods

parse() click to toggle source

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

    # File lib/json/pure/parser.rb, line 112
    def parse
      reset
      obj = nil
      if @quirks_mode
        while !eos? && skip(IGNORE)
        end
        if eos?
          raise ParserError, "source did not contain any JSON!"
        else
          obj = parse_value
          obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
        end
      else
        until eos?
          case
          when scan(OBJECT_OPEN)
            obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
            @current_nesting = 1
            obj = parse_object
          when scan(ARRAY_OPEN)
            obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
            @current_nesting = 1
            obj = parse_array
          when skip(IGNORE)
            ;
          else
            raise ParserError, "source '#{peek(20)}' not in JSON!"
          end
        end
        obj or raise ParserError, "source did not contain any JSON!"
      end
      obj
    end

quirks_mode?() click to toggle source

    # File lib/json/pure/parser.rb, line 101
    def quirks_mode?
      !!@quirks_mode
    end

reset() click to toggle source

    # File lib/json/pure/parser.rb, line 105
    def reset
      super
      @current_nesting = 0
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.