jsonify 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +32 -11
- data/lib/jsonify/builder.rb +17 -4
- data/lib/jsonify/version.rb +1 -1
- data/spec/builder_spec.rb +34 -13
- metadata +16 -16
data/README.md
CHANGED
@@ -31,18 +31,38 @@ But an even greater motivation for me was emulating the simplicity of [Builder](
|
|
31
31
|
|
32
32
|
## Usage
|
33
33
|
|
34
|
-
In the examples that follow, the JSON output is usually shown "prettified".
|
34
|
+
In the examples that follow, the JSON output is usually shown "prettified". This is only
|
35
35
|
for illustration purposes, as the default behavior for Jsonify is not to prettify the output.
|
36
36
|
You can enable prettification by passing `:format => :pretty` to the Jsonify::Builder constructor; however,
|
37
37
|
pretty printing is a relatively costly operation and should not be used in production (unless, of course, you explicitly
|
38
38
|
want to show this format). The default format, `plain`, dictates no special formatting: the result will be rendered as a compact string without any newlines.
|
39
39
|
|
40
|
+
### Compatibility Warning
|
41
|
+
|
42
|
+
Starting with version 0.2.0, the handling of arrays has changed to provide a more natural feel. As a consequence, however, code written using earlier versions of Jsonify may not work correctly. The example that follows demonstrates the changes you will need to be make.
|
43
|
+
|
44
|
+
Previously, when arrays were processed, you had to put away the builder-style, and use more conventional Rubyisms.
|
45
|
+
|
46
|
+
json.links(@links) do |link|
|
47
|
+
{:rel => link.type, :href => link.url}
|
48
|
+
end
|
49
|
+
|
50
|
+
This difference was a frequent stumbling block with users and I wanted to remedy it. The interface for handling arrays is now consistent with the builder-style and should be less surprising to developers. The above snippet is now implemented as:
|
51
|
+
|
52
|
+
json.links(@links) do |link|
|
53
|
+
json.rel link.type
|
54
|
+
json.href link.url
|
55
|
+
end
|
56
|
+
|
57
|
+
As always, all feedback is greatly appreciated. I want to know how this new style works out.
|
58
|
+
|
40
59
|
### Standalone
|
41
60
|
# Create some objects that represent a person and associated hyperlinks
|
42
61
|
@person = Struct.new(:first_name,:last_name).new('George','Burdell')
|
62
|
+
Link = Struct.new(:type, :url)
|
43
63
|
@links = [
|
44
|
-
|
45
|
-
|
64
|
+
Link.new('self', 'http://example.com/people/123'),
|
65
|
+
Link.new('school', 'http://gatech.edu')
|
46
66
|
]
|
47
67
|
|
48
68
|
# Build this information as JSON
|
@@ -57,7 +77,8 @@ want to show this format). The default format, `plain`, dictates no special form
|
|
57
77
|
|
58
78
|
# Relevant links
|
59
79
|
json.links(@links) do |link|
|
60
|
-
|
80
|
+
json.rel link.type
|
81
|
+
json.href link.url
|
61
82
|
end
|
62
83
|
|
63
84
|
# Evaluate the result to a string
|
@@ -303,20 +324,20 @@ Jsonify supports JSON array construction through two approaches: `method_missing
|
|
303
324
|
|
304
325
|
##### method_missing
|
305
326
|
|
306
|
-
Pass an array and a block to `method_missing` (or `tag!`), and Jsonify will iterate
|
307
|
-
|
308
|
-
If you pass an array that has a length of 5, you will end up with a JSON array that has 5 items.
|
327
|
+
Pass an array and a block to `method_missing` (or `tag!`), and Jsonify will create a JSON array. It will then iterate over your array and call the block for each item in the array. Within the block, you use the `json` object to add items to the JSON array.
|
328
|
+
|
309
329
|
That JSON array is then set as the value of the name-value pair, where the name comes from the method name (for `method_missing`)
|
310
330
|
or symbol (for `tag!`).
|
311
331
|
|
312
332
|
So this construct is really doing two things -- creating a JSON pair, and creating a JSON array as the value of the pair.
|
313
333
|
|
314
|
-
|
315
|
-
|
316
|
-
|
334
|
+
Jsonify::Builder.pretty do |json|
|
335
|
+
json.letters('a'..'c') do |letter|
|
336
|
+
json << letter.upcase
|
337
|
+
end
|
317
338
|
end
|
318
339
|
|
319
|
-
|
340
|
+
results in ...
|
320
341
|
|
321
342
|
{
|
322
343
|
"letters": [
|
data/lib/jsonify/builder.rb
CHANGED
@@ -116,11 +116,12 @@ module Jsonify
|
|
116
116
|
# If a block is given and an argument is passed, the argument it is assumed to be an
|
117
117
|
# Array (more specifically, an object that responds to `each`).
|
118
118
|
# The argument is iterated over and each item is yielded to the block.
|
119
|
-
# The result of the block becomes an array item of
|
119
|
+
# The result of the block becomes an array item of the JsonArray.
|
120
120
|
#
|
121
121
|
# @example Map an of array of links to an array of JSON objects
|
122
122
|
# json.links(@links) do |link|
|
123
|
-
#
|
123
|
+
# json.rel link.first
|
124
|
+
# json.href link.last
|
124
125
|
# end
|
125
126
|
#
|
126
127
|
# @example compiles to something like ...
|
@@ -153,7 +154,19 @@ module Jsonify
|
|
153
154
|
else
|
154
155
|
__array
|
155
156
|
args.each do |arg|
|
156
|
-
@
|
157
|
+
@level += 1
|
158
|
+
block.call(arg)
|
159
|
+
@level -= 1
|
160
|
+
|
161
|
+
value = @stack.pop
|
162
|
+
|
163
|
+
# If the object created was an array with a single value
|
164
|
+
# assume that just the value should be added
|
165
|
+
if (JsonArray === value && value.values.length <= 1)
|
166
|
+
value = value.values.first
|
167
|
+
end
|
168
|
+
|
169
|
+
@stack[@level].add value
|
157
170
|
end
|
158
171
|
end
|
159
172
|
|
@@ -202,4 +215,4 @@ module Jsonify
|
|
202
215
|
end
|
203
216
|
|
204
217
|
end
|
205
|
-
end
|
218
|
+
end
|
data/lib/jsonify/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -184,21 +184,14 @@ PRETTY_JSON
|
|
184
184
|
|
185
185
|
it 'hash with array by iteration' do
|
186
186
|
ary = [1,2,3]
|
187
|
-
json.foo
|
188
|
-
|
187
|
+
json.foo do
|
188
|
+
ary.each do |n|
|
189
|
+
json << (n * 2)
|
190
|
+
end
|
189
191
|
end
|
190
192
|
json.compile!.should == '{"foo":[2,4,6]}'
|
191
193
|
end
|
192
194
|
|
193
|
-
it 'hash with empty array by iteration' do
|
194
|
-
ary = []
|
195
|
-
json.foo(ary) do |n|
|
196
|
-
n * 2
|
197
|
-
end
|
198
|
-
expected = '{"foo":[]}'
|
199
|
-
JSON.parse(json.compile!).should == JSON.parse(expected)
|
200
|
-
end
|
201
|
-
|
202
195
|
it 'simple array with object' do
|
203
196
|
json << 1
|
204
197
|
json << {:foo => :bar}
|
@@ -261,7 +254,8 @@ PRETTY_JSON
|
|
261
254
|
json.lname 'Burdell'
|
262
255
|
end
|
263
256
|
json.links(links) do |link|
|
264
|
-
|
257
|
+
json.href link.url
|
258
|
+
json.rel link.type
|
265
259
|
end
|
266
260
|
end
|
267
261
|
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
|
@@ -313,4 +307,31 @@ PRETTY_JSON
|
|
313
307
|
end
|
314
308
|
end
|
315
309
|
end
|
316
|
-
|
310
|
+
|
311
|
+
describe 'with new array style' do
|
312
|
+
it 'should work' do
|
313
|
+
results =[
|
314
|
+
{:id => 1, :kids => [{:id => 'a'},{:id => 'b'}]},
|
315
|
+
{:id => 2, :kids => [{:id => 'c'},{:id => 'd'}]},
|
316
|
+
]
|
317
|
+
|
318
|
+
json.results(results) do |result|
|
319
|
+
json.id result[:id]
|
320
|
+
json.children(result[:kids]) do |kid|
|
321
|
+
json.id kid[:id]
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
expected = '{"results":[{"id":1,"children":[{"id":"a"},{"id":"b"}]},{"id":2,"children":[{"id":"c"},{"id":"d"}]}]}'
|
326
|
+
JSON.parse(json.compile!).should == JSON.parse(expected)
|
327
|
+
end
|
328
|
+
it 'simple append' do
|
329
|
+
json.letters('a'..'c') do |letter|
|
330
|
+
json << letter.upcase
|
331
|
+
end
|
332
|
+
expected = '{"letters":["A","B","C"]}'
|
333
|
+
JSON.parse(json.compile!).should == JSON.parse(expected)
|
334
|
+
end
|
335
|
+
|
336
|
+
end
|
337
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-05 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70165351228400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70165351228400
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: tilt
|
27
|
-
requirement: &
|
27
|
+
requirement: &70165351226320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.3.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70165351226320
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70165351224580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70165351224580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70165351223680 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70165351223680
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: yard
|
60
|
-
requirement: &
|
60
|
+
requirement: &70165351220980 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70165351220980
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdiscount
|
71
|
-
requirement: &
|
71
|
+
requirement: &70165351220280 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70165351220280
|
80
80
|
description: Turn Ruby objects into JSON -- correctly!
|
81
81
|
email:
|
82
82
|
- bsiggelkow@me.com
|
@@ -122,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
segments:
|
124
124
|
- 0
|
125
|
-
hash:
|
125
|
+
hash: 50285731348355282
|
126
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
131
|
version: '0'
|
132
132
|
segments:
|
133
133
|
- 0
|
134
|
-
hash:
|
134
|
+
hash: 50285731348355282
|
135
135
|
requirements: []
|
136
136
|
rubyforge_project: jsonify
|
137
137
|
rubygems_version: 1.8.6
|