module MessagePack::Inspect::JSONStreamer

Public Class Methods

object(io, depth, index) { || ... } click to toggle source
# 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) { || ... } click to toggle source
# 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

Public Instance Methods

attributes(io) click to toggle source
Calls superclass method
# 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_key() click to toggle source
Calls superclass method
# 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_value() click to toggle source
Calls superclass method
# 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) click to toggle source
Calls superclass method
# 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) click to toggle source
# File lib/msgpack/inspect/streamer.rb, line 147
def indent(head = @first_line)
  if head
    "    " * (@depth - 1) + "  { "
  else
    "    " * @depth
  end
end
write(*attrs) click to toggle source
# 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