Sha256: 2646d9672035c5a2604de37504d6dcc63748d3b719a94f000fd369ab7900cf7d

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# encoding: UTF-8

module JSON
  module Stream
    # A parser listener that builds a full, in memory, object graph from a
    # JSON document. Typically, we would use the json gem's JSON.parse() method
    # when we have the full JSON document because it's much faster than this.
    # JSON::Stream is typically used when we have a huge JSON document streaming
    # to us and we don't want to hold the entire parsed object in memory.
    # Regardless, this is a good example of how to write parser callbacks.
    #
    # parser = JSON::Stream::Parser.new
    # builder = JSON::Stream::Builder.new(parser)
    # parser << json
    # obj = builder.result
    class Builder
      METHODS = %w[start_document end_document start_object end_object start_array end_array key value]

      attr_reader :result

      def initialize(parser)
        METHODS.each do |name|
          parser.send(name, &method(name))
        end
      end

      def start_document
        @stack, @result = [], nil
      end

      def end_document
        @result = @stack.pop.obj
      end

      def start_object
        @stack.push(ObjectNode.new)
      end

      def end_object
        unless @stack.size == 1
          node = @stack.pop
          @stack[-1] << node.obj
        end
      end
      alias :end_array :end_object

      def start_array
        @stack.push(ArrayNode.new)
      end

      def key(key)
        @stack[-1] << key
      end

      def value(value)
        @stack[-1] << value
      end
    end

    class ArrayNode
      attr_reader :obj

      def initialize
        @obj = []
      end

      def <<(node)
        @obj << node
        self
      end
    end

    class ObjectNode
      attr_reader :obj

      def initialize
        @obj, @key = {}, nil
      end

      def <<(node)
        if @key
          @obj[@key] = node
          @key = nil
        else
          @key = node
        end
        self
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-stream-0.1.3 lib/json/stream/builder.rb