Module: MessagePack::Inspect::JSONStreamer

Defined in:
lib/msgpack/inspect/streamer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.object(io, depth, index) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/msgpack/inspect/streamer.rb', line 137

def self.object(io, depth, index)
  if index > 0
    io.puts ","
  end
  retval = yield
  io.puts "" # write newline after last attribute of object
  io.print "    " * (depth - 1), "  }"
  retval
end

.objects(io, depth) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/msgpack/inspect/streamer.rb', line 129

def self.objects(io, depth)
  io.puts "["
  retval = yield
  io.puts "" # newline after tailing object without comma
  io.print "  " * depth, "]"
  retval
end

Instance Method Details

#attributes(io) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/msgpack/inspect/streamer.rb', line 180

def attributes(io)
  write(:format, :header)

  super

  write(:error) if @error

  case @format
  when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
    write(:data, :value)
  when :fixmap, :map16, :map32
    write(:length)
  when :fixarray, :array16, :array32
    write(:length)
  when :fixstr, :str8, :str16, :str32
    write(:length, :data, :value)
  when :nil
    write(:data, :value)
  when :false
    write(:data, :value)
  when :true
    write(:data, :value)
  when :bin8, :bin16, :bin32
    write(:length, :data, :value)
  when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
    if @value
      write(:exttype, :length, :data, :value)
    else
      write(:exttype, :length, :data)
    end
  when :float32, :float64
    write(:data, :value)
  when :never_used
    write(:data)
  end
end

#element_keyObject



231
232
233
234
235
236
237
238
239
# File 'lib/msgpack/inspect/streamer.rb', line 231

def element_key
  if @first_kv_pair
    @first_kv_pair = false
  else
    @io.puts ","
  end
  @io.puts  %!#{indent}    { "key":!
  super
end

#element_valueObject



241
242
243
244
245
246
247
# File 'lib/msgpack/inspect/streamer.rb', line 241

def element_value
  @io.puts "," # tailing key object
  @io.puts  %!#{indent}      "value":!
  super
  @io.puts "" # newline after value object
  @io.print %!#{indent}    }!
end

#elements(&block) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/msgpack/inspect/streamer.rb', line 217

def elements(&block)
  @io.puts ","

  if @length == 0
    @io.print %!#{indent}"children": []!
    return
  end

  @io.puts %!#{indent}"children": [!
  super
  @io.puts "" # newline after last element of array/hash
  @io.print %!#{indent}]!
end

#indent(head = @first_line) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/msgpack/inspect/streamer.rb', line 147

def indent(head = @first_line)
  if head
    "    " * (@depth - 1) + "  { "
  else
    "    " * @depth
  end
end

#write(*attrs) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/msgpack/inspect/streamer.rb', line 155

def write(*attrs)
  attrs.each do |attr|
    @io.puts "," unless @first_line
    case attr
    when :format
      @io.print %!#{indent}"format": "#{@format.to_s}"!
    when :header
      @io.print %!#{indent}"header": "0x#{hex(@header)}"!
    when :data
      @io.print %!#{indent}"data": "0x#{@data}"!
    when :value
      if @value.nil?
        @io.print %!#{indent}"value": null!
      else
        @io.print %!#{indent}"value": #{@value.inspect}!
      end
    when :length
      @io.print %!#{indent}"length": #{@length}!
    when :exttype
      @io.print %!#{indent}"exttype": #{@exttype}!
    end
    @first_line = false
  end
end