Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reset parser status? #3

Closed
hiroyuki-sato opened this issue May 14, 2015 · 1 comment
Closed

How to reset parser status? #3

hiroyuki-sato opened this issue May 14, 2015 · 1 comment

Comments

@hiroyuki-sato
Copy link

Hello

I would like to parse multiple JSON string. I referenced JSON::Stream::Builder class.
And I create reset_state method for parse multiple JSON string in JSON::Stream::Parser class.

Do you have any idea to reset status?

Best regards.


Hiroyuki Sato.

  module Stream
    class Parser
      def reset_state
        @state = :start_document
      end
    end
  end
end

reset status in Builder class

  def end_document
    #....
    @parser.reset_state
    #....
  end

complete code is below.

require 'json/stream'
require 'pp'


class Builder
  METHODS = %w[start_document end_document start_object end_object start_array end_array key value]

  attr_reader :result

  def initialize(parser)
    @parser = parser
    METHODS.each do |name|
      parser.send(name, &method(name))
    end
    @callbacks = []
  end

  def register_callback(&block)
    @callbacks.push block
  end

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

  def end_document
    @result = @stack.pop
    @callbacks.each{ |c| c.call @result }
    @parser.reset_state
    @result
  end

  def start_object
    @stack.push({})
  end

  def end_object
    return if @stack.size == 1

    node = @stack.pop
    top = @stack[-1]

    case top
    when Hash
      top[@keys.pop] = node
    when Array
      top << node
    end
  end
  alias :end_array :end_object

  def start_array
    @stack.push([])
  end

  def key(key)
    @keys << key
  end

  def value(value)
    top = @stack[-1]
    case top
    when Hash
      top[@keys.pop] = value
    when Array
      top << value
    else
      @stack << value
    end
  end
end

module JSON
  module Stream
    class Parser
      def reset_state
        @state = :start_document
      end
    end
  end
end


parser = JSON::Stream::Parser.new
builder = Builder.new(parser)

builder.register_callback do |object|
  puts '-----'
  pp object
end

TEST=<<-EOS
{"test":[1,2,3]}
{"test":[1,2,3]}
EOS

TEST2=<<-EOS
{"test":[1,2,3]
EOS

TEST3='}'
parser << TEST
parser << TEST2
parser << TEST3
@dgraham
Copy link
Owner

dgraham commented Nov 8, 2016

The Parser class is designed to parse a single document right now. The reset_state method you added needs to perform the same steps as the constructor.

@dgraham dgraham closed this as completed Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants