Sha256: 29c2e720398cd8491f2655212f16f242e743647c07f49065f843880c55e7797a

Contents?: true

Size: 833 Bytes

Versions: 1

Compression:

Stored size: 833 Bytes

Contents

# encoding: UTF-8

require 'json'
require 'json-write-stream/yielding'
require 'json-write-stream/stateful'

class JsonWriteStream
  DEFAULT_ENCODING = Encoding::UTF_8

  class << self
    def from_stream(stream, options = {})
      encoding = options.fetch(:encoding, DEFAULT_ENCODING)
      stream.set_encoding(encoding)

      if block_given?
        yield writer = YieldingWriter.new(stream)
        writer.close
      else
        StatefulWriter.new(stream, options)
      end
    end

    def open(file, options = {})
      encoding = options.fetch(:encoding, DEFAULT_ENCODING)
      handle = File.open(file, 'w')
      handle.set_encoding(encoding)

      if block_given?
        yield writer = YieldingWriter.new(handle)
        writer.close
      else
        StatefulWriter.new(handle, options)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-write-stream-2.0.0 lib/json-write-stream.rb