outstream-json 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -5
- data/lib/outstream/json.rb +8 -2
- data/test/test_json.rb +10 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb23d15ac017bb74a60f4d8b5d939ca4ea6a5220
|
4
|
+
data.tar.gz: 850a1a01ec27e3f663a106ffc420ed858bdeda03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ef5dc77093aa2024ac51a95da3729e5d3acc74bdb88585e633e5aeb9cf1d64fc9dc7fc64308e89adb4b0fdfe0f79729eeda92520d0baa2b178f6f508f69ee59
|
7
|
+
data.tar.gz: e482b9774860e128bc00aa10c397a5303013661378682cd837720ec7f52df1e82b4f2e342842a6b203323366ed012ded2b0d011da44de1f5570f45f4a229f600
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ The Json.create method defines a JSON object
|
|
25
25
|
out = Outstream::Json.create do
|
26
26
|
...
|
27
27
|
end
|
28
|
-
# use out.to_s or out.each to
|
28
|
+
# use out.to_s or out.each to produce result
|
29
29
|
|
30
30
|
Use with basic ruby types
|
31
31
|
|
@@ -35,17 +35,22 @@ Use with basic ruby types
|
|
35
35
|
end
|
36
36
|
# {"string":"hello","number":42,"array":[1,2,3]}
|
37
37
|
|
38
|
+
Use with hashes or blocks to create nested objects
|
39
|
+
|
38
40
|
Outstream::Json.create do
|
39
|
-
add "
|
40
|
-
|
41
|
+
add "block_object" do
|
42
|
+
add "foo" => "bar"
|
43
|
+
end
|
44
|
+
add "hash_object" => {
|
45
|
+
"wow" => "cool"
|
41
46
|
}
|
42
47
|
end
|
43
|
-
# {"
|
48
|
+
# {"block_object":{"foo":"bar"},"hash_object":{"wow":"cool"}}
|
44
49
|
|
45
50
|
Use with transformed SQL result set
|
46
51
|
|
47
52
|
client = Mysql2::Client.new
|
48
|
-
|
53
|
+
results = client.query("SELECT * FROM hugetable", stream: true)
|
49
54
|
Outstream::Json.create do
|
50
55
|
add results: results.lazy.map {|row| transform_row(row)}
|
51
56
|
end
|
data/lib/outstream/json.rb
CHANGED
@@ -23,13 +23,19 @@ module Outstream
|
|
23
23
|
# and string values as quoted strings. If called without a block, returns an enumerator.
|
24
24
|
#
|
25
25
|
# Example:
|
26
|
-
# json.each {|token| puts token}
|
26
|
+
# json.each {|token| puts token} => nil
|
27
|
+
# json.each => an_enumerator
|
27
28
|
def each(&out_block)
|
28
29
|
e = Enumerator.new {|yielder|
|
29
30
|
Collector.new(yielder).collect &@body_block
|
30
31
|
}
|
31
32
|
|
32
|
-
|
33
|
+
if out_block
|
34
|
+
e.each(&out_block)
|
35
|
+
nil
|
36
|
+
else
|
37
|
+
e
|
38
|
+
end
|
33
39
|
end
|
34
40
|
|
35
41
|
# Produce a compact string of the JSON. The entire string is produced at once; this is not
|
data/test/test_json.rb
CHANGED
@@ -31,7 +31,7 @@ class TestJson < Test::Unit::TestCase
|
|
31
31
|
end
|
32
32
|
def testHash
|
33
33
|
out = Outstream::Json.create {
|
34
|
-
add "foo" => {a: 1, b: 2}
|
34
|
+
add "foo" => { a: 1, b: 2 }
|
35
35
|
}
|
36
36
|
assert_equal '{"foo":{"a":1,"b":2}}', out.to_s
|
37
37
|
end
|
@@ -76,6 +76,15 @@ class TestJson < Test::Unit::TestCase
|
|
76
76
|
assert_equal '{"foo":"bar","wow":"cool"}', out.to_s
|
77
77
|
end
|
78
78
|
|
79
|
+
def testEachBlock
|
80
|
+
out = Outstream::Json.create {
|
81
|
+
add "foo" => "bar"
|
82
|
+
}
|
83
|
+
tokens = []
|
84
|
+
assert_equal nil, out.each {|str| tokens << str}
|
85
|
+
assert_equal %w({ "foo" : "bar" }), tokens
|
86
|
+
end
|
87
|
+
|
79
88
|
def testEnumerator
|
80
89
|
x = "fun"
|
81
90
|
out = Outstream::Json.create {
|