jsonify 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/README.md +2 -2
- data/jsonify.gemspec +2 -2
- data/lib/jsonify.rb +1 -5
- data/lib/jsonify/builder.rb +7 -6
- data/lib/jsonify/json_value.rb +13 -7
- data/lib/jsonify/version.rb +1 -1
- data/spec/builder_spec.rb +16 -16
- data/spec/generate_spec.rb +2 -2
- data/spec/json_value_spec.rb +6 -6
- data/spec/spec_helper.rb +1 -0
- data/spec/template_spec.rb +2 -0
- metadata +27 -16
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -39,7 +39,7 @@ want to show this format). The default format, `plain`, dictates no special form
|
|
39
39
|
|
40
40
|
### Compatibility Warning
|
41
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
|
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 need to make.
|
43
43
|
|
44
44
|
Previously, when arrays were processed, you had to put away the builder-style, and use more conventional Rubyisms.
|
45
45
|
|
@@ -153,7 +153,7 @@ Let's assume this this is your main template, `index.jsonify`:
|
|
153
153
|
json.ingest! (render :partial=>'my_partial')
|
154
154
|
|
155
155
|
From the first line, you can tell that an array will be created as this line uses the append operator.
|
156
|
-
On the second line, a partial is being added to the builder. Note that you cannot simply place `render :
|
156
|
+
On the second line, a partial is being added to the builder. Note that you cannot simply place `render :partial ...` 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.
|
157
157
|
|
158
158
|
Let's say that the partial file, `_my_partial.jsonify`, is as follows:
|
159
159
|
|
data/jsonify.gemspec
CHANGED
@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
|
21
|
+
s.add_runtime_dependency 'multi_json', '~>1.0.4'
|
22
|
+
|
22
23
|
s.add_development_dependency 'json' unless RUBY_VERSION =~ /^1.9/
|
23
|
-
|
24
24
|
s.add_development_dependency 'bundler'
|
25
25
|
s.add_development_dependency 'tilt', '~>1.3.2'
|
26
26
|
s.add_development_dependency 'rake'
|
data/lib/jsonify.rb
CHANGED
data/lib/jsonify/builder.rb
CHANGED
@@ -57,16 +57,17 @@ module Jsonify
|
|
57
57
|
end
|
58
58
|
|
59
59
|
# Compiles the JSON objects into a string representation.
|
60
|
-
# If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +
|
61
|
-
# If initialized with +:format => :pretty+, the compiled result will be parsed and
|
60
|
+
# If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +MultiJson.decode+.
|
61
|
+
# If initialized with +:format => :pretty+, the compiled result will be parsed and encoded via +MultiJson.encode(<json>, :pretty => true)+
|
62
62
|
# This method can be called without any side effects. You can call +compile!+ at any time, and multiple times if desired.
|
63
63
|
#
|
64
64
|
# @raise [TypeError] only if +:verify+ is set to true
|
65
65
|
# @raise [JSON::ParseError] only if +:verify+ is set to true
|
66
66
|
def compile!
|
67
|
-
result = (@stack[0] || {}).
|
68
|
-
|
69
|
-
|
67
|
+
result = (@stack[0] || {}).encode_as_json
|
68
|
+
MultiJson.decode(result) if @verify
|
69
|
+
result = MultiJson.encode(MultiJson.decode(result), :pretty => true) if @pretty
|
70
|
+
result
|
70
71
|
end
|
71
72
|
|
72
73
|
# Stores the key and value into a JSON object
|
@@ -186,7 +187,7 @@ module Jsonify
|
|
186
187
|
# @param [String] json_string a full JSON string (e.g. from a rendered partial)
|
187
188
|
def ingest!(json_string)
|
188
189
|
return if json_string.empty?
|
189
|
-
res = Jsonify::Generate.value(
|
190
|
+
res = Jsonify::Generate.value(MultiJson.decode(json_string))
|
190
191
|
current = @stack[@level]
|
191
192
|
if current.nil?
|
192
193
|
@stack[@level] = res
|
data/lib/jsonify/json_value.rb
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
class Object
|
2
|
+
def encode_as_json
|
3
|
+
MultiJson.encode self
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
1
7
|
module Jsonify
|
2
8
|
class JsonValue
|
3
9
|
attr_accessor :values
|
@@ -6,8 +12,8 @@ module Jsonify
|
|
6
12
|
@values = values || []
|
7
13
|
end
|
8
14
|
|
9
|
-
def
|
10
|
-
wrap values.map {|v| v.
|
15
|
+
def encode_as_json
|
16
|
+
wrap values.map {|v| v.encode_as_json}.join(',')
|
11
17
|
end
|
12
18
|
|
13
19
|
def add(jsonValue)
|
@@ -69,25 +75,25 @@ module Jsonify
|
|
69
75
|
@key = key.to_s
|
70
76
|
@value = Generate.value(value)
|
71
77
|
end
|
72
|
-
def
|
73
|
-
%Q{#{key.
|
78
|
+
def encode_as_json
|
79
|
+
%Q{#{key.encode_as_json}:#{value.encode_as_json}}
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
77
83
|
class JsonTrue < JsonValue
|
78
|
-
def
|
84
|
+
def encode_as_json
|
79
85
|
'true'
|
80
86
|
end
|
81
87
|
end
|
82
88
|
|
83
89
|
class JsonFalse < JsonValue
|
84
|
-
def
|
90
|
+
def encode_as_json
|
85
91
|
'false'
|
86
92
|
end
|
87
93
|
end
|
88
94
|
|
89
95
|
class JsonNull < JsonValue
|
90
|
-
def
|
96
|
+
def encode_as_json
|
91
97
|
'null'
|
92
98
|
end
|
93
99
|
end
|
data/lib/jsonify/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -50,14 +50,14 @@ PRETTY_JSON
|
|
50
50
|
attr_accessor :stack
|
51
51
|
end
|
52
52
|
class FooBar
|
53
|
-
def
|
53
|
+
def encode_as_json
|
54
54
|
"foobar"
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
json = TestBuilder.new(:verify => true)
|
59
59
|
json.stack << FooBar.new
|
60
|
-
lambda{ json.compile! }.should raise_error(
|
60
|
+
lambda{ json.compile! }.should raise_error(MultiJson::DecodeError)
|
61
61
|
end
|
62
62
|
end
|
63
63
|
describe 'unicode characters' do
|
@@ -108,7 +108,7 @@ PRETTY_JSON
|
|
108
108
|
end
|
109
109
|
it 'should support the store! message' do
|
110
110
|
json.store!( "foo", "bar" ).store!( 'no', "whar" )
|
111
|
-
|
111
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode('{"foo":"bar","no":"whar"}')
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
@@ -137,12 +137,12 @@ PRETTY_JSON
|
|
137
137
|
json.foo :bar
|
138
138
|
json.go :far
|
139
139
|
expected = '{"foo":"bar","go":"far"}'
|
140
|
-
|
140
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
141
141
|
end
|
142
142
|
it 'should handle arrays' do
|
143
143
|
json[1] = [2, 3]
|
144
144
|
json[4] = 5
|
145
|
-
|
145
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode('{"1":[2,3],"4":5}')
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -155,7 +155,7 @@ PRETTY_JSON
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
expected = '{"foo foo":{"bar bar":{"buzz buzz":"goo goo"}}}'
|
158
|
-
|
158
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
159
159
|
end
|
160
160
|
|
161
161
|
it 'complex hash' do
|
@@ -216,7 +216,7 @@ PRETTY_JSON
|
|
216
216
|
end
|
217
217
|
end
|
218
218
|
expected = "{\"foo\":{\"bar\":{\"baz\":\"goo\",\"years\":[2011,2012]}}}"
|
219
|
-
|
219
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
220
220
|
end
|
221
221
|
end
|
222
222
|
|
@@ -226,7 +226,7 @@ PRETTY_JSON
|
|
226
226
|
it 'should work' do
|
227
227
|
json.bar [1,2,{:foo => 'goo'}]
|
228
228
|
expected = "{\"bar\":[1,2,{\"foo\":\"goo\"}]}"
|
229
|
-
|
229
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
230
230
|
end
|
231
231
|
end
|
232
232
|
|
@@ -259,7 +259,7 @@ PRETTY_JSON
|
|
259
259
|
end
|
260
260
|
end
|
261
261
|
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
|
262
|
-
|
262
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
263
263
|
end
|
264
264
|
end
|
265
265
|
|
@@ -269,20 +269,20 @@ PRETTY_JSON
|
|
269
269
|
context 'into' do
|
270
270
|
it 'nothing -- should replace it' do
|
271
271
|
json.ingest! json_string
|
272
|
-
|
272
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(json_string)
|
273
273
|
end
|
274
274
|
it 'json object -- should merge' do
|
275
275
|
json["my boy"] = "Monday"
|
276
276
|
json["my girl"] = "Sunday"
|
277
277
|
json.ingest! json_string
|
278
278
|
expected = '{"my boy":"Monday","my girl":"Friday","my daughter":"Wednesday"}'
|
279
|
-
|
279
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
280
280
|
end
|
281
281
|
it 'json array -- should add' do
|
282
282
|
json << 1 << 2
|
283
283
|
json.ingest! json_string
|
284
284
|
expected = '[1,2,{"my girl":"Friday","my daughter":"Wednesday"}]'
|
285
|
-
|
285
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
286
286
|
end
|
287
287
|
end
|
288
288
|
end
|
@@ -291,7 +291,7 @@ PRETTY_JSON
|
|
291
291
|
context 'into' do
|
292
292
|
it 'nothing -- should replace it' do
|
293
293
|
json.ingest! json_string
|
294
|
-
|
294
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(json_string)
|
295
295
|
end
|
296
296
|
it 'json object -- should raise error' do
|
297
297
|
json["my boy"] = "Monday"
|
@@ -302,7 +302,7 @@ PRETTY_JSON
|
|
302
302
|
json << 1 << 2
|
303
303
|
json.ingest! json_string
|
304
304
|
expected = '[1,2,[1,2,3]]'
|
305
|
-
|
305
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
306
306
|
end
|
307
307
|
end
|
308
308
|
end
|
@@ -323,14 +323,14 @@ PRETTY_JSON
|
|
323
323
|
end
|
324
324
|
|
325
325
|
expected = '{"results":[{"id":1,"children":[{"id":"a"},{"id":"b"}]},{"id":2,"children":[{"id":"c"},{"id":"d"}]}]}'
|
326
|
-
|
326
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
327
327
|
end
|
328
328
|
it 'simple append' do
|
329
329
|
json.letters('a'..'c') do |letter|
|
330
330
|
json << letter.upcase
|
331
331
|
end
|
332
332
|
expected = '{"letters":["A","B","C"]}'
|
333
|
-
|
333
|
+
MultiJson.decode(json.compile!).should == MultiJson.decode(expected)
|
334
334
|
end
|
335
335
|
|
336
336
|
end
|
data/spec/generate_spec.rb
CHANGED
@@ -13,7 +13,7 @@ describe Jsonify::Generate do
|
|
13
13
|
json = Jsonify::Generate
|
14
14
|
result = json.value links
|
15
15
|
expected = '{"links":[{"rel":"foo","href":"goo"},{"rel":"bar","href":"baz"}]}'
|
16
|
-
|
16
|
+
MultiJson.decode(result.encode_as_json).should == MultiJson.decode(expected)
|
17
17
|
end
|
18
18
|
|
19
19
|
describe 'complex example' do
|
@@ -29,7 +29,7 @@ describe Jsonify::Generate do
|
|
29
29
|
}
|
30
30
|
)
|
31
31
|
expected = "{\"links\":[{\"rel\":\"foo\",\"href\":\"goo\"},{\"rel\":\"bar\",\"href\":\"baz\"}]}"
|
32
|
-
|
32
|
+
MultiJson.decode(json.encode_as_json).should == MultiJson.decode(expected)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/spec/json_value_spec.rb
CHANGED
@@ -8,35 +8,35 @@ describe Jsonify::JsonValue do
|
|
8
8
|
pair.key.should == 'key'
|
9
9
|
end
|
10
10
|
it 'should evaluate to key:value' do
|
11
|
-
pair.
|
11
|
+
pair.encode_as_json.should == "\"key\":\"value\""
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
describe Jsonify::JsonTrue do
|
16
16
|
it 'should have a value of true' do
|
17
|
-
Jsonify::JsonTrue.new.
|
17
|
+
Jsonify::JsonTrue.new.encode_as_json.should == 'true'
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe Jsonify::JsonFalse do
|
22
22
|
it 'should have a value of false' do
|
23
|
-
Jsonify::JsonFalse.new.
|
23
|
+
Jsonify::JsonFalse.new.encode_as_json.should == 'false'
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
describe Jsonify::JsonNull do
|
28
28
|
it 'should have a value of true' do
|
29
|
-
Jsonify::JsonNull.new.
|
29
|
+
Jsonify::JsonNull.new.encode_as_json.should == 'null'
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
describe 'strings' do
|
34
34
|
it 'should quote the value' do
|
35
|
-
'foo'.
|
35
|
+
'foo'.encode_as_json.should == "\"foo\""
|
36
36
|
end
|
37
37
|
it 'should encode unicode' do
|
38
38
|
unicode = 'goober'.concat(16)
|
39
|
-
unicode.
|
39
|
+
unicode.encode_as_json.should == "\"goober\\u0010\""
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
data/spec/spec_helper.rb
CHANGED
data/spec/template_spec.rb
CHANGED
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.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-30 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: multi_json
|
16
|
+
requirement: &70207654989540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.4
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70207654989540
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: bundler
|
16
|
-
requirement: &
|
27
|
+
requirement: &70207654987420 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ! '>='
|
@@ -21,10 +32,10 @@ dependencies:
|
|
21
32
|
version: '0'
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *70207654987420
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: tilt
|
27
|
-
requirement: &
|
38
|
+
requirement: &70207654982440 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ~>
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: 1.3.2
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70207654982440
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rake
|
38
|
-
requirement: &
|
49
|
+
requirement: &70207654967060 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70207654967060
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: rspec
|
49
|
-
requirement: &
|
60
|
+
requirement: &70207654965240 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70207654965240
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: yard
|
60
|
-
requirement: &
|
71
|
+
requirement: &70207654963880 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,10 +76,10 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70207654963880
|
69
80
|
- !ruby/object:Gem::Dependency
|
70
81
|
name: rdiscount
|
71
|
-
requirement: &
|
82
|
+
requirement: &70207654961800 !ruby/object:Gem::Requirement
|
72
83
|
none: false
|
73
84
|
requirements:
|
74
85
|
- - ! '>='
|
@@ -76,7 +87,7 @@ dependencies:
|
|
76
87
|
version: '0'
|
77
88
|
type: :development
|
78
89
|
prerelease: false
|
79
|
-
version_requirements: *
|
90
|
+
version_requirements: *70207654961800
|
80
91
|
description: Turn Ruby objects into JSON -- correctly!
|
81
92
|
email:
|
82
93
|
- bsiggelkow@me.com
|
@@ -122,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
133
|
version: '0'
|
123
134
|
segments:
|
124
135
|
- 0
|
125
|
-
hash:
|
136
|
+
hash: 3560170480060204469
|
126
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
138
|
none: false
|
128
139
|
requirements:
|
@@ -131,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
142
|
version: '0'
|
132
143
|
segments:
|
133
144
|
- 0
|
134
|
-
hash:
|
145
|
+
hash: 3560170480060204469
|
135
146
|
requirements: []
|
136
147
|
rubyforge_project: jsonify
|
137
148
|
rubygems_version: 1.8.6
|