Permalink
Browse files

Parser#initialize can take a block in which to register callback procs.

  • Loading branch information...
dgraham committed Aug 1, 2010
1 parent b322e48 commit 11f53f69a1298a0e45b6dcc4c27261e8bfb286dd
Showing with 46 additions and 17 deletions.
  1. +11 −10 README
  2. +14 −1 lib/json/stream/parser.rb
  3. +21 −6 test/parser_test.rb
View
21 README
@@ -38,20 +38,21 @@ receive_data loop is where JSON::Stream is really useful. Inside our
EventMachine::Connection subclass we might have:
def post_init
- @p = JSON::Stream::Parser.new
- @p.start_document { puts "start document" }
- @p.end_document { puts "end document" }
- @p.start_object { puts "start object" }
- @p.end_object { puts "end object" }
- @p.start_array { puts "start array" "
- @p.end_array { puts "end array" }
- @p.key {|key| puts "key: #{key}" }
- @p.value {|value| puts "value: #{value}" }
+ @parser = JSON::Stream::Parser.new do
+ start_document { puts "start document" }
+ end_document { puts "end document" }
+ start_object { puts "start object" }
+ end_object { puts "end object" }
+ start_array { puts "start array" "
+ end_array { puts "end array" }
+ key {|k| puts "key: #{k}" }
+ value {|v| puts "value: #{v}" }
+ end
end
def receive_data(data)
begin
- @p << data
+ @parser << data
rescue JSON::Stream::ParserError => e
close_connection
end
View
@@ -68,11 +68,24 @@ def self.parse(json)
private "notify_#{name}"
end
- def initialize
+ # Create a new parser with an optional initialization block where
+ # we can register event callbacks. For example:
+ # parser = JSON::Stream::Parser.new do
+ # start_document { puts "start document" }
+ # end_document { puts "end document" }
+ # start_object { puts "start object" }
+ # end_object { puts "end object" }
+ # start_array { puts "start array" "
+ # end_array { puts "end array" }
+ # key {|k| puts "key: #{k}" }
+ # value {|v| puts "value: #{v}" }
+ # end
+ def initialize(&block)
@state = :start_document
@utf8 = Buffer.new
@listeners = Hash.new {|h, k| h[k] = [] }
@stack, @unicode, @buf, @pos = [], "", "", -1
+ instance_eval(&block) if block_given?
end
# Pass data into the parser to advance the state machine and
View
@@ -316,12 +316,6 @@ def test_object_trailing_comma
assert_equal(expected, events('{"key 1" : 12,}'))
end
- def test_parse
- json = "[1,2,3]"
- obj = JSON::Stream::Parser.parse(json)
- assert_equal([1,2,3], obj)
- end
-
def test_single_byte_utf8
expected = [:start_document, :start_array, [:value, "test"], :end_array, :end_document]
assert_equal(expected, events('["test"]'))
@@ -380,6 +374,27 @@ def test_partial_four_byte_utf8
assert_equal(expected, events("[\"\xF0\x90\x84\x82\"]"))
end
+ def test_parse
+ json = "[1,2,3]"
+ obj = JSON::Stream::Parser.parse(json)
+ assert_equal([1,2,3], obj)
+ end
+
+ def test_initializer_block
+ events = []
+ parser = JSON::Stream::Parser.new do
+ start_document { events << :start_document }
+ end_document { events << :end_document }
+ start_object { events << :start_object }
+ end_object { events << :end_object }
+ key {|k| events << [:key, k] }
+ value {|v| events << [:value, v] }
+ end
+ parser << '{"key":12}'
+ expected = [:start_document, :start_object, [:key, "key"], [:value, 12], :end_object, :end_document]
+ assert_equal(expected, events)
+ end
+
private
# Run a worst case, one character at a time, parse against the

0 comments on commit 11f53f6

Please sign in to comment.