lib/json-write-stream/yielding.rb in json-write-stream-1.2.0 vs lib/json-write-stream/yielding.rb in json-write-stream-2.0.0
- old
+ new
@@ -1,14 +1,15 @@
# encoding: UTF-8
class JsonWriteStream
class YieldingWriter
- attr_reader :stream, :index
+ attr_reader :stream, :index, :options
- def initialize(stream)
+ def initialize(stream, options = {})
@stream = stream
@index = 0
+ @options = options
after_initialize
end
def after_initialize
end
@@ -58,37 +59,31 @@
class YieldingObjectWriter < YieldingWriter
def after_initialize
stream.write('{')
end
- def write_array(key, options = DEFAULT_OPTIONS)
+ def write_array(key)
write_comma
- stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
increment
write_key(key)
stream.write(':')
- stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
super(true)
end
- def write_object(key, options = DEFAULT_OPTIONS)
+ def write_object(key)
write_comma
- stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
increment
write_key(key)
stream.write(':')
- stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
super(true)
end
- def write_key_value(key, value, options = DEFAULT_OPTIONS)
+ def write_key_value(key, value)
write_comma
- stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
increment
write_key(key)
stream.write(':')
- stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
stream.write(escape(value))
end
def close
stream.write('}')
@@ -104,12 +99,11 @@
class YieldingArrayWriter < YieldingWriter
def after_initialize
stream.write('[')
end
- def write_element(element, options = DEFAULT_OPTIONS)
+ def write_element(element)
write_comma
- stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
increment
stream.write(escape(element))
end
def close