RFC7159 7159.04 → 7159.05
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/RFC7159.rb +12 -0
- data/lib/RFC7159/array.rb +4 -2
- data/lib/RFC7159/dumper.rb +23 -16
- data/lib/RFC7159/object.rb +6 -9
- data/lib/RFC7159/string.rb +1 -1
- data/lib/RFC7159/version.rb +1 -1
- 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: b662d283a8723554682d016119ce8459a88a1acc
|
4
|
+
data.tar.gz: eda2d8e84dd1824e1eb698f1a428ebc00924af1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfb49eb59408e63948ad0c574de89b0339dbcb26ccf983a01c67f23f6feb7b5c7aad238d27a18df1058ca0bfb41164c7063ad60e6b639989dafd0c5933fcc5b3
|
7
|
+
data.tar.gz: 3e0df175d36f92735ed1d1dd70b38ad385888121fbc01742e54ec7eb3270328852afcaca39d58535c984a2c6d5e6fe4c25d0d8c2c2334899bc1152cad4a715ab
|
data/lib/RFC7159.rb
CHANGED
@@ -68,6 +68,18 @@ module RFC7159
|
|
68
68
|
bag.start_dump obj
|
69
69
|
return port
|
70
70
|
end
|
71
|
+
|
72
|
+
# (Experimental) Sometimes JSON is required somewhere inside of a
|
73
|
+
# pretty-print, and you might want to inherit the indentation(s) of
|
74
|
+
# surrounding PP structure. In order to do so here we propose a dump
|
75
|
+
# variant that takes a PP object rather than destination IO.
|
76
|
+
# @param [::Object] obj The input (should be JSONable)
|
77
|
+
# @param [PP] pp The PP.
|
78
|
+
def self.pp_object pp, obj, indent=4
|
79
|
+
bag = RFC7159::Dumper.new nil, indent, nil, pp
|
80
|
+
bag.start_dump obj
|
81
|
+
return nil
|
82
|
+
end
|
71
83
|
end
|
72
84
|
|
73
85
|
#
|
data/lib/RFC7159/array.rb
CHANGED
@@ -108,8 +108,10 @@ class RFC7159::Array < RFC7159::Value
|
|
108
108
|
def pretty_print pp
|
109
109
|
hdr = sprintf '#<%p:%#016x', self.class, self.object_id << 1
|
110
110
|
pp.group 1, hdr, '>' do
|
111
|
-
pp.
|
112
|
-
@array.
|
111
|
+
pp.text ' '
|
112
|
+
RFC7159::Dumper.kandr pp, 1, @array.each, '[', ']' do |i|
|
113
|
+
i.pretty_print pp
|
114
|
+
end
|
113
115
|
end
|
114
116
|
end
|
115
117
|
|
data/lib/RFC7159/dumper.rb
CHANGED
@@ -37,12 +37,30 @@ require 'prettyprint'
|
|
37
37
|
# Dumps ruby object into JSON string
|
38
38
|
class RFC7159::Dumper
|
39
39
|
|
40
|
+
# much like PP#object_group, except that it indents like K&R.
|
41
|
+
def self.kandr pp, indent, enum, open, close
|
42
|
+
pp.text open
|
43
|
+
pp.group_sub do
|
44
|
+
pp.nest indent do
|
45
|
+
enum.with_index do |a, i|
|
46
|
+
if i > 0
|
47
|
+
pp.text ','
|
48
|
+
end
|
49
|
+
pp.breakable ' '
|
50
|
+
yield a
|
51
|
+
end
|
52
|
+
end
|
53
|
+
pp.breakable ' '
|
54
|
+
end
|
55
|
+
pp.text close
|
56
|
+
end
|
57
|
+
|
40
58
|
# @param [#<<] port output destination
|
41
|
-
def initialize port, indent = 4, width = 79
|
59
|
+
def initialize port, indent = 4, width = 79, pp = PrettyPrint.new(port, width)
|
42
60
|
@port = port
|
43
61
|
@bag = Hash.new
|
44
62
|
@indent = indent
|
45
|
-
@pp =
|
63
|
+
@pp = pp
|
46
64
|
@bag.compare_by_identity
|
47
65
|
end
|
48
66
|
|
@@ -125,21 +143,10 @@ class RFC7159::Dumper
|
|
125
143
|
# much like PP#object_group, except that it indents like K&R.
|
126
144
|
def kandr obj, method, open, close
|
127
145
|
ensure_unique obj do
|
128
|
-
enum = obj.
|
129
|
-
@pp
|
130
|
-
|
131
|
-
@pp.nest @indent do
|
132
|
-
enum.with_index do |a, i|
|
133
|
-
if i > 0
|
134
|
-
@pp.text ','
|
135
|
-
end
|
136
|
-
@pp.breakable ' '
|
137
|
-
yield a
|
138
|
-
end
|
139
|
-
end
|
140
|
-
@pp.breakable ' '
|
146
|
+
enum = obj.enum_for method
|
147
|
+
RFC7159::Dumper.kandr @pp, @indent, enum, open, close do |obj|
|
148
|
+
yield obj
|
141
149
|
end
|
142
|
-
@pp.text close
|
143
150
|
end
|
144
151
|
end
|
145
152
|
|
data/lib/RFC7159/object.rb
CHANGED
@@ -62,7 +62,7 @@ class RFC7159::Object < RFC7159::Value
|
|
62
62
|
|
63
63
|
# iterates over the pairs.
|
64
64
|
# @yield [key, value] the pair.
|
65
|
-
def each_pair
|
65
|
+
def each_pair &b
|
66
66
|
e = Enumerator.new do |y|
|
67
67
|
@assoc.each do |a|
|
68
68
|
y << a
|
@@ -106,14 +106,11 @@ class RFC7159::Object < RFC7159::Value
|
|
106
106
|
def pretty_print pp
|
107
107
|
hdr = sprintf '#<%p:%#016x', self.class, self.object_id << 1
|
108
108
|
pp.group 1, hdr, '>' do
|
109
|
-
pp.
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
pp.text ': '
|
115
|
-
v.pretty_print pp
|
116
|
-
end
|
109
|
+
pp.text ' '
|
110
|
+
RFC7159::Dumper.kandr pp, 1, @assoc.each, '{', '}' do |(i, j)|
|
111
|
+
i.pretty_print pp
|
112
|
+
pp.text ': '
|
113
|
+
j.pretty_print pp
|
117
114
|
end
|
118
115
|
end
|
119
116
|
end
|
data/lib/RFC7159/string.rb
CHANGED
data/lib/RFC7159/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: RFC7159
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '7159.
|
4
|
+
version: '7159.05'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Urabe,
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|