json-write-stream 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -3
- data/README.md +25 -0
- data/lib/json-write-stream.rb +6 -5
- data/lib/json-write-stream/stateful.rb +104 -29
- data/lib/json-write-stream/version.rb +1 -1
- data/lib/json-write-stream/yielding.rb +7 -13
- data/spec/json-write-stream_spec.rb +4 -4
- data/spec/shared_examples.rb +10 -10
- data/spec/spec_helper.rb +7 -8
- data/spec/stateful_spec.rb +100 -14
- data/spec/yielding_spec.rb +3 -18
- metadata +3 -4
- data/History.txt +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f3f098796321aaf71c015a22ee3b2c5fc7044ad0d67e5cbfd1c6c11ec8f47bec
|
4
|
+
data.tar.gz: 476ab378e44c73e9630642a91422224db73812414d2e9fb0bb504699a19c1d57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5be8cc906b92bef8ab60e81c535af4937ceccb412271d3eedfbabf41f05fc0a4e4f12877c1bf65054981c19e36e4ee6d8c57471eb9d4574beeb4f51513c3b16b
|
7
|
+
data.tar.gz: acc20ad490eed07edaa2d93263d3ab5927570c410c4d8be7da5c71b05e60a051b25b52f2c056d376cca7dff64afd2ebaa1426c82879160b23d75bb65e1096d3c
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -102,6 +102,31 @@ writer.write_object
|
|
102
102
|
writer.close
|
103
103
|
```
|
104
104
|
|
105
|
+
### Options
|
106
|
+
|
107
|
+
JsonWriteStream supports generating "pretty" JSON, i.e. JSON formatted in a more human-readable way. Currently only the stateful writer supports pretty generation. Example:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
stream = StringIO.new
|
111
|
+
writer = JsonWriteStream.from_stream(stream, pretty: true)
|
112
|
+
writer.write_object
|
113
|
+
writer.write_key_value('foo', 'bar')
|
114
|
+
writer.write_array('baz')
|
115
|
+
writer.write_element('goo')
|
116
|
+
writer.close
|
117
|
+
```
|
118
|
+
|
119
|
+
Now `stream.string` will contain
|
120
|
+
|
121
|
+
```json
|
122
|
+
{
|
123
|
+
"foo": "bar",
|
124
|
+
"baz": [
|
125
|
+
"goo"
|
126
|
+
]
|
127
|
+
}
|
128
|
+
```
|
129
|
+
|
105
130
|
## Requirements
|
106
131
|
|
107
132
|
No external requirements.
|
data/lib/json-write-stream.rb
CHANGED
@@ -6,21 +6,22 @@ require 'json-write-stream/stateful'
|
|
6
6
|
|
7
7
|
class JsonWriteStream
|
8
8
|
DEFAULT_ENCODING = Encoding::UTF_8
|
9
|
-
DEFAULT_OPTIONS = { before: '', between: '' }.freeze
|
10
9
|
|
11
10
|
class << self
|
12
|
-
def from_stream(stream,
|
11
|
+
def from_stream(stream, options = {})
|
12
|
+
encoding = options.fetch(:encoding, DEFAULT_ENCODING)
|
13
13
|
stream.set_encoding(encoding)
|
14
14
|
|
15
15
|
if block_given?
|
16
16
|
yield writer = YieldingWriter.new(stream)
|
17
17
|
writer.close
|
18
18
|
else
|
19
|
-
StatefulWriter.new(stream)
|
19
|
+
StatefulWriter.new(stream, options)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def open(file,
|
23
|
+
def open(file, options = {})
|
24
|
+
encoding = options.fetch(:encoding, DEFAULT_ENCODING)
|
24
25
|
handle = File.open(file, 'w')
|
25
26
|
handle.set_encoding(encoding)
|
26
27
|
|
@@ -28,7 +29,7 @@ class JsonWriteStream
|
|
28
29
|
yield writer = YieldingWriter.new(handle)
|
29
30
|
writer.close
|
30
31
|
else
|
31
|
-
StatefulWriter.new(handle)
|
32
|
+
StatefulWriter.new(handle, options)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
@@ -1,35 +1,47 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'json'
|
4
|
+
|
3
5
|
class JsonWriteStream
|
4
6
|
class NotInObjectError < StandardError; end
|
5
7
|
class NotInArrayError < StandardError; end
|
6
8
|
class EndOfStreamError < StandardError; end
|
7
9
|
|
8
10
|
class StatefulWriter
|
9
|
-
attr_reader :stream, :index, :
|
10
|
-
|
11
|
+
attr_reader :stream, :stack, :index, :closed, :options
|
12
|
+
alias_method :closed?, :closed
|
11
13
|
|
12
|
-
def initialize(stream)
|
14
|
+
def initialize(stream, options = {})
|
13
15
|
@stream = stream
|
14
|
-
@index = 0
|
15
16
|
@stack = []
|
16
17
|
@closed = false
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def after_initialize
|
18
|
+
@options = options
|
19
|
+
@index = 0
|
21
20
|
end
|
22
21
|
|
23
22
|
def write_object(*args)
|
24
23
|
check_eos
|
25
|
-
|
26
|
-
|
24
|
+
new_indent_level = 1
|
25
|
+
|
26
|
+
if current
|
27
|
+
current.write_object(*args)
|
28
|
+
new_indent_level = current.indent_level + 1
|
29
|
+
end
|
30
|
+
|
31
|
+
stack.push(StatefulObjectWriter.new(self, new_indent_level))
|
27
32
|
end
|
28
33
|
|
29
34
|
def write_array(*args)
|
30
35
|
check_eos
|
31
|
-
|
32
|
-
|
36
|
+
|
37
|
+
new_indent_level = 1
|
38
|
+
|
39
|
+
if current
|
40
|
+
current.write_array(*args)
|
41
|
+
new_indent_level = current.indent_level + 1
|
42
|
+
end
|
43
|
+
|
44
|
+
stack.push(StatefulArrayWriter.new(self, new_indent_level))
|
33
45
|
end
|
34
46
|
|
35
47
|
def write_key_value(*args)
|
@@ -93,8 +105,20 @@ class JsonWriteStream
|
|
93
105
|
(stack.size == 0 && index > 0) || closed?
|
94
106
|
end
|
95
107
|
|
108
|
+
def pretty?
|
109
|
+
options.fetch(:pretty, false)
|
110
|
+
end
|
111
|
+
|
112
|
+
def indent_size
|
113
|
+
options.fetch(:indent_size, 2)
|
114
|
+
end
|
115
|
+
|
96
116
|
protected
|
97
117
|
|
118
|
+
def increment
|
119
|
+
@index += 1
|
120
|
+
end
|
121
|
+
|
98
122
|
def check_eos
|
99
123
|
if eos?
|
100
124
|
raise EndOfStreamError, 'end of stream.'
|
@@ -104,53 +128,99 @@ class JsonWriteStream
|
|
104
128
|
def current
|
105
129
|
stack.last
|
106
130
|
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class BaseWriter
|
134
|
+
attr_reader :writer, :indent_level, :index
|
135
|
+
|
136
|
+
def initialize(writer, indent_level)
|
137
|
+
@writer = writer
|
138
|
+
@indent_level = indent_level
|
139
|
+
@index = 0
|
140
|
+
after_initialize
|
141
|
+
end
|
142
|
+
|
143
|
+
def after_initialize
|
144
|
+
end
|
145
|
+
|
146
|
+
def stream
|
147
|
+
writer.stream
|
148
|
+
end
|
149
|
+
|
150
|
+
def increment
|
151
|
+
@index += 1
|
152
|
+
end
|
153
|
+
|
154
|
+
def indent(level = indent_level)
|
155
|
+
stream.write(' ' * indent_size * level) if pretty?
|
156
|
+
end
|
157
|
+
|
158
|
+
def indent_size
|
159
|
+
writer.indent_size
|
160
|
+
end
|
107
161
|
|
108
162
|
def escape(str)
|
109
163
|
JSON.generate([str])[1..-2]
|
110
164
|
end
|
111
165
|
|
112
166
|
def write_comma
|
113
|
-
|
167
|
+
if index > 0
|
168
|
+
stream.write(",")
|
169
|
+
write_newline
|
170
|
+
end
|
114
171
|
end
|
115
172
|
|
116
|
-
def
|
117
|
-
|
173
|
+
def write_colon
|
174
|
+
stream.write(':')
|
175
|
+
stream.write(' ') if pretty?
|
176
|
+
end
|
177
|
+
|
178
|
+
def write_newline
|
179
|
+
stream.write("\n") if pretty?
|
180
|
+
end
|
181
|
+
|
182
|
+
def pretty?
|
183
|
+
writer.pretty?
|
118
184
|
end
|
119
185
|
end
|
120
186
|
|
121
|
-
class StatefulObjectWriter <
|
187
|
+
class StatefulObjectWriter < BaseWriter
|
122
188
|
def after_initialize
|
123
|
-
stream.write(
|
189
|
+
stream.write("{")
|
190
|
+
write_newline
|
124
191
|
end
|
125
192
|
|
126
193
|
# prep work (array is written afterwards)
|
127
194
|
def write_array(key)
|
128
195
|
write_comma
|
129
196
|
increment
|
197
|
+
indent
|
130
198
|
write_key(key)
|
131
|
-
|
199
|
+
write_colon
|
132
200
|
end
|
133
201
|
|
134
202
|
# prep work (object is written afterwards)
|
135
203
|
def write_object(key)
|
136
204
|
write_comma
|
137
205
|
increment
|
206
|
+
indent
|
138
207
|
write_key(key)
|
139
|
-
|
208
|
+
write_colon
|
140
209
|
end
|
141
210
|
|
142
|
-
def write_key_value(key, value
|
211
|
+
def write_key_value(key, value)
|
143
212
|
write_comma
|
144
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
145
213
|
increment
|
214
|
+
indent
|
146
215
|
write_key(key)
|
147
|
-
|
148
|
-
stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
|
216
|
+
write_colon
|
149
217
|
stream.write(escape(value))
|
150
218
|
end
|
151
219
|
|
152
220
|
def close
|
153
|
-
|
221
|
+
write_newline
|
222
|
+
indent(indent_level - 1)
|
223
|
+
stream.write("}")
|
154
224
|
end
|
155
225
|
|
156
226
|
def is_object?
|
@@ -173,15 +243,16 @@ class JsonWriteStream
|
|
173
243
|
end
|
174
244
|
end
|
175
245
|
|
176
|
-
class StatefulArrayWriter <
|
246
|
+
class StatefulArrayWriter < BaseWriter
|
177
247
|
def after_initialize
|
178
|
-
stream.write(
|
248
|
+
stream.write("[")
|
249
|
+
write_newline
|
179
250
|
end
|
180
251
|
|
181
|
-
def write_element(element
|
252
|
+
def write_element(element)
|
182
253
|
write_comma
|
183
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
184
254
|
increment
|
255
|
+
indent
|
185
256
|
stream.write(escape(element))
|
186
257
|
end
|
187
258
|
|
@@ -189,16 +260,20 @@ class JsonWriteStream
|
|
189
260
|
def write_array
|
190
261
|
write_comma
|
191
262
|
increment
|
263
|
+
indent
|
192
264
|
end
|
193
265
|
|
194
266
|
# prep work
|
195
267
|
def write_object
|
196
268
|
write_comma
|
197
269
|
increment
|
270
|
+
indent
|
198
271
|
end
|
199
272
|
|
200
273
|
def close
|
201
|
-
|
274
|
+
write_newline
|
275
|
+
indent(indent_level - 1)
|
276
|
+
stream.write("]")
|
202
277
|
end
|
203
278
|
|
204
279
|
def is_object?
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
class JsonWriteStream
|
4
4
|
class YieldingWriter
|
5
|
-
attr_reader :stream, :index
|
5
|
+
attr_reader :stream, :index, :options
|
6
6
|
|
7
|
-
def initialize(stream)
|
7
|
+
def initialize(stream, options = {})
|
8
8
|
@stream = stream
|
9
9
|
@index = 0
|
10
|
+
@options = options
|
10
11
|
after_initialize
|
11
12
|
end
|
12
13
|
|
@@ -60,33 +61,27 @@ class JsonWriteStream
|
|
60
61
|
stream.write('{')
|
61
62
|
end
|
62
63
|
|
63
|
-
def write_array(key
|
64
|
+
def write_array(key)
|
64
65
|
write_comma
|
65
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
66
66
|
increment
|
67
67
|
write_key(key)
|
68
68
|
stream.write(':')
|
69
|
-
stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
|
70
69
|
super(true)
|
71
70
|
end
|
72
71
|
|
73
|
-
def write_object(key
|
72
|
+
def write_object(key)
|
74
73
|
write_comma
|
75
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
76
74
|
increment
|
77
75
|
write_key(key)
|
78
76
|
stream.write(':')
|
79
|
-
stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
|
80
77
|
super(true)
|
81
78
|
end
|
82
79
|
|
83
|
-
def write_key_value(key, value
|
80
|
+
def write_key_value(key, value)
|
84
81
|
write_comma
|
85
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
86
82
|
increment
|
87
83
|
write_key(key)
|
88
84
|
stream.write(':')
|
89
|
-
stream.write(options.fetch(:between, DEFAULT_OPTIONS[:between]))
|
90
85
|
stream.write(escape(value))
|
91
86
|
end
|
92
87
|
|
@@ -106,9 +101,8 @@ class JsonWriteStream
|
|
106
101
|
stream.write('[')
|
107
102
|
end
|
108
103
|
|
109
|
-
def write_element(element
|
104
|
+
def write_element(element)
|
110
105
|
write_comma
|
111
|
-
stream.write(options.fetch(:before, DEFAULT_OPTIONS[:before]))
|
112
106
|
increment
|
113
107
|
stream.write(escape(element))
|
114
108
|
end
|
@@ -25,7 +25,7 @@ describe JsonWriteStream do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'supports specifying a different encoding' do
|
28
|
-
stream_writer.from_stream(stream, Encoding::UTF_16BE) do |writer|
|
28
|
+
stream_writer.from_stream(stream, encoding: Encoding::UTF_16BE) do |writer|
|
29
29
|
writer.write_object do |obj_writer|
|
30
30
|
obj_writer.write_key_value('foo', 'bar')
|
31
31
|
end
|
@@ -38,7 +38,7 @@ describe JsonWriteStream do
|
|
38
38
|
|
39
39
|
describe '#open' do
|
40
40
|
it 'opens a file and yields a yielding stream if given a block' do
|
41
|
-
|
41
|
+
expect(File).to receive(:open).with(tempfile, 'w').and_return(tempfile)
|
42
42
|
stream_writer.open(tempfile) do |writer|
|
43
43
|
expect(writer).to be_a(yielding_writer)
|
44
44
|
expect(writer.stream.path).to eq(tempfile.path)
|
@@ -46,14 +46,14 @@ describe JsonWriteStream do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'opens a file and returns a stateful writer if not given a block' do
|
49
|
-
|
49
|
+
expect(File).to receive(:open).with(tempfile, 'w').and_return(tempfile)
|
50
50
|
writer = stream_writer.open(tempfile)
|
51
51
|
expect(writer).to be_a(stateful_writer)
|
52
52
|
expect(writer.stream.path).to eq(tempfile.path)
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'supports specifying a different encoding' do
|
56
|
-
stream_writer.open(tempfile, Encoding::UTF_16BE) do |writer|
|
56
|
+
stream_writer.open(tempfile, encoding: Encoding::UTF_16BE) do |writer|
|
57
57
|
writer.write_object do |obj_writer|
|
58
58
|
obj_writer.write_key_value('foo', 'bar')
|
59
59
|
end
|
data/spec/shared_examples.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
shared_examples 'a json stream' do
|
3
|
+
shared_examples 'a json stream' do |options = {}|
|
4
4
|
it 'handles a simple array' do
|
5
|
-
check_roundtrip(['abc'])
|
5
|
+
check_roundtrip(['abc'], options)
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'handles a simple object' do
|
9
|
-
check_roundtrip({ 'foo' => 'bar' })
|
9
|
+
check_roundtrip({ 'foo' => 'bar' }, options)
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'handles one level of array nesting' do
|
13
|
-
check_roundtrip([['def'],'abc'])
|
14
|
-
check_roundtrip(['abc',['def']])
|
13
|
+
check_roundtrip([['def'],'abc'], options)
|
14
|
+
check_roundtrip(['abc',['def']], options)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'handles one level of object nesting' do
|
18
|
-
check_roundtrip({ 'foo' => { 'bar' => 'baz' } })
|
18
|
+
check_roundtrip({ 'foo' => { 'bar' => 'baz' } }, options)
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'handles one level of mixed nesting' do
|
22
|
-
check_roundtrip({ 'foo' => ['bar', 'baz'] })
|
23
|
-
check_roundtrip([{ 'foo' => 'bar' }])
|
22
|
+
check_roundtrip({ 'foo' => ['bar', 'baz'] }, options)
|
23
|
+
check_roundtrip([{ 'foo' => 'bar' }], options)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'handles multiple levels of mixed nesting' do
|
27
|
-
check_roundtrip({'foo' => ['bar', { 'baz' => 'moo', 'gaz' => ['doo'] }, 'kal'], 'jim' => ['jill', ['john']] })
|
28
|
-
check_roundtrip(['foo', { 'bar' => 'baz', 'moo' => ['gaz', ['jim', ['jill']], 'jam'] }])
|
27
|
+
check_roundtrip({'foo' => ['bar', { 'baz' => 'moo', 'gaz' => ['doo'] }, 'kal'], 'jim' => ['jill', ['john']] }, options)
|
28
|
+
check_roundtrip(['foo', { 'bar' => 'baz', 'moo' => ['gaz', ['jim', ['jill']], 'jam'] }], options)
|
29
29
|
end
|
30
30
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,19 +3,18 @@
|
|
3
3
|
require 'rspec'
|
4
4
|
require 'json-write-stream'
|
5
5
|
require 'shared_examples'
|
6
|
-
require 'pry-
|
6
|
+
require 'pry-byebug'
|
7
7
|
|
8
8
|
RSpec.configure do |config|
|
9
|
-
config.mock_with :rr
|
10
9
|
end
|
11
10
|
|
12
11
|
class RoundtripChecker
|
13
12
|
class << self
|
14
13
|
include RSpec::Matchers
|
15
14
|
|
16
|
-
def check_roundtrip(obj)
|
15
|
+
def check_roundtrip(obj, options = {})
|
17
16
|
stream = StringIO.new
|
18
|
-
writer = create_writer(stream)
|
17
|
+
writer = create_writer(stream, options)
|
19
18
|
serialize(obj, writer)
|
20
19
|
writer.close
|
21
20
|
new_obj = JSON.parse(stream.string)
|
@@ -49,8 +48,8 @@ class YieldingRoundtripChecker < RoundtripChecker
|
|
49
48
|
class << self
|
50
49
|
protected
|
51
50
|
|
52
|
-
def create_writer(stream)
|
53
|
-
JsonWriteStream::YieldingWriter.new(stream)
|
51
|
+
def create_writer(stream, options = {})
|
52
|
+
JsonWriteStream::YieldingWriter.new(stream, options)
|
54
53
|
end
|
55
54
|
|
56
55
|
def serialize(obj, writer)
|
@@ -106,8 +105,8 @@ class StatefulRoundtripChecker < RoundtripChecker
|
|
106
105
|
class << self
|
107
106
|
protected
|
108
107
|
|
109
|
-
def create_writer(stream)
|
110
|
-
JsonWriteStream::StatefulWriter.new(stream)
|
108
|
+
def create_writer(stream, options = {})
|
109
|
+
JsonWriteStream::StatefulWriter.new(stream, options)
|
111
110
|
end
|
112
111
|
|
113
112
|
def serialize(obj, writer)
|
data/spec/stateful_spec.rb
CHANGED
@@ -9,10 +9,11 @@ describe JsonWriteStream::YieldingWriter do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
let(:
|
12
|
+
let(:options) { {} }
|
13
|
+
let(:stream_writer) { JsonWriteStream::StatefulWriter.new(stream, options) }
|
13
14
|
|
14
|
-
def check_roundtrip(obj)
|
15
|
-
StatefulRoundtripChecker.check_roundtrip(obj)
|
15
|
+
def check_roundtrip(obj, options = {})
|
16
|
+
StatefulRoundtripChecker.check_roundtrip(obj, options)
|
16
17
|
end
|
17
18
|
|
18
19
|
def utf8(str)
|
@@ -21,20 +22,105 @@ describe JsonWriteStream::YieldingWriter do
|
|
21
22
|
|
22
23
|
it_behaves_like 'a json stream'
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
stream_writer.write_key_value('foo', 'bar', before: "\n ")
|
27
|
-
stream_writer.close
|
25
|
+
context 'with the pretty option' do
|
26
|
+
let(:options) { { pretty: true } }
|
28
27
|
|
29
|
-
|
30
|
-
end
|
28
|
+
it_behaves_like 'a json stream', pretty: true
|
31
29
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
it 'prettifies a basic array' do
|
31
|
+
stream_writer.write_array
|
32
|
+
stream_writer.write_element('foo')
|
33
|
+
stream_writer.close
|
34
|
+
expect(stream.string).to eq(<<~END.strip)
|
35
|
+
[
|
36
|
+
"foo"
|
37
|
+
]
|
38
|
+
END
|
39
|
+
end
|
36
40
|
|
37
|
-
|
41
|
+
it 'prettifies a basic object' do
|
42
|
+
stream_writer.write_object
|
43
|
+
stream_writer.write_key_value('foo', 'bar')
|
44
|
+
stream_writer.close
|
45
|
+
expect(stream.string).to eq(<<~END.strip)
|
46
|
+
{
|
47
|
+
"foo": "bar"
|
48
|
+
}
|
49
|
+
END
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'prettifies a complex structure' do
|
53
|
+
stream_writer.write_object
|
54
|
+
stream_writer.write_array('foo')
|
55
|
+
stream_writer.write_element('bar')
|
56
|
+
stream_writer.write_object
|
57
|
+
stream_writer.write_key_value('baz', 'moo')
|
58
|
+
stream_writer.write_array('gaz')
|
59
|
+
stream_writer.write_element('doo')
|
60
|
+
stream_writer.close_array
|
61
|
+
stream_writer.close_object
|
62
|
+
stream_writer.write_element('kal')
|
63
|
+
stream_writer.close_array
|
64
|
+
stream_writer.write_array('jim')
|
65
|
+
stream_writer.write_element('jill')
|
66
|
+
stream_writer.write_array
|
67
|
+
stream_writer.write_element('john')
|
68
|
+
stream_writer.close
|
69
|
+
expect(stream.string).to eq(<<~END.strip)
|
70
|
+
{
|
71
|
+
"foo": [
|
72
|
+
"bar",
|
73
|
+
{
|
74
|
+
"baz": "moo",
|
75
|
+
"gaz": [
|
76
|
+
"doo"
|
77
|
+
]
|
78
|
+
},
|
79
|
+
"kal"
|
80
|
+
],
|
81
|
+
"jim": [
|
82
|
+
"jill",
|
83
|
+
[
|
84
|
+
"john"
|
85
|
+
]
|
86
|
+
]
|
87
|
+
}
|
88
|
+
END
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'and the indent_size option' do
|
92
|
+
let(:options) { super().merge(indent_size: 4) }
|
93
|
+
|
94
|
+
it 'indents a basic object correctly' do
|
95
|
+
stream_writer.write_object
|
96
|
+
stream_writer.write_key_value('foo', 'bar')
|
97
|
+
stream_writer.close
|
98
|
+
expect(stream.string).to eq(<<~END.strip)
|
99
|
+
{
|
100
|
+
"foo": "bar"
|
101
|
+
}
|
102
|
+
END
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'indents a more complicated object correctly' do
|
106
|
+
stream_writer.write_object
|
107
|
+
stream_writer.write_array('foo')
|
108
|
+
stream_writer.write_element('bar')
|
109
|
+
stream_writer.write_object
|
110
|
+
stream_writer.write_key_value('baz', 'moo')
|
111
|
+
stream_writer.close
|
112
|
+
expect(stream.string).to eq(<<~END.strip)
|
113
|
+
{
|
114
|
+
"foo": [
|
115
|
+
"bar",
|
116
|
+
{
|
117
|
+
"baz": "moo"
|
118
|
+
}
|
119
|
+
]
|
120
|
+
}
|
121
|
+
END
|
122
|
+
end
|
123
|
+
end
|
38
124
|
end
|
39
125
|
|
40
126
|
describe '#close' do
|
data/spec/yielding_spec.rb
CHANGED
@@ -11,8 +11,8 @@ describe JsonWriteStream::YieldingWriter do
|
|
11
11
|
|
12
12
|
let(:stream_writer) { JsonWriteStream::YieldingWriter.new(stream) }
|
13
13
|
|
14
|
-
def check_roundtrip(obj)
|
15
|
-
YieldingRoundtripChecker.check_roundtrip(obj)
|
14
|
+
def check_roundtrip(obj, options = {})
|
15
|
+
YieldingRoundtripChecker.check_roundtrip(obj, options)
|
16
16
|
end
|
17
17
|
|
18
18
|
def utf8(str)
|
@@ -20,22 +20,7 @@ describe JsonWriteStream::YieldingWriter do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it_behaves_like 'a json stream'
|
23
|
-
|
24
|
-
it 'respects the "before" option' do
|
25
|
-
stream_writer.write_object do |object_writer|
|
26
|
-
object_writer.write_key_value('foo', 'bar', before: "\n ")
|
27
|
-
end
|
28
|
-
|
29
|
-
expect(stream.string).to eq("{\n \"foo\":\"bar\"}")
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'respects the "between" option' do
|
33
|
-
stream_writer.write_object do |object_writer|
|
34
|
-
object_writer.write_key_value('foo', 'bar', between: ' ')
|
35
|
-
end
|
36
|
-
|
37
|
-
expect(stream.string).to eq('{"foo": "bar"}')
|
38
|
-
end
|
23
|
+
it_behaves_like 'a json stream', pretty: true
|
39
24
|
|
40
25
|
describe '#write_key_value' do
|
41
26
|
it 'converts all keys to strings' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-write-stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_pure
|
@@ -32,7 +32,6 @@ extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
34
|
- Gemfile
|
35
|
-
- History.txt
|
36
35
|
- README.md
|
37
36
|
- Rakefile
|
38
37
|
- json-write-stream.gemspec
|
@@ -64,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
63
|
version: '0'
|
65
64
|
requirements: []
|
66
65
|
rubyforge_project:
|
67
|
-
rubygems_version: 2.
|
66
|
+
rubygems_version: 2.7.6
|
68
67
|
signing_key:
|
69
68
|
specification_version: 4
|
70
69
|
summary: An easy, streaming way to generate JSON.
|
data/History.txt
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
== 1.0.0
|
2
|
-
|
3
|
-
* Birthday!
|
4
|
-
|
5
|
-
== 1.0.1
|
6
|
-
|
7
|
-
* Support custom encodings (UTF-8, UTF-16, etc).
|
8
|
-
|
9
|
-
== 1.0.2
|
10
|
-
|
11
|
-
* Add flush methods
|
12
|
-
|
13
|
-
== 1.1.0
|
14
|
-
|
15
|
-
* Add way of inserting text after commas.
|
16
|
-
|
17
|
-
== 1.2.0
|
18
|
-
|
19
|
-
* Add way of inserting text between key/value pairs.
|