jsonify 0.0.4 → 0.0.5
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 +54 -29
- data/jsonify.gemspec +0 -1
- data/lib/jsonify.rb +1 -2
- data/lib/jsonify/version.rb +1 -1
- metadata +74 -95
- data/lib/jsonify/rails.rb +0 -19
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# Jsonify — a builder for JSON [](http://travis-ci.org/bsiggelkow/jsonify)
|
2
2
|
|
3
|
-
[Jsonify](https://github.com/bsiggelkow/jsonify) is to JSON as [Builder](https://github.com/jimweirich/builder) is to XML.
|
3
|
+
[Jsonify](https://github.com/bsiggelkow/jsonify) is to JSON as [Builder](https://github.com/jimweirich/builder) is to XML.
|
4
|
+
[Jsonify-Rails](https://github.com/bsiggelkow/jsonify-rails) allows you to
|
5
|
+
create Rails views using Jsonify templates.
|
4
6
|
|
5
7
|
## Goal
|
6
8
|
|
7
9
|
Jsonify provides a ___builder___ style engine for creating correct JSON representations of Ruby objects.
|
8
10
|
|
9
|
-
Jsonify hooks into Rails ActionView to allow you to create JSON view templates in much the same way that you can use Builder for XML templates.
|
11
|
+
[Jsonify-Rails](http://github.com/bsiggelkow/jsonify-rails) hooks into Rails ActionView to allow you to create JSON view templates in much the same way that you can use Builder for XML templates.
|
10
12
|
|
11
13
|
## Motivation
|
12
14
|
|
@@ -85,70 +87,72 @@ Results in ...
|
|
85
87
|
|
86
88
|
### View Templates
|
87
89
|
|
88
|
-
Jsonify includes Rails 3 template handler.
|
90
|
+
Jsonify includes a Rails 3 template handler. Any template with a `.jsonify` will be handled by Rails.
|
91
|
+
|
89
92
|
The Jsonify template handler exposes the `Jsonify::Builder` instance to your template with the `json` variable as in the following example:
|
90
93
|
|
91
94
|
json.hello do
|
92
95
|
json.world "Jsonify is Working!"
|
93
96
|
end
|
97
|
+
|
98
|
+
Just like with any other template, your Jsonify template will have access to
|
99
|
+
any instance variables that are exposed through the controller.
|
94
100
|
|
95
101
|
#### Partials
|
96
102
|
|
97
|
-
You can use partials
|
98
|
-
depend on how the information they return is structured. An important to keep in
|
99
|
-
mind is that a partial, no matter what kind it is, always a returns a string.
|
103
|
+
You can use partials from Jsonify views, and you can create Jsonify partials. How your Jsonify template uses a partial depends on how the information the partial returns is structured. Keep in mind that any paritial, be it a Jsonify template, erb, or anything else, always a returns its result as a string.
|
100
104
|
|
101
105
|
##### Jsonify partials
|
102
106
|
|
103
107
|
Any Jsonify partial — that is, the file has a `.jsonify` extension —
|
104
|
-
will return, by design, a string that is valid JSON. It will represent either a JSON object,
|
105
|
-
|
108
|
+
will return, by design, a string that is valid JSON. It will represent either a JSON object,wrapped in curly braces ( {} ), or a JSON array, wrapped in square brackets ( [] ).
|
109
|
+
|
110
|
+
To incorporate such a value into a Jsonify template, use the `ingest!` method.
|
106
111
|
|
107
|
-
|
112
|
+
`ingest!` assumes that the value it receives is valid JSON representation. It parses the JSON into a Jsonify object graph, and then adds it to the current Jsonify builder.
|
108
113
|
|
109
|
-
|
110
|
-
Let's assume this our main template, `index.jsonify`:
|
114
|
+
Let's assume this this is your main template, `index.jsonify`:
|
111
115
|
|
112
116
|
json << 1
|
113
117
|
json.ingest! (render :partial=>'my_partial')
|
114
118
|
|
115
|
-
From the first line, you can tell that an array will be created
|
116
|
-
On the second line, a partial is being added to the builder. Note that you cannot simply place `render :parial ...`
|
117
|
-
on a line by itself as you can do with other templates like `erb` and `haml`; you have to explicitly tell Jsonify
|
118
|
-
to add it.
|
119
|
+
From the first line, you can tell that an array will be created as this line uses the append operator.
|
120
|
+
On the second line, a partial is being added to the builder. Note that you cannot simply place `render :parial ...` on a line by itself as you can do with other templates like `erb` and `haml`. You have to explicitly tell Jsonify to add it to the builder.
|
119
121
|
|
120
122
|
Let's say that the partial file, `_my_partial.jsonify`, is as follows:
|
121
123
|
|
122
124
|
json << 3
|
123
125
|
json << 4
|
124
126
|
|
125
|
-
This `json` variable is a separate
|
126
|
-
|
127
|
+
This `json` variable in this partial is a separate distinct `Jsonify::Builder` instance from the `json` variable in the main template.
|
128
|
+
|
129
|
+
> Note: Figure out if a the `json` instance can be passed to the Jsonify partial.
|
130
|
+
> It would make things easier and we wouldn't have to ingest the result.
|
131
|
+
|
132
|
+
This partial results in the following string:
|
127
133
|
|
128
134
|
"[3,4]"
|
129
135
|
|
130
|
-
The `ingest!` method will actually parse this string back into a Jsonify-based object, and
|
131
|
-
to the builder's current state. The resulting output will be:
|
136
|
+
The `ingest!` method will actually parse this string back into a Jsonify-based object, and adds it to the builder's current state. The resulting output will be:
|
132
137
|
|
133
138
|
"[1,[3,4]]"
|
134
139
|
|
135
140
|
##### Other partials
|
136
141
|
|
137
|
-
You can also use output from non-Jsonify templates (e.g. erb); just remember that the output from a template
|
138
|
-
|
139
|
-
For example, suppose
|
142
|
+
You can also use output from non-Jsonify templates (e.g. erb); just remember that the output from a template is always a string and that you have to tell the builder how to include the result of the partial.
|
143
|
+
|
144
|
+
For example, suppose you have the partial `_today.erb` with the following content:
|
140
145
|
|
141
146
|
<%= Date.today %>
|
142
147
|
|
143
148
|
You can then incorporate this partial into your Jsonify template just as you would any other string value:
|
144
149
|
|
145
150
|
json << 1
|
146
|
-
json.ingest! (render :partial=>'my_partial')
|
147
151
|
json << {:date => (render :partial => 'today')}
|
148
152
|
|
149
153
|
renders ...
|
150
154
|
|
151
|
-
[1,
|
155
|
+
[1,{"date":"2011-07-30"}]
|
152
156
|
|
153
157
|
### Usage Patterns
|
154
158
|
|
@@ -168,10 +172,9 @@ must be one of these structures and Jsonify ensures that this condition is met.
|
|
168
172
|
|
169
173
|
A JSON object, sometimes
|
170
174
|
referred to as an ___object literal___, is a common structure familiar
|
171
|
-
to most developers.
|
175
|
+
to most developers. It's analogous to the nested element structure common
|
172
176
|
in XML. The [JSON RFC](http://www.ietf.org/rfc/rfc4627.txt) states that
|
173
|
-
"the names within an object SHOULD be unique". Jsonify
|
174
|
-
by backing the JsonObject with a `Hash`; an object must have unique keys and the last one in, wins.
|
177
|
+
"the names within an object SHOULD be unique". Jsonify enforces this recommendation by backing the JsonObject with a `Hash`; an object must have unique keys and the last one in, wins.
|
175
178
|
|
176
179
|
json = Jsonify::Builder.new
|
177
180
|
json.person do # start a new JsonObject where the key is 'foo'
|
@@ -326,7 +329,30 @@ Of course, standard iteration works here as well ...
|
|
326
329
|
|
327
330
|
#### Mixing JSON Arrays and Objects
|
328
331
|
|
329
|
-
|
332
|
+
You can readily mix JSON arrays and objects and the Jsonify builder will do
|
333
|
+
its best to keep things straight.
|
334
|
+
|
335
|
+
Here's an example where we start off with an array; but then decide to throw in an object.
|
336
|
+
|
337
|
+
json = Jsonify::Builder.new
|
338
|
+
json.append! 1,2,3
|
339
|
+
json.say "go, cat go"
|
340
|
+
|
341
|
+
compiles to ...
|
342
|
+
|
343
|
+
[1,2,3,{"say":"go, cat go"}]
|
344
|
+
|
345
|
+
When Jsonify detected that you were trying to add a JSON name-value pair to a JSON array, it converted that pair to a JSON object.
|
346
|
+
|
347
|
+
Let's take a look at the inverse approach ... say, we are creating a JSON object; and then decide to add an array item ...
|
348
|
+
|
349
|
+
json.foo 'bar'
|
350
|
+
json.go 'far'
|
351
|
+
json << 'baz'
|
352
|
+
|
353
|
+
In this case, Jsonify decides from the first line that you are creating a JSON object. When it gets to the third line, it simply turns the single item ('baz') into a name-value pair with a `null` value:
|
354
|
+
|
355
|
+
{"foo":"bar","go":"far","baz":null}
|
330
356
|
|
331
357
|
## Documentation
|
332
358
|
|
@@ -342,7 +368,6 @@ ___coming soon___
|
|
342
368
|
|
343
369
|
## TODOs
|
344
370
|
1. Benchmark performance
|
345
|
-
1. Document how partials can be used
|
346
371
|
1. Clean up specs
|
347
372
|
|
348
373
|
## Roadmap
|
data/jsonify.gemspec
CHANGED
data/lib/jsonify.rb
CHANGED
data/lib/jsonify/version.rb
CHANGED
metadata
CHANGED
@@ -1,116 +1,100 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonify
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.4
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Bill Siggelkow
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-08-02 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: json
|
18
|
-
requirement: &
|
16
|
+
requirement: &70100933802020 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
24
22
|
type: :runtime
|
25
23
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: actionpack
|
29
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
-
none: false
|
31
|
-
requirements:
|
32
|
-
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: 3.0.0
|
35
|
-
type: :runtime
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
24
|
+
version_requirements: *70100933802020
|
25
|
+
- !ruby/object:Gem::Dependency
|
39
26
|
name: bundler
|
40
|
-
requirement: &
|
27
|
+
requirement: &70100933801000 !ruby/object:Gem::Requirement
|
41
28
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
46
33
|
type: :development
|
47
34
|
prerelease: false
|
48
|
-
version_requirements: *
|
49
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: *70100933801000
|
36
|
+
- !ruby/object:Gem::Dependency
|
50
37
|
name: rake
|
51
|
-
requirement: &
|
38
|
+
requirement: &70100933799960 !ruby/object:Gem::Requirement
|
52
39
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
57
44
|
type: :development
|
58
45
|
prerelease: false
|
59
|
-
version_requirements: *
|
60
|
-
- !ruby/object:Gem::Dependency
|
46
|
+
version_requirements: *70100933799960
|
47
|
+
- !ruby/object:Gem::Dependency
|
61
48
|
name: rspec
|
62
|
-
requirement: &
|
49
|
+
requirement: &70100933798940 !ruby/object:Gem::Requirement
|
63
50
|
none: false
|
64
|
-
requirements:
|
65
|
-
- -
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version:
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
68
55
|
type: :development
|
69
56
|
prerelease: false
|
70
|
-
version_requirements: *
|
71
|
-
- !ruby/object:Gem::Dependency
|
57
|
+
version_requirements: *70100933798940
|
58
|
+
- !ruby/object:Gem::Dependency
|
72
59
|
name: autotest
|
73
|
-
requirement: &
|
60
|
+
requirement: &70100933797480 !ruby/object:Gem::Requirement
|
74
61
|
none: false
|
75
|
-
requirements:
|
76
|
-
- -
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version:
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
79
66
|
type: :development
|
80
67
|
prerelease: false
|
81
|
-
version_requirements: *
|
82
|
-
- !ruby/object:Gem::Dependency
|
68
|
+
version_requirements: *70100933797480
|
69
|
+
- !ruby/object:Gem::Dependency
|
83
70
|
name: yard
|
84
|
-
requirement: &
|
71
|
+
requirement: &70100933796540 !ruby/object:Gem::Requirement
|
85
72
|
none: false
|
86
|
-
requirements:
|
87
|
-
- -
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version:
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
90
77
|
type: :development
|
91
78
|
prerelease: false
|
92
|
-
version_requirements: *
|
93
|
-
- !ruby/object:Gem::Dependency
|
79
|
+
version_requirements: *70100933796540
|
80
|
+
- !ruby/object:Gem::Dependency
|
94
81
|
name: rdiscount
|
95
|
-
requirement: &
|
82
|
+
requirement: &70100933795500 !ruby/object:Gem::Requirement
|
96
83
|
none: false
|
97
|
-
requirements:
|
98
|
-
- -
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
version:
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
101
88
|
type: :development
|
102
89
|
prerelease: false
|
103
|
-
version_requirements: *
|
90
|
+
version_requirements: *70100933795500
|
104
91
|
description: Turn Ruby objects into JSON -- correctly!
|
105
|
-
email:
|
92
|
+
email:
|
106
93
|
- bsiggelkow@me.com
|
107
94
|
executables: []
|
108
|
-
|
109
95
|
extensions: []
|
110
|
-
|
111
96
|
extra_rdoc_files: []
|
112
|
-
|
113
|
-
files:
|
97
|
+
files:
|
114
98
|
- .gitignore
|
115
99
|
- .travis.yml
|
116
100
|
- Gemfile
|
@@ -123,48 +107,43 @@ files:
|
|
123
107
|
- lib/jsonify/builder.rb
|
124
108
|
- lib/jsonify/generate.rb
|
125
109
|
- lib/jsonify/json_value.rb
|
126
|
-
- lib/jsonify/rails.rb
|
127
110
|
- lib/jsonify/version.rb
|
128
111
|
- spec/builder_spec.rb
|
129
112
|
- spec/generate_spec.rb
|
130
113
|
- spec/json_value_spec.rb
|
131
114
|
- spec/jsonify_spec.rb
|
132
115
|
- spec/spec_helper.rb
|
133
|
-
has_rdoc: true
|
134
116
|
homepage: http://github.com/bsiggelkow/jsonify
|
135
117
|
licenses: []
|
136
|
-
|
137
118
|
post_install_message:
|
138
119
|
rdoc_options: []
|
139
|
-
|
140
|
-
require_paths:
|
120
|
+
require_paths:
|
141
121
|
- lib
|
142
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
123
|
none: false
|
144
|
-
requirements:
|
145
|
-
- -
|
146
|
-
- !ruby/object:Gem::Version
|
147
|
-
|
148
|
-
segments:
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
segments:
|
149
129
|
- 0
|
150
|
-
|
151
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
hash: 184180262065251326
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
132
|
none: false
|
153
|
-
requirements:
|
154
|
-
- -
|
155
|
-
- !ruby/object:Gem::Version
|
156
|
-
|
157
|
-
segments:
|
133
|
+
requirements:
|
134
|
+
- - ! '>='
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
segments:
|
158
138
|
- 0
|
159
|
-
|
139
|
+
hash: 184180262065251326
|
160
140
|
requirements: []
|
161
|
-
|
162
141
|
rubyforge_project: jsonify
|
163
|
-
rubygems_version: 1.
|
142
|
+
rubygems_version: 1.8.6
|
164
143
|
signing_key:
|
165
144
|
specification_version: 3
|
166
145
|
summary: Turn Ruby objects into JSON
|
167
|
-
test_files:
|
146
|
+
test_files:
|
168
147
|
- spec/builder_spec.rb
|
169
148
|
- spec/generate_spec.rb
|
170
149
|
- spec/json_value_spec.rb
|
data/lib/jsonify/rails.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'action_view'
|
2
|
-
|
3
|
-
module ActionView
|
4
|
-
module Template::Handlers
|
5
|
-
class JsonifyBuilder < Template::Handler
|
6
|
-
include Compilable
|
7
|
-
|
8
|
-
self.default_format = Mime::JSON
|
9
|
-
|
10
|
-
def compile(template)
|
11
|
-
"json = ::Jsonify::Builder.new();" +
|
12
|
-
template.source +
|
13
|
-
";json.compile!;"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
ActionView::Template.register_template_handler :jsonify, ActionView::Template::Handlers::JsonifyBuilder
|