class Transit::Writer

Transit::Writer marshals Ruby objects as transit values to an output stream. @see github.com/cognitect/transit-format

Public Class Methods

new(format, io, opts={}) click to toggle source

@param [Symbol] format required :json, :json_verbose, or :msgpack @param [IO] io required @param [Hash] opts optional

Creates a new Writer configured to write to io in format (:json, :json_verbose, :msgpack).

Use opts to register custom write handlers, associating each one with its type.

@example

json_writer                 = Transit::Writer.new(:json, io)
json_verbose_writer         = Transit::Writer.new(:json_verbose, io)
msgpack_writer              = Transit::Writer.new(:msgpack, io)
writer_with_custom_handlers = Transit::Writer.new(:json, io,
  :handlers => {Point => PointWriteHandler})

@see Transit::WriteHandlers

# File lib/transit/writer.rb, line 39
def initialize(format, io, opts={})
  @marshaler = case format
               when :json
                 Marshaler::Json.new(io, {:handlers => {},
                                          :oj_opts => {:indent => -1}}.merge(opts))
               when :json_verbose
                 Marshaler::VerboseJson.new(io, {:handlers => {}}.merge(opts))
               else
                 Marshaler::MessagePack.new(io, {:handlers => {}}.merge(opts))
               end
end

Public Instance Methods

write(obj) click to toggle source
# File lib/transit/writer.rb, line 59
def write(obj)
  @marshaler.write(obj)
end