Class: TurboStreamer::OjEncoder
- Inherits:
-
Object
- Object
- TurboStreamer::OjEncoder
- Defined in:
- lib/turbostreamer/encoders/oj.rb
Constant Summary collapse
- BUFFER_SIZE =
4096
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #array_close ⇒ Object
- #array_open ⇒ Object
- #capture(to = nil) ⇒ Object
- #flush ⇒ Object
-
#initialize(io, options = {}) ⇒ OjEncoder
constructor
A new instance of OjEncoder.
- #inject(string) ⇒ Object
- #key(k) ⇒ Object
- #map_close ⇒ Object
- #map_open ⇒ Object
- #value(v) ⇒ Object
Constructor Details
#initialize(io, options = {}) ⇒ OjEncoder
Returns a new instance of OjEncoder.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/turbostreamer/encoders/oj.rb', line 10 def initialize(io, ={}) @stack = [] @indexes = [] @options = {mode: :json, buffer_size: BUFFER_SIZE}.merge() @output = io @stream_writer = ::Oj::StreamWriter.new(io, @options) @write_comma_on_next_push = false end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
6 7 8 |
# File 'lib/turbostreamer/encoders/oj.rb', line 6 def output @output end |
Instance Method Details
#array_close ⇒ Object
67 68 69 70 71 |
# File 'lib/turbostreamer/encoders/oj.rb', line 67 def array_close @indexes.pop @stack.pop @stream_writer.pop end |
#array_open ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/turbostreamer/encoders/oj.rb', line 58 def array_open @stack << :array @indexes << 0 if @write_comma_on_next_push @write_comma_on_next_push = false end @stream_writer.push_array end |
#capture(to = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/turbostreamer/encoders/oj.rb', line 105 def capture(to=nil) @stream_writer.flush old_writer = @stream_writer old_output = @output @indexes << 0 @output = (to || ::StringIO.new) @stream_writer = ::Oj::StreamWriter.new(@output, @options) # This is to prevent error from OJ streamer # We will strip the brackets afterward if @stack.last == :map @stream_writer.push_object elsif @stack.last == :array @stream_writer.push_array end yield @stream_writer.pop_all @stream_writer.flush result = output.string.sub(/\A,/, ''.freeze).chomp(",".freeze).strip # Strip brackets as promised above if @stack.last == :map result = result.sub(/\A{/, ''.freeze).chomp("}".freeze) elsif @stack.last == :array result = result.sub(/\A\[/, ''.freeze).chomp("]".freeze) end # Possible for `output.string` to have value like # `[,{"key":"value"}]\n` # Thus the comma must be removed here result.sub(/\A,/, ''.freeze) ensure @indexes.pop @stream_writer = old_writer @output = old_output end |
#flush ⇒ Object
146 147 148 |
# File 'lib/turbostreamer/encoders/oj.rb', line 146 def flush @stream_writer.flush end |
#inject(string) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/turbostreamer/encoders/oj.rb', line 73 def inject(string) @stream_writer.flush # It's possible to have # `@write_comma_on_next_push == true` and `@indexes.last > 0` # So there might be double comma written without this flag comma_written = false if @write_comma_on_next_push @output.write(",".freeze) @write_comma_on_next_push = false comma_written = true end if @stack.last == :array && !string.empty? if @indexes.last > 0 self.output.write(",") unless comma_written else @write_comma_on_next_push = true end @indexes[-1] += 1 elsif @stack.last == :map && !string.empty? if @indexes.last > 0 self.output.write(",") unless comma_written else @write_comma_on_next_push = true end @indexes[-1] += 1 end self.output.write(string.sub(/\A,/, ''.freeze).chomp(",".freeze).strip) end |
#key(k) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/turbostreamer/encoders/oj.rb', line 21 def key(k) if @write_comma_on_next_push && (@stack.last == :array || @stack.last == :map) @stream_writer.flush @output.write(",".freeze) @write_comma_on_next_push = false end @stream_writer.push_key(k) end |
#map_close ⇒ Object
52 53 54 55 56 |
# File 'lib/turbostreamer/encoders/oj.rb', line 52 def map_close @indexes.pop @stack.pop @stream_writer.pop end |
#map_open ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/turbostreamer/encoders/oj.rb', line 43 def map_open @stack << :map @indexes << 0 if @write_comma_on_next_push @write_comma_on_next_push = false end @stream_writer.push_object end |
#value(v) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/turbostreamer/encoders/oj.rb', line 30 def value(v) if @stack.last == :array || @stack.last == :map @indexes[-1] += 1 if @write_comma_on_next_push @stream_writer.flush @output.write(",".freeze) @write_comma_on_next_push = false end end @stream_writer.push_value(v) end |