jsonify 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +3 -0
- data/README.md +44 -13
- data/jsonify.gemspec +2 -0
- data/lib/jsonify/builder.rb +84 -14
- data/lib/jsonify/generate.rb +16 -1
- data/lib/jsonify/json_value.rb +5 -4
- data/lib/jsonify/version.rb +1 -1
- data/spec/builder_spec.rb +59 -31
- data/spec/generate_spec.rb +4 -2
- data/spec/json_value_spec.rb +40 -2
- metadata +27 -4
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Jsonify
|
2
2
|
|
3
|
-
[Jsonify](https://github.com/bsiggelkow/jsonify)
|
3
|
+
[Jsonify](https://github.com/bsiggelkow/jsonify) is to JSON as [Builder](https://github.com/jimweirich/builder) is to XML.
|
4
4
|
|
5
5
|
## Goal
|
6
6
|
|
@@ -13,6 +13,19 @@ Jsonify hooks into Rails ActionView to allow you to create JSON view templates i
|
|
13
13
|
JSON and XML are without a doubt the most common representations used by RESTful applications. Jsonify was built around the notion that these representations belong in the ___view___ layer of the application.
|
14
14
|
For XML representations, Rails makes this easy through its support of Builder templates, but, when it comes to JSON, there is no clear approach.
|
15
15
|
|
16
|
+
For many applications, particularly those based on legacy database, its not uncommon to expose the data in more client-friendly representations than would be presented by the default Rails `to_json` method.
|
17
|
+
Rails does provide control of the emitted via a custom implementation of `as_json`.
|
18
|
+
Nevertheless, this forces the developer to place this code into the model when it more rightly belongs in the view.
|
19
|
+
|
20
|
+
When someone asks "Where are the model representations defined?", I don't want to have to say "Well, look in the views folder for XML, but you have to look at the code in the model for the JSON format."
|
21
|
+
|
22
|
+
There are other good libraries available that help solve this problem, such as Tokamak and RABL, that allow the developer to specify the representation in one format that is then interpreted into the wanted format at runtime.
|
23
|
+
Please take a look at these projects when you consider alternatives; however, its my opinion that they are substantial and inherent differences between XML and JSON.
|
24
|
+
|
25
|
+
These differences may force the developer make concessions in one format to make the other format correct.
|
26
|
+
|
27
|
+
But an even greater motivation for me was emulating the simplicity of Builder. I have not found a single framework for JSON that provides the simplicity and elegance of Builder. Jsonify is my attempt at remedying that situation.
|
28
|
+
|
16
29
|
___more coming soon___
|
17
30
|
|
18
31
|
## Installation
|
@@ -22,10 +35,9 @@ gem install jsonify
|
|
22
35
|
## Usage
|
23
36
|
|
24
37
|
### Standalone
|
25
|
-
|
26
38
|
# Create some objects that represent a person and associated hyperlinks
|
27
|
-
person = Struct.new(:first_name,:last_name).new('George','Burdell')
|
28
|
-
links = [
|
39
|
+
@person = Struct.new(:first_name,:last_name).new('George','Burdell')
|
40
|
+
@links = [
|
29
41
|
['self', 'http://example.com/people/123'],
|
30
42
|
['school', 'http://gatech.edu'],
|
31
43
|
]
|
@@ -33,16 +45,14 @@ gem install jsonify
|
|
33
45
|
# Build this information as JSON
|
34
46
|
require 'jsonify'
|
35
47
|
json = Jsonify::Builder.new(:pretty => true)
|
36
|
-
|
48
|
+
|
37
49
|
json.result do
|
38
50
|
json.alumnus do
|
39
|
-
json.fname person.first_name
|
40
|
-
json.lname person.last_name
|
51
|
+
json.fname @person.first_name
|
52
|
+
json.lname @person.last_name
|
41
53
|
end
|
42
|
-
json.links do
|
43
|
-
|
44
|
-
{:rel => link.first, :href => link.last}
|
45
|
-
end
|
54
|
+
json.links(@links) do |link|
|
55
|
+
{:rel => link.first, :href => link.last}
|
46
56
|
end
|
47
57
|
end
|
48
58
|
|
@@ -70,10 +80,31 @@ Results in ...
|
|
70
80
|
}
|
71
81
|
}
|
72
82
|
|
73
|
-
|
74
83
|
### View Templates
|
75
84
|
|
76
|
-
|
85
|
+
Jsonify includes Rails 3 template handler. Rails will handle any template with a ___.jsonify___ extension with Jsonify.
|
86
|
+
The Jsonify template handler exposes the `Jsonify::Builder` instance to your template with the `json` variable as in the following example:
|
87
|
+
|
88
|
+
json.hello do
|
89
|
+
json.world "Jsonify is Working!"
|
90
|
+
end
|
91
|
+
|
92
|
+
## Documentation
|
93
|
+
|
94
|
+
[Jsonify Yard Docs](http://rubydoc.info/github/bsiggelkow/jsonify/master/frames)
|
95
|
+
|
96
|
+
## Build Status
|
97
|
+
|
98
|
+
[Compliments of Travis](http://travis-ci.org/bsiggelkow/jsonify)
|
99
|
+
|
100
|
+
## TODOs
|
101
|
+
1. Consider simplified means of creating arrays (e.g. json.links(@links) {|link| ...})
|
102
|
+
1. Benchmark performance
|
103
|
+
|
104
|
+
## Roadmap
|
105
|
+
|
106
|
+
1. Split Rails template handling into separate gem
|
107
|
+
1. Add support for Sinatra and Padrino (maybe separate gems)
|
77
108
|
|
78
109
|
## License
|
79
110
|
|
data/jsonify.gemspec
CHANGED
data/lib/jsonify/builder.rb
CHANGED
@@ -1,53 +1,96 @@
|
|
1
1
|
module Jsonify
|
2
2
|
class Builder < BlankSlate
|
3
3
|
|
4
|
+
# Initializes a new builder. The Jsonify::Builder works by keeping a stack of +JsonValue+s.
|
5
|
+
#
|
6
|
+
# @param [Hash] options the options to create with
|
7
|
+
# @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 [pretty] :pretty Builder will output the JSON string in a prettier format with new lines and indentation; this option does incur a performance penalty and generally should only be used in development
|
4
9
|
def initialize(options={})
|
5
10
|
@verify = options[:verify].nil? ? false : options[:verify]
|
6
11
|
@pretty = options[:pretty].nil? ? false : options[:pretty]
|
7
12
|
reset!
|
8
13
|
end
|
9
14
|
|
15
|
+
# Clears the builder data
|
10
16
|
def reset!
|
11
17
|
@level = 0
|
12
18
|
@stack = []
|
13
19
|
end
|
14
20
|
|
15
|
-
#
|
21
|
+
# Adds a new JsonPair to the builder. Use this method if the pair "key" has spaces or other characters that prohibit creation via method_missing.
|
22
|
+
#
|
23
|
+
# @param sym [String] the key for the pair
|
24
|
+
# @param *args [arguments] If a block is passed, the first argument will be iterated over and the subsequent result will be added to a JSON array; otherwise, the arguments set value for the `JsonPair`
|
25
|
+
# @param &block a code block the result of which will be used to populate the value for the JSON pair
|
16
26
|
def tag!(sym, *args, &block)
|
17
27
|
method_missing(sym, *args, &block)
|
18
28
|
end
|
19
29
|
|
30
|
+
# Compiles the JSON objects into a string representation.
|
31
|
+
# If initialized with +:verify => true+, the compiled result will be verified by attempting to re-parse it using +JSON.parse+.
|
32
|
+
# If initialized with +:pretty => true+, the compiled result will be parsed and regenerated via +JSON.pretty_generate+.
|
33
|
+
# This method can be called without any side effects. You can call +compile!+ at any time, and multiple times if desired.
|
34
|
+
#
|
35
|
+
# @raise [TypeError] only if +:verify+ is set to true
|
36
|
+
# @raise [JSON::ParseError] only if +:verify+ is set to true
|
20
37
|
def compile!
|
21
38
|
result = (@stack[0] ? @stack[0].evaluate : {}.to_json)
|
22
39
|
JSON.parse(result) if @verify
|
23
40
|
@pretty ? JSON.pretty_generate(JSON.parse(result)) : result
|
24
41
|
end
|
25
42
|
|
26
|
-
|
27
|
-
|
43
|
+
# Adds the value(s) to the current JSON object in the builder's stack.
|
44
|
+
# @param *args values
|
45
|
+
def add!(*args)
|
46
|
+
__current.add *args
|
28
47
|
end
|
29
48
|
|
49
|
+
alias_method :<<, :add!
|
50
|
+
|
51
|
+
# Adds a new JsonPair to the builder.
|
52
|
+
# This method will be called if the name does not match an existing method name.
|
53
|
+
#
|
54
|
+
# @param *args [Array] iterates over the given array yielding each array item to the block; the result of which is added to a JsonArray
|
30
55
|
def method_missing(sym, *args, &block)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
56
|
+
|
57
|
+
# When no block given, simply add the symbol and arg as key - value for a JsonPair to current
|
58
|
+
return __current.add( sym, args.length > 1 ? args : args.first ) unless block
|
59
|
+
|
60
|
+
# Create a JSON pair and add it to the current object
|
61
|
+
pair = Generate.pair_value(sym)
|
62
|
+
__current.add(pair)
|
63
|
+
|
64
|
+
# Now process the block
|
65
|
+
@level += 1
|
66
|
+
|
67
|
+
unless args.empty?
|
68
|
+
# Argument was given, iterate over it and add result to a JsonArray
|
69
|
+
__set_current JsonArray.new
|
70
|
+
args.first.each do |arg|
|
71
|
+
__current.add block.call(arg)
|
41
72
|
end
|
42
|
-
|
73
|
+
else
|
74
|
+
# No argument was given; ordinary JsonObject is expected
|
75
|
+
block.call(self)
|
43
76
|
end
|
77
|
+
|
78
|
+
# Set the value on the pair to current
|
79
|
+
pair.value = __current
|
80
|
+
|
81
|
+
# Pop current off the top of the stack; we are done with it at this point
|
82
|
+
@stack.pop
|
83
|
+
|
84
|
+
@level -= 1
|
44
85
|
end
|
45
86
|
|
87
|
+
# Sets the object at the top of the stack to a new JsonObject, which is yielded to the block.
|
46
88
|
def object!
|
47
89
|
__set_current JsonObject.new
|
48
90
|
yield __current
|
49
91
|
end
|
50
92
|
|
93
|
+
# Sets the object at the top of the stack to a new JsonArray, which is yielded to the block.
|
51
94
|
def array!
|
52
95
|
__set_current JsonArray.new
|
53
96
|
@level += 1
|
@@ -56,6 +99,27 @@ module Jsonify
|
|
56
99
|
__current
|
57
100
|
end
|
58
101
|
|
102
|
+
# Maps each element in the given array to a JsonArray. The result of the block becomes an array item of the JsonArray.
|
103
|
+
# @param array array of objects to iterate over.
|
104
|
+
#
|
105
|
+
# @example Map an of array of links to an array of JSON objects
|
106
|
+
# json.links do
|
107
|
+
# json.map!(links) do |link|
|
108
|
+
# {:rel => link.first, :href => link.last}
|
109
|
+
# end
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# @example compiles to something like ...
|
113
|
+
# "links": [
|
114
|
+
# {
|
115
|
+
# "rel": "self",
|
116
|
+
# "href": "http://example.com/people/123"
|
117
|
+
# },
|
118
|
+
# {
|
119
|
+
# "rel": "school",
|
120
|
+
# "href": "http://gatech.edu"
|
121
|
+
# }
|
122
|
+
# ]
|
59
123
|
def map!(array)
|
60
124
|
__set_current JsonArray.new
|
61
125
|
array.each do |item|
|
@@ -70,10 +134,16 @@ module Jsonify
|
|
70
134
|
|
71
135
|
# Inheriting from BlankSlate requires these funky (aka non-idiomatic) method names
|
72
136
|
|
137
|
+
# Current object at the top of the stack. If there is no object there; initializes to a new JsonObject
|
138
|
+
#
|
139
|
+
# @return [JsonValue] object at the top of the stack
|
73
140
|
def __current
|
74
141
|
@stack[@level] ||= JsonObject.new
|
75
142
|
end
|
76
143
|
|
144
|
+
# Sets the current object at the top of the stack
|
145
|
+
#
|
146
|
+
# @param val object to set at the top of the stack
|
77
147
|
def __set_current(val)
|
78
148
|
@stack[@level] = val
|
79
149
|
end
|
data/lib/jsonify/generate.rb
CHANGED
@@ -1,8 +1,23 @@
|
|
1
1
|
module Jsonify
|
2
|
+
|
3
|
+
# Provides a set of functions for creating JsonValues from Ruby objects.
|
2
4
|
module Generate
|
3
5
|
|
4
6
|
class << self
|
5
|
-
|
7
|
+
|
8
|
+
# Coerces the given value into a JsonValue.
|
9
|
+
#
|
10
|
+
# The coercion rules are based on the type (class) of the value as follows:
|
11
|
+
# - +JsonValue+ => no coercion
|
12
|
+
# - +String+ => JsonString ( \"foo\" )
|
13
|
+
# - +Numeric+ => JsonNumber ( 1 )
|
14
|
+
# - +TrueClass+ => JsonTrue ( true )
|
15
|
+
# - +FalseClass+=> JsonFalse ( false )
|
16
|
+
# - +NilClass+ => JsonNull ( null )
|
17
|
+
# - +Array+ => JsonArray ( [1,2,3] )
|
18
|
+
# - +Hash+ => JsonObject ( <code>{"\a":1,\"b\":2}</code> )
|
19
|
+
#
|
20
|
+
# @param val value to coerce into a JsonValue.
|
6
21
|
def value(val)
|
7
22
|
case val
|
8
23
|
when JsonValue; val
|
data/lib/jsonify/json_value.rb
CHANGED
@@ -29,13 +29,13 @@ module Jsonify
|
|
29
29
|
@values.values
|
30
30
|
end
|
31
31
|
|
32
|
-
def add(val)
|
33
|
-
|
34
|
-
|
35
|
-
@values.store(val.key, val)
|
32
|
+
def add(key, val=nil)
|
33
|
+
pair = (JsonPair === key) ? key : JsonPair.new(key, val)
|
34
|
+
@values.store(pair.key, pair)
|
36
35
|
end
|
37
36
|
|
38
37
|
alias_method :<<, :add
|
38
|
+
alias_method :add!, :add # for consistency with the Builder api
|
39
39
|
|
40
40
|
end
|
41
41
|
|
@@ -55,6 +55,7 @@ module Jsonify
|
|
55
55
|
end
|
56
56
|
|
57
57
|
alias_method :<<, :add
|
58
|
+
alias_method :add!, :add # for consistency with the Builder api
|
58
59
|
|
59
60
|
end
|
60
61
|
|
data/lib/jsonify/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -17,14 +17,19 @@ describe Jsonify::Builder do
|
|
17
17
|
end
|
18
18
|
describe 'with verify set' do
|
19
19
|
it 'should report a parse error if the result is not parseable' do
|
20
|
-
|
20
|
+
|
21
21
|
# Hackery to come up with a failing case
|
22
|
+
class TestBuilder < Jsonify::Builder
|
23
|
+
attr_accessor :stack
|
24
|
+
end
|
22
25
|
class FooBar
|
23
26
|
def evaluate
|
24
27
|
"foobar"
|
25
28
|
end
|
26
29
|
end
|
27
|
-
|
30
|
+
|
31
|
+
json = TestBuilder.new(:verify => true)
|
32
|
+
json.stack << FooBar.new
|
28
33
|
lambda{ json.compile! }.should raise_error(JSON::ParserError)
|
29
34
|
end
|
30
35
|
end
|
@@ -78,8 +83,8 @@ PRETTY_JSON
|
|
78
83
|
end
|
79
84
|
it 'array of hashes should work' do
|
80
85
|
json.array! do |ary|
|
81
|
-
ary << {foo
|
82
|
-
ary << {go
|
86
|
+
ary << {:foo => :bar}
|
87
|
+
ary << {:go => :far}
|
83
88
|
end
|
84
89
|
json.compile!.should == "[{\"foo\":\"bar\"},{\"go\":\"far\"}]"
|
85
90
|
end
|
@@ -88,22 +93,31 @@ PRETTY_JSON
|
|
88
93
|
describe 'objects' do
|
89
94
|
it 'simple object should work' do
|
90
95
|
json.object! do |obj|
|
91
|
-
obj
|
92
|
-
obj
|
96
|
+
obj.add :foo,:bar
|
97
|
+
obj.add :go, :far
|
93
98
|
end
|
94
99
|
json.compile!.should == "{\"foo\":\"bar\",\"go\":\"far\"}"
|
95
100
|
end
|
96
|
-
it 'should
|
101
|
+
it 'should handle arrays' do
|
97
102
|
json.object! do |obj|
|
98
|
-
obj
|
99
|
-
obj
|
103
|
+
obj.add 1, [2, 3]
|
104
|
+
obj.add 4, 5
|
100
105
|
end
|
101
106
|
json.compile!.should == '{"1":[2,3],"4":5}'
|
102
107
|
end
|
103
108
|
end
|
104
109
|
|
105
110
|
describe 'using blocks' do
|
106
|
-
|
111
|
+
|
112
|
+
it 'should allow names with spaces using tag!' do
|
113
|
+
json.tag!("foo foo") do
|
114
|
+
json.tag!("bar bar") do
|
115
|
+
json.tag!('buzz buzz','goo goo')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
json.compile!.should == "{\"foo foo\":{\"bar bar\":{\"buzz buzz\":\"goo goo\"}}}"
|
119
|
+
end
|
120
|
+
|
107
121
|
it 'complex hash' do
|
108
122
|
json.foo do
|
109
123
|
json.bar do
|
@@ -112,12 +126,14 @@ PRETTY_JSON
|
|
112
126
|
end
|
113
127
|
json.compile!.should == "{\"foo\":{\"bar\":{\"baz\":\"goo\"}}}"
|
114
128
|
end
|
129
|
+
|
115
130
|
it 'simple hash' do
|
116
131
|
json.foo do
|
117
132
|
json.baz :goo
|
118
133
|
end
|
119
134
|
json.compile!.should == "{\"foo\":{\"baz\":\"goo\"}}"
|
120
135
|
end
|
136
|
+
|
121
137
|
it 'hash with array' do
|
122
138
|
json.foo do
|
123
139
|
json.array! do |ary|
|
@@ -128,6 +144,14 @@ PRETTY_JSON
|
|
128
144
|
json.compile!.should == "{\"foo\":[1,2]}"
|
129
145
|
end
|
130
146
|
|
147
|
+
it 'hash with array by iteration' do
|
148
|
+
ary = [1,2,3]
|
149
|
+
json.foo(ary) do |n|
|
150
|
+
n * 2
|
151
|
+
end
|
152
|
+
json.compile!.should == "{\"foo\":[2,4,6]}"
|
153
|
+
end
|
154
|
+
|
131
155
|
it 'simple array with object' do
|
132
156
|
json.array! do |ary|
|
133
157
|
ary << 1
|
@@ -148,17 +172,27 @@ PRETTY_JSON
|
|
148
172
|
end
|
149
173
|
end
|
150
174
|
end
|
151
|
-
|
175
|
+
expected = "{\"foo\":{\"bar\":{\"baz\":\"goo\",\"years\":[2011,2012]}}}"
|
176
|
+
JSON.parse(json.compile!).should == JSON.parse(expected)
|
152
177
|
end
|
153
178
|
end
|
154
179
|
|
155
180
|
describe 'without blocks' do
|
181
|
+
|
156
182
|
describe 'complex array' do
|
157
183
|
it 'should work' do
|
158
184
|
json.bar [1,2,{:foo => 'goo'}]
|
159
185
|
json.compile!.should == "{\"bar\":[1,2,{\"foo\":\"goo\"}]}"
|
160
186
|
end
|
161
187
|
end
|
188
|
+
|
189
|
+
describe 'object with null' do
|
190
|
+
it 'should handle missing argument' do
|
191
|
+
json.foo
|
192
|
+
json.compile!.should == '{"foo":null}'
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
162
196
|
end
|
163
197
|
|
164
198
|
describe 'super complex example' do
|
@@ -175,34 +209,28 @@ PRETTY_JSON
|
|
175
209
|
json.fname 'George'
|
176
210
|
json.lname 'Burdell'
|
177
211
|
end
|
178
|
-
json.links do
|
179
|
-
|
180
|
-
links.each do |link|
|
181
|
-
ary << { href: link.url, rel: link.type}
|
182
|
-
end
|
183
|
-
end
|
212
|
+
json.links(links) do |link|
|
213
|
+
{ :href => link.url, :rel => link.type}
|
184
214
|
end
|
185
215
|
end
|
186
216
|
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
|
187
|
-
json.compile
|
217
|
+
JSON.parse(json.compile!).should == JSON.parse(expected)
|
188
218
|
end
|
189
219
|
|
190
|
-
|
191
|
-
|
192
|
-
json.
|
193
|
-
json.
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
json.links do
|
198
|
-
|
199
|
-
{ href: link.url, rel: link.type}
|
200
|
-
end
|
220
|
+
it "should work using map! with argument" do
|
221
|
+
json.result do
|
222
|
+
json.person do
|
223
|
+
json.fname 'George'
|
224
|
+
json.lname 'Burdell'
|
225
|
+
end
|
226
|
+
json.links do
|
227
|
+
json.map!(links) do |link|
|
228
|
+
{ :href => link.url, :rel => link.type}
|
201
229
|
end
|
202
230
|
end
|
203
|
-
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
|
204
|
-
json.compile!.should == expected
|
205
231
|
end
|
232
|
+
expected = "{\"result\":{\"person\":{\"fname\":\"George\",\"lname\":\"Burdell\"},\"links\":[{\"href\":\"example.com\",\"rel\":\"self\"},{\"href\":\"foo.com\",\"rel\":\"parent\"}]}}"
|
233
|
+
JSON.parse(json.compile!).should == JSON.parse(expected)
|
206
234
|
end
|
207
235
|
end
|
208
236
|
|
data/spec/generate_spec.rb
CHANGED
@@ -12,7 +12,8 @@ describe Jsonify::Generate do
|
|
12
12
|
it 'should build json' 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
|
+
JSON.parse(result.evaluate).should == JSON.parse(expected)
|
16
17
|
end
|
17
18
|
|
18
19
|
describe 'complex example' do
|
@@ -27,7 +28,8 @@ describe Jsonify::Generate do
|
|
27
28
|
])
|
28
29
|
}
|
29
30
|
)
|
30
|
-
|
31
|
+
expected = "{\"links\":[{\"rel\":\"foo\",\"href\":\"goo\"},{\"rel\":\"bar\",\"href\":\"baz\"}]}"
|
32
|
+
JSON.parse(json.evaluate).should == JSON.parse(expected)
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/spec/json_value_spec.rb
CHANGED
@@ -1,15 +1,53 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Jsonify::JsonValue do
|
4
|
+
|
4
5
|
describe Jsonify::JsonPair do
|
5
6
|
let(:pair) { Jsonify::JsonPair.new('key',Jsonify::JsonString.new('value')) }
|
6
7
|
it 'should be constructed of a key and value' do
|
7
8
|
pair.key.should == 'key'
|
8
|
-
# pair.value.should ==
|
9
9
|
end
|
10
10
|
it 'should evaluate to key:value' do
|
11
11
|
pair.evaluate.should == "\"key\":\"value\""
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
|
+
describe Jsonify::JsonTrue do
|
16
|
+
it 'should have a value of true' do
|
17
|
+
Jsonify::JsonTrue.new.evaluate.should == 'true'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Jsonify::JsonFalse do
|
22
|
+
it 'should have a value of false' do
|
23
|
+
Jsonify::JsonFalse.new.evaluate.should == 'false'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Jsonify::JsonNull do
|
28
|
+
it 'should have a value of true' do
|
29
|
+
Jsonify::JsonNull.new.evaluate.should == 'null'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Jsonify::JsonNumber do
|
34
|
+
it 'should accept an integer' do
|
35
|
+
Jsonify::JsonNumber.new(1).evaluate.should == 1
|
36
|
+
end
|
37
|
+
it 'should accept a float' do
|
38
|
+
Jsonify::JsonNumber.new(1.23).evaluate.should == 1.23
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Jsonify::JsonString do
|
43
|
+
it 'should quote the value' do
|
44
|
+
Jsonify::JsonString.new('foo').evaluate.should == "\"foo\""
|
45
|
+
end
|
46
|
+
it 'should encode unicode' do
|
47
|
+
unicode = 'goober'.concat(16)
|
48
|
+
Jsonify::JsonString.new(unicode).evaluate.should == "\"goober\\u0010\""
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
15
53
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jsonify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Bill Siggelkow
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-25 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -79,6 +79,28 @@ dependencies:
|
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
81
|
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: yard
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: *id007
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: rdiscount
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
type: :development
|
102
|
+
prerelease: false
|
103
|
+
version_requirements: *id008
|
82
104
|
description: Turn Ruby objects into JSON -- correctly!
|
83
105
|
email:
|
84
106
|
- bsiggelkow@me.com
|
@@ -90,6 +112,7 @@ extra_rdoc_files: []
|
|
90
112
|
|
91
113
|
files:
|
92
114
|
- .gitignore
|
115
|
+
- .travis.yml
|
93
116
|
- Gemfile
|
94
117
|
- LICENSE
|
95
118
|
- README.md
|
@@ -121,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
121
144
|
requirements:
|
122
145
|
- - ">="
|
123
146
|
- !ruby/object:Gem::Version
|
124
|
-
hash:
|
147
|
+
hash: -2236513269003945627
|
125
148
|
segments:
|
126
149
|
- 0
|
127
150
|
version: "0"
|
@@ -130,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
153
|
requirements:
|
131
154
|
- - ">="
|
132
155
|
- !ruby/object:Gem::Version
|
133
|
-
hash:
|
156
|
+
hash: -2236513269003945627
|
134
157
|
segments:
|
135
158
|
- 0
|
136
159
|
version: "0"
|