Class: Transit::Writer
- Inherits:
-
Object
- Object
- Transit::Writer
- Defined in:
- lib/transit/writer.rb
Overview
Transit::Writer marshals Ruby objects as transit values to an output stream.
Instance Method Summary collapse
-
#initialize(format, io, opts = {}) ⇒ Writer
constructor
Creates a new Writer configured to write to io in format (:json, :json_verbose, :msgpack).
- #write(obj) ⇒ Object
Constructor Details
#initialize(format, io, opts = {}) ⇒ Writer
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/transit/writer.rb', line 39 def initialize(format, io, opts={}) @marshaler = case format when :json Marshaler::Json.new(io, {:prefer_strings => true, :verbose => false, :handlers => {}, :oj_opts => {:indent => -1}}.merge(opts)) when :json_verbose Marshaler::VerboseJson.new(io, {:prefer_strings => true, :verbose => true, :handlers => {}}.merge(opts)) else Marshaler::MessagePack.new(io, {:prefer_strings => false, :verbose => false, :handlers => {}}.merge(opts)) end end |
Instance Method Details
#write(obj) ⇒ Object
68 69 70 |
# File 'lib/transit/writer.rb', line 68 def write(obj) @marshaler.write(obj) end |