json-write-stream 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +4 -0
- data/lib/json-write-stream.rb +7 -2
- data/lib/json-write-stream/version.rb +1 -1
- data/spec/json-write-stream_spec.rb +25 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: baa06ccdc85e122396611df9322e6d8343997cfe
|
4
|
+
data.tar.gz: 27682e70af99b13fa8d8533865bf4aa37f1fe0d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 131c75bb3ab428521cb9185e4f6ffb209d5a6385185f043fb145362233c7a5d19419054323de4c7536d4af499c299b94a310d017890a2fd9228ebb648fbb8ab6
|
7
|
+
data.tar.gz: b4c58f19084da6b9cb02749adc6356eb14ef9a946ac9a4edfcf79330daa6e17898c7d10cc83d5b16d3fc4680fcc40b96adbee02325fee68e7307f863495319d3
|
data/History.txt
CHANGED
data/lib/json-write-stream.rb
CHANGED
@@ -5,8 +5,12 @@ require 'json-write-stream/yielding'
|
|
5
5
|
require 'json-write-stream/stateful'
|
6
6
|
|
7
7
|
class JsonWriteStream
|
8
|
+
DEFAULT_ENCODING = Encoding::UTF_8
|
9
|
+
|
8
10
|
class << self
|
9
|
-
def from_stream(stream)
|
11
|
+
def from_stream(stream, encoding = DEFAULT_ENCODING)
|
12
|
+
stream.set_encoding(encoding)
|
13
|
+
|
10
14
|
if block_given?
|
11
15
|
yield writer = YieldingWriter.new(stream)
|
12
16
|
writer.close
|
@@ -15,8 +19,9 @@ class JsonWriteStream
|
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
|
-
def open(file)
|
22
|
+
def open(file, encoding = DEFAULT_ENCODING)
|
19
23
|
handle = File.open(file, 'w')
|
24
|
+
handle.set_encoding(encoding)
|
20
25
|
|
21
26
|
if block_given?
|
22
27
|
yield writer = YieldingWriter.new(handle)
|
@@ -23,6 +23,17 @@ describe JsonWriteStream do
|
|
23
23
|
expect(writer).to be_a(stateful_writer)
|
24
24
|
expect(writer.stream).to equal(stream)
|
25
25
|
end
|
26
|
+
|
27
|
+
it 'supports specifying a different encoding' do
|
28
|
+
stream_writer.from_stream(stream, Encoding::UTF_16BE) do |writer|
|
29
|
+
writer.write_object do |obj_writer|
|
30
|
+
obj_writer.write_key_value('foo', 'bar')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
expect(stream.string.bytes).to_not eq('{"foo":"bar"}'.bytes)
|
35
|
+
expect(stream.string.encode(Encoding::UTF_8).bytes).to eq('{"foo":"bar"}'.bytes)
|
36
|
+
end
|
26
37
|
end
|
27
38
|
|
28
39
|
describe '#open' do
|
@@ -40,5 +51,19 @@ describe JsonWriteStream do
|
|
40
51
|
expect(writer).to be_a(stateful_writer)
|
41
52
|
expect(writer.stream.path).to eq(tempfile.path)
|
42
53
|
end
|
54
|
+
|
55
|
+
it 'supports specifying a different encoding' do
|
56
|
+
stream_writer.open(tempfile, Encoding::UTF_16BE) do |writer|
|
57
|
+
writer.write_object do |obj_writer|
|
58
|
+
obj_writer.write_key_value('foo', 'bar')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
written = tempfile.read
|
63
|
+
written.force_encoding(Encoding::UTF_16BE)
|
64
|
+
|
65
|
+
expect(written.bytes).to_not eq('{"foo":"bar"}'.bytes)
|
66
|
+
expect(written.encode(Encoding::UTF_8).bytes).to eq('{"foo":"bar"}'.bytes)
|
67
|
+
end
|
43
68
|
end
|
44
69
|
end
|
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: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_pure
|