json_builder 2.0.6 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +122 -0
- data/README.md +221 -59
- data/Rakefile +26 -0
- data/lib/json_builder.rb +1 -1
- data/lib/json_builder/compiler.rb +63 -0
- data/lib/json_builder/elements.rb +17 -0
- data/lib/json_builder/member.rb +24 -0
- data/lib/json_builder/template.rb +40 -14
- data/lib/json_builder/value.rb +30 -0
- data/lib/json_builder/version.rb +1 -1
- data/spec/benchmarks/builder.rb +60 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/users_helper.rb +2 -0
- data/spec/dummy/app/models/user.rb +9 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/users/index.json_builder +3 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +5 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20111127061428_create_users.rb +14 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +191 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/controllers/users_controller_spec.rb +14 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/json_builder_spec.rb +7 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/json_builder.rb +7 -0
- data/spec/unit/json_builder_spec.rb +76 -0
- metadata +158 -93
- data/lib/json_builder/generator.rb +0 -127
- data/test/benchmarks/speed.rb +0 -85
- data/test/fixtures/simple.rb +0 -27
- data/test/test_helper.rb +0 -4
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem "rails", "3.0.10"
|
6
|
+
gem "capybara", ">= 0.4.0"
|
7
|
+
gem "sqlite3"
|
8
|
+
|
9
|
+
gem "rspec-rails", ">= 2.0.0.beta"
|
10
|
+
|
11
|
+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
12
|
+
# gem 'ruby-debug'
|
13
|
+
# gem 'ruby-debug19'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
json_builder (3.0.0)
|
5
|
+
activesupport (>= 2.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
abstract (1.0.0)
|
11
|
+
actionmailer (3.0.10)
|
12
|
+
actionpack (= 3.0.10)
|
13
|
+
mail (~> 2.2.19)
|
14
|
+
actionpack (3.0.10)
|
15
|
+
activemodel (= 3.0.10)
|
16
|
+
activesupport (= 3.0.10)
|
17
|
+
builder (~> 2.1.2)
|
18
|
+
erubis (~> 2.6.6)
|
19
|
+
i18n (~> 0.5.0)
|
20
|
+
rack (~> 1.2.1)
|
21
|
+
rack-mount (~> 0.6.14)
|
22
|
+
rack-test (~> 0.5.7)
|
23
|
+
tzinfo (~> 0.3.23)
|
24
|
+
activemodel (3.0.10)
|
25
|
+
activesupport (= 3.0.10)
|
26
|
+
builder (~> 2.1.2)
|
27
|
+
i18n (~> 0.5.0)
|
28
|
+
activerecord (3.0.10)
|
29
|
+
activemodel (= 3.0.10)
|
30
|
+
activesupport (= 3.0.10)
|
31
|
+
arel (~> 2.0.10)
|
32
|
+
tzinfo (~> 0.3.23)
|
33
|
+
activeresource (3.0.10)
|
34
|
+
activemodel (= 3.0.10)
|
35
|
+
activesupport (= 3.0.10)
|
36
|
+
activesupport (3.0.10)
|
37
|
+
arel (2.0.10)
|
38
|
+
builder (2.1.2)
|
39
|
+
capybara (1.1.1)
|
40
|
+
mime-types (>= 1.16)
|
41
|
+
nokogiri (>= 1.3.3)
|
42
|
+
rack (>= 1.0.0)
|
43
|
+
rack-test (>= 0.5.4)
|
44
|
+
selenium-webdriver (~> 2.0)
|
45
|
+
xpath (~> 0.1.4)
|
46
|
+
childprocess (0.2.2)
|
47
|
+
ffi (~> 1.0.6)
|
48
|
+
diff-lcs (1.1.3)
|
49
|
+
erubis (2.6.6)
|
50
|
+
abstract (>= 1.0.0)
|
51
|
+
ffi (1.0.9)
|
52
|
+
i18n (0.5.0)
|
53
|
+
json (1.6.1)
|
54
|
+
json_pure (1.6.1)
|
55
|
+
mail (2.2.19)
|
56
|
+
activesupport (>= 2.3.6)
|
57
|
+
i18n (>= 0.4.0)
|
58
|
+
mime-types (~> 1.16)
|
59
|
+
treetop (~> 1.4.8)
|
60
|
+
mime-types (1.17.2)
|
61
|
+
nokogiri (1.5.0)
|
62
|
+
polyglot (0.3.3)
|
63
|
+
rack (1.2.4)
|
64
|
+
rack-mount (0.6.14)
|
65
|
+
rack (>= 1.0.0)
|
66
|
+
rack-test (0.5.7)
|
67
|
+
rack (>= 1.0)
|
68
|
+
rails (3.0.10)
|
69
|
+
actionmailer (= 3.0.10)
|
70
|
+
actionpack (= 3.0.10)
|
71
|
+
activerecord (= 3.0.10)
|
72
|
+
activeresource (= 3.0.10)
|
73
|
+
activesupport (= 3.0.10)
|
74
|
+
bundler (~> 1.0)
|
75
|
+
railties (= 3.0.10)
|
76
|
+
railties (3.0.10)
|
77
|
+
actionpack (= 3.0.10)
|
78
|
+
activesupport (= 3.0.10)
|
79
|
+
rake (>= 0.8.7)
|
80
|
+
rdoc (~> 3.4)
|
81
|
+
thor (~> 0.14.4)
|
82
|
+
rake (0.9.2.2)
|
83
|
+
rdoc (3.11)
|
84
|
+
json (~> 1.4)
|
85
|
+
rspec (2.8.0.rc1)
|
86
|
+
rspec-core (= 2.8.0.rc1)
|
87
|
+
rspec-expectations (= 2.8.0.rc1)
|
88
|
+
rspec-mocks (= 2.8.0.rc1)
|
89
|
+
rspec-core (2.8.0.rc1)
|
90
|
+
rspec-expectations (2.8.0.rc1)
|
91
|
+
diff-lcs (~> 1.1.2)
|
92
|
+
rspec-mocks (2.8.0.rc1)
|
93
|
+
rspec-rails (2.8.0.rc1)
|
94
|
+
actionpack (~> 3.0)
|
95
|
+
activesupport (~> 3.0)
|
96
|
+
railties (~> 3.0)
|
97
|
+
rspec (= 2.8.0.rc1)
|
98
|
+
rubyzip (0.9.4)
|
99
|
+
selenium-webdriver (2.10.0)
|
100
|
+
childprocess (>= 0.2.1)
|
101
|
+
ffi (= 1.0.9)
|
102
|
+
json_pure
|
103
|
+
rubyzip
|
104
|
+
sqlite3 (1.3.4)
|
105
|
+
thor (0.14.6)
|
106
|
+
treetop (1.4.10)
|
107
|
+
polyglot
|
108
|
+
polyglot (>= 0.3.1)
|
109
|
+
tzinfo (0.3.31)
|
110
|
+
xpath (0.1.4)
|
111
|
+
nokogiri (~> 1.3)
|
112
|
+
|
113
|
+
PLATFORMS
|
114
|
+
ruby
|
115
|
+
|
116
|
+
DEPENDENCIES
|
117
|
+
capybara (>= 0.4.0)
|
118
|
+
json_builder!
|
119
|
+
rails (= 3.0.10)
|
120
|
+
rspec
|
121
|
+
rspec-rails (>= 2.0.0.beta)
|
122
|
+
sqlite3
|
data/README.md
CHANGED
@@ -1,90 +1,252 @@
|
|
1
1
|
# JSON Builder
|
2
|
-
Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard `to_json` works
|
2
|
+
Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. The standard `to_json` works just fine, but can get very verbose when you need full control of what is generated and performance is a factor. JSON Builder hopes to solve that problem.
|
3
3
|
|
4
|
-
##
|
5
|
-
First, make sure to add the gem to your `Gemfile`.
|
4
|
+
## Sample Usage
|
6
5
|
|
7
|
-
|
6
|
+
```ruby
|
7
|
+
require 'json_builder'
|
8
|
+
|
9
|
+
json = JSONBuilder::Compiler.generate do
|
10
|
+
name "Garrett Bjerkhoel"
|
11
|
+
email "spam@garrettbjerkhoel.com"
|
12
|
+
address do
|
13
|
+
street "1234 1st Ave"
|
14
|
+
street2 "Apt 1"
|
15
|
+
city "New York"
|
16
|
+
state "NY"
|
17
|
+
zip 10065
|
18
|
+
end
|
19
|
+
key :nil, "testing a custom key name"
|
20
|
+
skills do
|
21
|
+
ruby true
|
22
|
+
asp false
|
23
|
+
end
|
24
|
+
longstring do
|
25
|
+
# Could be a highly intensive process that only returns a string
|
26
|
+
"12345" * 25
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Which will generate:
|
32
|
+
|
33
|
+
```json
|
34
|
+
{
|
35
|
+
"name": "Garrett Bjerkhoel",
|
36
|
+
"email": "spam@garrettbjerkhoel.com",
|
37
|
+
"address": {
|
38
|
+
"street": "1234 1st Ave",
|
39
|
+
"street2": "Apt 1",
|
40
|
+
"city": "New York",
|
41
|
+
"state": "NY",
|
42
|
+
"zip": 10065
|
43
|
+
},
|
44
|
+
"nil": "testing a custom key name",
|
45
|
+
"skills": {
|
46
|
+
"ruby": true,
|
47
|
+
"asp": false
|
48
|
+
},
|
49
|
+
"longstring": "1234512345123451234512345..."
|
50
|
+
}
|
51
|
+
```
|
52
|
+
|
53
|
+
If you'd like to just generate an array:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
array ["Garrett Bjerkhoel", "John Doe"] do |name|
|
57
|
+
first, last = name.split(' ')
|
58
|
+
first first
|
59
|
+
last last
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Which will output the following:
|
64
|
+
|
65
|
+
```json
|
66
|
+
[
|
67
|
+
{
|
68
|
+
"first": "Garrett",
|
69
|
+
"last": "Bjerkhoel"
|
70
|
+
},
|
71
|
+
{
|
72
|
+
"first": "John",
|
73
|
+
"last": "Doe"
|
74
|
+
}
|
75
|
+
]
|
76
|
+
```
|
77
|
+
|
78
|
+
Just a note, if you use an array block, all other builder methods will be ignored.
|
8
79
|
|
9
|
-
|
80
|
+
## Using JSON Builder with Rails
|
81
|
+
First, make sure to add the gem to your `Gemfile`.
|
10
82
|
|
11
|
-
|
12
|
-
|
13
|
-
|
83
|
+
```ruby
|
84
|
+
gem 'json_builder'
|
85
|
+
```
|
14
86
|
|
15
87
|
Second, make sure your controller responds to `json`:
|
16
88
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
89
|
+
```ruby
|
90
|
+
class UsersController < ApplicationController
|
91
|
+
respond_to :json
|
92
|
+
|
93
|
+
def index
|
94
|
+
@users = User.order('id DESC').page(params[:page]).per(2)
|
95
|
+
respond_with @users
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
Lastly, create `app/views/users/index.json_builder` which could look something like:
|
27
101
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
102
|
+
```ruby
|
103
|
+
count @users.count
|
104
|
+
current_page @users.current_page
|
105
|
+
per_page @users.per_page
|
106
|
+
num_pages @users.num_pages
|
107
|
+
results @users do |user|
|
108
|
+
id user.id
|
109
|
+
name user.name
|
110
|
+
body user.body
|
111
|
+
links user.links do |link|
|
112
|
+
url link.url
|
113
|
+
visits link.visits
|
114
|
+
last_visited link.last_visited
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
37
118
|
|
38
119
|
You will get something like:
|
39
120
|
|
121
|
+
```json
|
122
|
+
{
|
123
|
+
"total": 10,
|
124
|
+
"page": 1,
|
125
|
+
"per_page": 2,
|
126
|
+
"total_pages": 5,
|
127
|
+
"results": [
|
40
128
|
{
|
41
|
-
"
|
129
|
+
"id": 1,
|
130
|
+
"name": "Garrett Bjerkhoel",
|
131
|
+
"body": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.",
|
132
|
+
"links": [
|
133
|
+
{
|
134
|
+
"url": "http://github.com/",
|
135
|
+
"visits": 500,
|
136
|
+
"last_visited": "2011-11-271T00:00:01Z"
|
137
|
+
},
|
138
|
+
{
|
139
|
+
"url": "http://garrettbjerkhoel.com/",
|
140
|
+
"visits": 1500,
|
141
|
+
"last_visited": "2011-11-261T00:00:01Z"
|
142
|
+
}
|
143
|
+
]
|
144
|
+
}, {
|
145
|
+
"id": 2,
|
146
|
+
"name": "John Doe",
|
147
|
+
"body": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod.",
|
148
|
+
"links": [
|
42
149
|
{
|
43
|
-
"
|
44
|
-
"
|
45
|
-
"
|
46
|
-
},
|
47
|
-
|
48
|
-
"
|
49
|
-
"
|
150
|
+
"url": "http://google.com/",
|
151
|
+
"visits": 11000,
|
152
|
+
"last_visited": "2010-05-221T00:00:01Z"
|
153
|
+
},
|
154
|
+
{
|
155
|
+
"url": "http://twitter.com/",
|
156
|
+
"visits": 155012857,
|
157
|
+
"last_visited": "2011-11-261T00:00:01Z"
|
50
158
|
}
|
51
159
|
]
|
52
160
|
}
|
161
|
+
]
|
162
|
+
}
|
163
|
+
```
|
53
164
|
|
54
|
-
|
165
|
+
### Including JSONP callbacks
|
55
166
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
167
|
+
Out of the box JSON Builder supports JSONP callbacks when used within a Rails project just by using the callback parameter. For instance, if you requested `/users.json?callback=myjscallback`, you'll get a callback wrapping the response:
|
168
|
+
|
169
|
+
```json
|
170
|
+
myjscallback([
|
171
|
+
{
|
172
|
+
"name": "Garrett Bjerkhoel"
|
173
|
+
},
|
174
|
+
{
|
175
|
+
"name": "John Doe"
|
176
|
+
}
|
177
|
+
])
|
178
|
+
```
|
179
|
+
|
180
|
+
To be able to turn off this functionality, you can do it per-environment, or globally:
|
181
|
+
|
182
|
+
#### Per Environment
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
Spyder::Application.configure do
|
186
|
+
config.action_view.json_callback = false
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
#### Globally
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
ActionView::Base.json_callback = false
|
194
|
+
```
|
195
|
+
|
196
|
+
### Pretty Print Output
|
197
|
+
|
198
|
+
Out of the box JSON Builder supports pretty printing the JSON for the development environment, the rest have it turned off for performance reasons. If you'd like to enable or disable pretty printing you can do it within your environment file or you can do it globally.
|
199
|
+
|
200
|
+
With pretty print on:
|
201
|
+
|
202
|
+
```json
|
203
|
+
{
|
204
|
+
"name": "Garrett Bjerkhoel",
|
205
|
+
"email": "spam@garrettbjerkhoel.com"
|
206
|
+
}
|
207
|
+
```
|
208
|
+
|
209
|
+
Without:
|
210
|
+
|
211
|
+
```json
|
212
|
+
{"name": "Garrett Bjerkhoel", "email": "spam@garrettbjerkhoel.com"}
|
213
|
+
```
|
214
|
+
|
215
|
+
#### Per Environment
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
Spyder::Application.configure do
|
219
|
+
config.action_view.pretty_print_json = false
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
#### Globally
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
ActionView::Base.pretty_print_json = false
|
227
|
+
```
|
73
228
|
|
74
229
|
## Conversions
|
75
230
|
|
76
231
|
Time - [ISO-8601](http://en.wikipedia.org/wiki/ISO_8601)
|
77
232
|
|
78
233
|
## Speed
|
79
|
-
JSON Builder is very fast, it's roughly 6 times faster than the core XML Builder based on the [speed benchmark](http://github.com/dewski/json_builder/blob/master/
|
234
|
+
JSON Builder is very fast, it's roughly 3.6 times faster than the core XML Builder based on the [speed benchmark](http://github.com/dewski/json_builder/blob/master/spec/benchmarks/builder.rb).
|
80
235
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
XML Builder 4.700000 0.110000 4.810000 (4.822932)
|
236
|
+
user system total real
|
237
|
+
JSONBuilder 2.950000 0.010000 2.960000 (2.968790)
|
238
|
+
Builder 10.820000 0.040000 10.860000 (10.930497)
|
85
239
|
|
86
240
|
## Examples
|
87
241
|
See the [examples](http://github.com/dewski/json_builder/tree/master/examples) directory.
|
88
242
|
|
243
|
+
## Note on Patches/Pull Requests
|
244
|
+
|
245
|
+
- Fork the project.
|
246
|
+
- Make your feature addition or bug fix.
|
247
|
+
- Add tests for it. This is important so I don't break it in a future version unintentionally.
|
248
|
+
- Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
249
|
+
- Send me a pull request. Bonus points for topic branches.
|
250
|
+
|
89
251
|
## Copyright
|
90
|
-
Copyright ©
|
252
|
+
Copyright © 2011 Garrett Bjerkhoel. See [MIT-LICENSE](http://github.com/dewski/json_builder/blob/master/MIT-LICENSE) for details.
|