Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/json/stream/buffer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JSON
module Stream
# A character buffer that expects a UTF-8 encoded stream of bytes.
Expand Down Expand Up @@ -29,6 +31,7 @@ def initialize
def <<(data)
# Avoid state machine for complete UTF-8.
if @buffer.empty?
data = data.dup
data.force_encoding(Encoding::UTF_8)
return data if data.valid_encoding?
end
Expand Down
18 changes: 10 additions & 8 deletions lib/json/stream/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JSON
module Stream
# Raised on any invalid JSON text.
Expand Down Expand Up @@ -102,8 +104,8 @@ def initialize(&block)

# Track parse stack.
@stack = []
@unicode = ""
@buf = ""
@unicode = +""
@buf = +""
@pos = -1

# Register any observers in the block.
Expand Down Expand Up @@ -178,7 +180,7 @@ def <<(data)
@state = :end_key
notify(:key, @buf)
end
@buf = ""
@buf = +""
when BACKSLASH
@state = :start_escape
when CONTROL
Expand Down Expand Up @@ -270,7 +272,7 @@ def <<(data)
@buf << ch
else
end_value(@buf.to_i)
@buf = ""
@buf = +""
@pos -= 1
redo
end
Expand All @@ -291,7 +293,7 @@ def <<(data)
@buf << ch
else
end_value(@buf.to_f)
@buf = ""
@buf = +""
@pos -= 1
redo
end
Expand All @@ -310,7 +312,7 @@ def <<(data)
else
error('Expected 0-9 digit') unless @buf =~ DIGIT_END
end_value(@buf.to_f)
@buf = ""
@buf = +""
@pos -= 1
redo
end
Expand All @@ -326,7 +328,7 @@ def <<(data)
@buf << ch
else
end_value(@buf.to_i)
@buf = ""
@buf = +""
@pos -= 1
redo
end
Expand Down Expand Up @@ -503,7 +505,7 @@ def keyword(word, value, re, ch)

if @buf.size == word.size
if @buf == word
@buf = ""
@buf = +""
end_value(value)
else
error("Expected #{word} keyword")
Expand Down