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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a397871c57d6b78327b37cac6e9848b212b4a2d6
4
- data.tar.gz: 2a0437147cacd67a906a1c1d34b266001ae1f2fc
3
+ metadata.gz: baa06ccdc85e122396611df9322e6d8343997cfe
4
+ data.tar.gz: 27682e70af99b13fa8d8533865bf4aa37f1fe0d8
5
5
  SHA512:
6
- metadata.gz: 6af47c318296dd8bfc6bc981fcb420e7ba9b42905cd08ac459668dae13c588cc67bc1159e00f98c06d740304d6c1c6b3c27a36b50457ebef9951254430665e20
7
- data.tar.gz: 619674607994da2fe54e97608869ae70ebf81a5f9e9100f0e2771c7dddae56af537f262f4a997221a31d28c7609f1523351b9d7a50d5e6a978954ed562b8c4b4
6
+ metadata.gz: 131c75bb3ab428521cb9185e4f6ffb209d5a6385185f043fb145362233c7a5d19419054323de4c7536d4af499c299b94a310d017890a2fd9228ebb648fbb8ab6
7
+ data.tar.gz: b4c58f19084da6b9cb02749adc6356eb14ef9a946ac9a4edfcf79330daa6e17898c7d10cc83d5b16d3fc4680fcc40b96adbee02325fee68e7307f863495319d3
@@ -1,3 +1,7 @@
1
1
  == 1.0.0
2
2
 
3
3
  * Birthday!
4
+
5
+ == 1.0.1
6
+
7
+ * Support custom encodings (UTF-8, UTF-16, etc).
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  class JsonWriteStream
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
@@ -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.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-17 00:00:00.000000000 Z
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_pure