Class: JSON::Stream::Builder
- Inherits:
-
Object
- Object
- JSON::Stream::Builder
- Defined in:
- lib/json/stream/builder.rb
Overview
Constant Summary collapse
- METHODS =
%w[start_document end_document start_object end_object start_array end_array key value]
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #end_document ⇒ Object
- #end_object ⇒ Object (also: #end_array)
-
#initialize(parser) ⇒ Builder
constructor
A new instance of Builder.
- #key(key) ⇒ Object
- #start_array ⇒ Object
- #start_document ⇒ Object
- #start_object ⇒ Object
- #value(value) ⇒ Object
Constructor Details
permalink #initialize(parser) ⇒ Builder
Returns a new instance of Builder.
19 20 21 22 23 |
# File 'lib/json/stream/builder.rb', line 19 def initialize(parser) METHODS.each do |name| parser.send(name, &method(name)) end end |
Instance Attribute Details
permalink #result ⇒ Object (readonly)
Returns the value of attribute result.
17 18 19 |
# File 'lib/json/stream/builder.rb', line 17 def result @result end |
Instance Method Details
permalink #end_document ⇒ Object
[View source]
31 32 33 |
# File 'lib/json/stream/builder.rb', line 31 def end_document @result = @stack.pop end |
permalink #end_object ⇒ Object Also known as: end_array
[View source]
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/json/stream/builder.rb', line 39 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 |
permalink #key(key) ⇒ Object
[View source]
58 59 60 |
# File 'lib/json/stream/builder.rb', line 58 def key(key) @keys << key end |
permalink #start_array ⇒ Object
[View source]
54 55 56 |
# File 'lib/json/stream/builder.rb', line 54 def start_array @stack.push([]) end |
permalink #start_document ⇒ Object
[View source]
25 26 27 28 29 |
# File 'lib/json/stream/builder.rb', line 25 def start_document @stack = [] @keys = [] @result = nil end |
permalink #start_object ⇒ Object
[View source]
35 36 37 |
# File 'lib/json/stream/builder.rb', line 35 def start_object @stack.push({}) end |
permalink #value(value) ⇒ Object
[View source]
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/json/stream/builder.rb', line 62 def value(value) top = @stack[-1] case top when Hash top[@keys.pop] = value when Array top << value else @stack << value end end |