Class: Rjq::JSON::Parser
- Inherits:
-
Object
- Object
- Rjq::JSON::Parser
- Defined in:
- lib/rjq/json/parser.rb
Defined Under Namespace
Classes: ParsedValue
Constant Summary collapse
- DEFAULT_MAX_DEPTH =
256
Class Method Summary collapse
- .parse(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
- .parse_one(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
- .parse_records(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(input, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Parser
constructor
A new instance of Parser.
- #parse_stream(locations: false) ⇒ Object
Constructor Details
#initialize(input, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Parser
Returns a new instance of Parser.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rjq/json/parser.rb', line 34 def initialize(input, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) (chunk_size, max_depth, max_number_digits, max_string_bytes) @input = InputBuffer.new(input, chunk_size: chunk_size) @seq = seq @on_error = on_error @max_depth = max_depth @max_number_digits = max_number_digits @max_string_bytes = max_string_bytes @depth = 0 @index = 0 @line = 1 @column = 1 advance if current == "\uFEFF" end |
Class Method Details
.parse(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/rjq/json/parser.rb', line 10 def parse(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) new(io_or_string, seq: seq, chunk_size: chunk_size, on_error: on_error, max_depth: max_depth, max_number_digits: max_number_digits, max_string_bytes: max_string_bytes).parse_stream end |
.parse_one(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/rjq/json/parser.rb', line 17 def parse_one(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) values = parse(io_or_string, seq: seq, chunk_size: chunk_size, max_depth: max_depth, max_number_digits: max_number_digits, max_string_bytes: max_string_bytes).take(2) raise JSONParseError, "expected one JSON value, got #{values.length}" unless values.length == 1 values.first end |
.parse_records(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rjq/json/parser.rb', line 26 def parse_records(io_or_string, seq: false, chunk_size: InputBuffer::DEFAULT_CHUNK_SIZE, on_error: nil, max_depth: DEFAULT_MAX_DEPTH, max_number_digits: nil, max_string_bytes: nil) new(io_or_string, seq: seq, chunk_size: chunk_size, on_error: on_error, max_depth: max_depth, max_number_digits: max_number_digits, max_string_bytes: max_string_bytes).parse_stream(locations: true) end |
Instance Method Details
#parse_stream(locations: false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rjq/json/parser.rb', line 50 def parse_stream(locations: false) Enumerator.new do |yielder| loop do skip_separators break if eof? begin line = @line value = parse_value yielder << (locations ? ParsedValue.new(value: value, line: line) : value) @input.discard_before(@index) skip_whitespace raise_error('expected record separator') if @seq && !(eof? || current == "\x1e") rescue JSONParseError => e raise unless @seq && @on_error @on_error.call(e.) resync_to_record_separator end end end end |