jsonify 0.1.1 → 0.1.2
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 +27 -28
- data/lib/jsonify/builder.rb +23 -3
- data/lib/jsonify/version.rb +1 -1
- data/spec/builder_spec.rb +28 -1
- metadata +20 -20
data/README.md
CHANGED
@@ -33,9 +33,9 @@ But an even greater motivation for me was emulating the simplicity of [Builder](
|
|
33
33
|
|
34
34
|
In the examples that follow, the JSON output is usually shown "prettified". Is this only
|
35
35
|
for illustration purposes, as the default behavior for Jsonify is not to prettify the output.
|
36
|
-
You can enable prettification by passing `:
|
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
|
-
want to show this format).
|
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
40
|
### Standalone
|
41
41
|
# Create some objects that represent a person and associated hyperlinks
|
@@ -47,16 +47,17 @@ want to show this format).
|
|
47
47
|
|
48
48
|
# Build this information as JSON
|
49
49
|
require 'jsonify'
|
50
|
-
json = Jsonify::Builder.new(:
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
50
|
+
json = Jsonify::Builder.new(:format => :pretty)
|
51
|
+
|
52
|
+
# Representation of the person
|
53
|
+
json.alumnus do
|
54
|
+
json.fname @person.first_name
|
55
|
+
json.lname @person.last_name
|
56
|
+
end
|
57
|
+
|
58
|
+
# Relevant links
|
59
|
+
json.links(@links) do |link|
|
60
|
+
{:rel => link.first, :href => link.last}
|
60
61
|
end
|
61
62
|
|
62
63
|
# Evaluate the result to a string
|
@@ -65,22 +66,20 @@ want to show this format).
|
|
65
66
|
Results in ...
|
66
67
|
|
67
68
|
{
|
68
|
-
"
|
69
|
-
"
|
70
|
-
|
71
|
-
|
69
|
+
"alumnus": {
|
70
|
+
"fname": "George",
|
71
|
+
"lname": "Burdell"
|
72
|
+
},
|
73
|
+
"links": [
|
74
|
+
{
|
75
|
+
"rel": "self",
|
76
|
+
"href": "http://example.com/people/123"
|
72
77
|
},
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
{
|
79
|
-
"rel": "school",
|
80
|
-
"href": "http://gatech.edu"
|
81
|
-
}
|
82
|
-
]
|
83
|
-
}
|
78
|
+
{
|
79
|
+
"rel": "school",
|
80
|
+
"href": "http://gatech.edu"
|
81
|
+
}
|
82
|
+
]
|
84
83
|
}
|
85
84
|
|
86
85
|
### View Templates
|
@@ -272,7 +271,7 @@ or symbol (for `tag!`).
|
|
272
271
|
|
273
272
|
So this construct is really doing two things -- creating a JSON pair, and creating a JSON array as the value of the pair.
|
274
273
|
|
275
|
-
json = Jsonify::Builder.new(:
|
274
|
+
json = Jsonify::Builder.new(:format => :pretty)
|
276
275
|
json.letters('a'..'c') do |letter|
|
277
276
|
letter.upcase
|
278
277
|
end
|
data/lib/jsonify/builder.rb
CHANGED
@@ -1,14 +1,34 @@
|
|
1
1
|
module Jsonify
|
2
2
|
class Builder < BlankSlate
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def compile( options={} )
|
7
|
+
builder = self.new options
|
8
|
+
yield builder
|
9
|
+
builder.compile!
|
10
|
+
end
|
11
|
+
|
12
|
+
def pretty(&block)
|
13
|
+
compile( :format => :pretty, &block )
|
14
|
+
end
|
15
|
+
|
16
|
+
def plain(&block)
|
17
|
+
compile( :format => :plain, &block )
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
3
21
|
|
4
22
|
# Initializes a new builder. The Jsonify::Builder works by keeping a stack of +JsonValue+s.
|
5
23
|
#
|
6
24
|
# @param [Hash] options the options to create with
|
7
25
|
# @option options [boolean] :verify Builder will verify that the compiled JSON string is parseable; this option does incur a performance penalty and generally should only be used in development
|
8
|
-
# @option options [
|
26
|
+
# @option options [symbol] :format Format for the resultant JSON string;
|
27
|
+
# `:pretty`, the JSON string will be output in a prettier format with new lines and indentation; this option does incur a performance penalty and generally should only be used in development
|
28
|
+
# `:plain`, no formatting (compact one-line JSON -- best for production)
|
9
29
|
def initialize(options={})
|
10
30
|
@verify = options[:verify].nil? ? false : options[:verify]
|
11
|
-
@pretty = options[:
|
31
|
+
@pretty = options[:format].to_s == 'pretty' ? true : false
|
12
32
|
reset!
|
13
33
|
end
|
14
34
|
|
@@ -29,7 +49,7 @@ module Jsonify
|
|
29
49
|
|
30
50
|
# Compiles the JSON objects into a string representation.
|
31
51
|
# If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +JSON.parse+.
|
32
|
-
# If initialized with +:
|
52
|
+
# If initialized with +:format => :pretty+, the compiled result will be parsed and regenerated via +JSON.pretty_generate+.
|
33
53
|
# This method can be called without any side effects. You can call +compile!+ at any time, and multiple times if desired.
|
34
54
|
#
|
35
55
|
# @raise [TypeError] only if +:verify+ is set to true
|
data/lib/jsonify/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -3,6 +3,33 @@ require 'spec_helper'
|
|
3
3
|
describe Jsonify::Builder do
|
4
4
|
|
5
5
|
let(:json) { Jsonify::Builder.new }
|
6
|
+
|
7
|
+
describe 'class methods' do
|
8
|
+
it '#compile should compile' do
|
9
|
+
Jsonify::Builder.compile do |j|
|
10
|
+
j.foo 'bar'
|
11
|
+
end.should == '{"foo":"bar"}'
|
12
|
+
end
|
13
|
+
it '#pretty should be pretty' do
|
14
|
+
pretty_results = <<PRETTY_JSON
|
15
|
+
{
|
16
|
+
"foo": {
|
17
|
+
"bar": "baz"
|
18
|
+
}
|
19
|
+
}
|
20
|
+
PRETTY_JSON
|
21
|
+
Jsonify::Builder.pretty do |j|
|
22
|
+
j.foo do
|
23
|
+
j.bar 'baz'
|
24
|
+
end
|
25
|
+
end.should == pretty_results.chomp
|
26
|
+
end
|
27
|
+
it '#plain should be plain' do
|
28
|
+
Jsonify::Builder.plain do |j|
|
29
|
+
j.foo 'bar'
|
30
|
+
end.should == '{"foo":"bar"}'
|
31
|
+
end
|
32
|
+
end
|
6
33
|
|
7
34
|
describe 'base behavior' do
|
8
35
|
describe 'should render empty object on literal' do
|
@@ -49,7 +76,7 @@ describe Jsonify::Builder do
|
|
49
76
|
json.compile!.should == non_pretty_results
|
50
77
|
end
|
51
78
|
it "should be pretty when asked for" do
|
52
|
-
json = Jsonify::Builder.new(:
|
79
|
+
json = Jsonify::Builder.new(:format => :pretty)
|
53
80
|
json.foo do
|
54
81
|
json.bar 'baz'
|
55
82
|
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.1.
|
4
|
+
version: 0.1.2
|
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-08-
|
12
|
+
date: 2011-08-09 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70122619875620 !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: *70122619875620
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: tilt
|
27
|
-
requirement: &
|
27
|
+
requirement: &70122619874940 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70122619874940
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70122619874400 !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: *70122619874400
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70122619873760 !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: *70122619873760
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: autotest
|
60
|
-
requirement: &
|
60
|
+
requirement: &70122619872980 !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: *70122619872980
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: yard
|
71
|
-
requirement: &
|
71
|
+
requirement: &70122619872260 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70122619872260
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rdiscount
|
82
|
-
requirement: &
|
82
|
+
requirement: &70122619871560 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70122619871560
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: ruby-prof
|
93
|
-
requirement: &
|
93
|
+
requirement: &70122619871100 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70122619871100
|
102
102
|
description: Turn Ruby objects into JSON -- correctly!
|
103
103
|
email:
|
104
104
|
- bsiggelkow@me.com
|
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
segments:
|
146
146
|
- 0
|
147
|
-
hash:
|
147
|
+
hash: -3850211948781890722
|
148
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
153
|
version: '0'
|
154
154
|
segments:
|
155
155
|
- 0
|
156
|
-
hash:
|
156
|
+
hash: -3850211948781890722
|
157
157
|
requirements: []
|
158
158
|
rubyforge_project: jsonify
|
159
159
|
rubygems_version: 1.8.6
|