module JSON::Pure::Generator::GeneratorMethods::Hash - JSON implemention for Ruby

# module JSON::Pure::Generator::GeneratorMethods::Hash

### Public Instance Methods

to_json(state = nil, *) click to toggle source

Returns a JSON string containing a JSON object, that is unparsed from this Hash instance. state is a JSON::State object, that can also be used to configure the produced JSON string output further. depth is used to find out nesting depth, to indent accordingly.

    # File lib/json/pure/generator.rb, line 280
    def to_json(state = nil, *)
      state = State.from_state(state)
      state.check_max_nesting
      json_transform(state)
    end

### Private Instance Methods

json_shift(state) click to toggle source

    # File lib/json/pure/generator.rb, line 288
    def json_shift(state)
      state.object_nl.empty? or return ''
      state.indent * state.depth
    end

json_transform(state) click to toggle source

    # File lib/json/pure/generator.rb, line 293
    def json_transform(state)
      delim = ','
      delim << state.object_nl
      result = '{'
      result << state.object_nl
      depth = state.depth += 1
      first = true
      indent = !state.object_nl.empty?
      each { |key,value|
        result << delim unless first
        result << state.indent * depth if indent
        result << key.to_s.to_json(state)
        result << state.space_before
        result << ':'
        result << state.space
        if value.respond_to?(:to_json)
          result << value.to_json(state)
        else
          result << %Q{"#{String(value)}"}
        end
        first = false
      }
      depth = state.depth -= 1
      result << state.object_nl
      result << state.indent * depth if indent
      result << '}'
      result
    end