Sha256: 6cc51ef32c90f71ff4a4d20da88ce9285d0aab0f494e2a7f97b0f46278e76ca1

Contents?: true

Size: 780 Bytes

Versions: 1

Compression:

Stored size: 780 Bytes

Contents

# encoding: UTF-8

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

class JsonWriteStream
  DEFAULT_ENCODING = Encoding::UTF_8
  DEFAULT_OPTIONS = { before: '', between: '' }.freeze

  class << self
    def from_stream(stream, encoding = DEFAULT_ENCODING)
      stream.set_encoding(encoding)

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

    def open(file, 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)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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