rufus-json 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,12 @@
2
2
  = rufus-json CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-json - 0.2.4 released 2010/07/17
6
+
7
+ - Rufus::Json.pretty_encode(o) (except for active_support)
8
+ - Rufus::Json.encode(o, opts={}) options are accessible
9
+
10
+
5
11
  == rufus-json - 0.2.3 released 2010/06/29
6
12
 
7
13
  - made sure we're able to parse any JSON string
data/lib/rufus/json.rb CHANGED
@@ -26,12 +26,15 @@
26
26
  module Rufus
27
27
  module Json
28
28
 
29
- VERSION = '0.2.3'
29
+ VERSION = '0.2.4'
30
30
 
31
31
  # The JSON / JSON pure decoder
32
32
  #
33
33
  JSON = [
34
- lambda { |o| o.to_json },
34
+ lambda { |o, opts|
35
+ opts[:max_nesting] = false unless opts.has_key?(:max_nesting)
36
+ ::JSON.generate(o, opts)
37
+ },
35
38
  lambda { |s| ::JSON.parse("[#{s}]", :max_nesting => nil).first },
36
39
  lambda { ::JSON::ParserError }
37
40
  ]
@@ -39,7 +42,7 @@ module Json
39
42
  # The Rails ActiveSupport::JSON decoder
40
43
  #
41
44
  ACTIVE_SUPPORT = [
42
- lambda { |o| ActiveSupport::JSON.encode(o) },
45
+ lambda { |o, opts| ActiveSupport::JSON.encode(o, opts) },
43
46
  lambda { |s| decode_e(s) || ActiveSupport::JSON.decode(s) },
44
47
  #lambda { ::ActiveSupport::JSON::ParseError }
45
48
  lambda { RuntimeError }
@@ -49,7 +52,7 @@ module Json
49
52
  # http://github.com/brianmario/yajl-ruby/
50
53
  #
51
54
  YAJL = [
52
- lambda { |o| Yajl::Encoder.encode(o) },
55
+ lambda { |o, opts| Yajl::Encoder.encode(o, opts) },
53
56
  lambda { |s| Yajl::Parser.parse(s) },
54
57
  lambda { ::Yajl::ParseError }
55
58
  ]
@@ -57,7 +60,7 @@ module Json
57
60
  # The "raise an exception because there's no backend" backend
58
61
  #
59
62
  NONE = [
60
- lambda { |s| raise 'no JSON backend found' },
63
+ lambda { |o, opts| raise 'no JSON backend found' },
61
64
  lambda { |s| raise 'no JSON backend found' },
62
65
  lambda { raise 'no JSON backend found' }
63
66
  ]
@@ -103,28 +106,42 @@ module Json
103
106
  #
104
107
  def self.backend= (b)
105
108
 
106
- b = { :yajl => YAJL, :json => JSON, :active => ACTIVE, :none => NONE }[b] \
107
- if b.is_a?(Symbol)
109
+ if b.is_a?(Symbol)
110
+ b = { :yajl => YAJL, :json => JSON, :active => ACTIVE, :none => NONE }[b]
111
+ end
108
112
 
109
113
  @backend = b
110
114
  end
111
115
 
112
- def self.encode (o)
116
+ # Encodes the given object to a JSON string.
117
+ #
118
+ def self.encode (o, opts={})
113
119
 
114
- @backend[0].call(o)
120
+ @backend[0].call(o, opts)
121
+ end
122
+
123
+ # Pretty encoding
124
+ #
125
+ def self.pretty_encode (o)
126
+
127
+ case @backend
128
+ when JSON
129
+ encode(o, :indent => ' ', :object_nl => "\n", :array_nl => "\n", :space => ' ')
130
+ when YAJL
131
+ encode(o, :pretty => true, :indent => ' ')
132
+ else
133
+ encode(o)
134
+ end
115
135
  end
116
136
 
117
137
  # Decodes the given JSON string.
118
138
  #
119
139
  def self.decode (s)
120
140
 
121
- begin
141
+ @backend[1].call(s)
122
142
 
123
- @backend[1].call(s)
124
-
125
- rescue @backend[2].call => e
126
- raise ParserError, e.message
127
- end
143
+ rescue @backend[2].call => e
144
+ raise ParserError.new(e.message)
128
145
  end
129
146
 
130
147
  # Duplicates an object by turning it into JSON and back.
data/rufus-json.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rufus-json}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Mettraux", "Torsten Schoenebaum"]
12
- s.date = %q{2010-06-29}
12
+ s.date = %q{2010-07-17}
13
13
  s.description = %q{
14
14
  One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, active_support). Has a preference for yajl.
15
15
  }
@@ -27,6 +27,8 @@ One interface to various JSON ruby libs (yajl, json, json_pure, json-jruby, acti
27
27
  "lib/rufus-json.rb",
28
28
  "lib/rufus/json.rb",
29
29
  "rufus-json.gemspec",
30
+ "test/backend_test.rb",
31
+ "test/do_test.rb",
30
32
  "test/nesting20.rb",
31
33
  "test/test.rb"
32
34
  ]
@@ -0,0 +1,49 @@
1
+
2
+ #
3
+ # testing rufus-json
4
+ #
5
+ # Fri Jul 31 13:05:37 JST 2009
6
+ #
7
+
8
+ require 'test/unit'
9
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'rufus/json'
11
+ require 'rubygems'
12
+
13
+
14
+ class BackendTest < Test::Unit::TestCase
15
+
16
+ def setup
17
+ Rufus::Json.backend = Rufus::Json::NONE
18
+ end
19
+ #def teardown
20
+ #end
21
+
22
+ def test_no_backend
23
+
24
+ assert_raise RuntimeError do
25
+ Rufus::Json.decode('nada')
26
+ end
27
+ end
28
+
29
+ def test_get_backend
30
+
31
+ assert_equal :none, Rufus::Json.backend
32
+
33
+ require 'json'
34
+
35
+ Rufus::Json.detect_backend
36
+
37
+ assert_not_equal :none, Rufus::Json.backend
38
+ end
39
+
40
+ def test_set_backend
41
+
42
+ require 'json'
43
+
44
+ Rufus::Json.backend = :json
45
+
46
+ assert_equal :json, Rufus::Json.backend
47
+ end
48
+ end
49
+
data/test/do_test.rb ADDED
@@ -0,0 +1,110 @@
1
+
2
+ #
3
+ # testing rufus-json
4
+ #
5
+ # Fri Jul 31 13:05:37 JST 2009
6
+ #
7
+
8
+ require 'test/unit'
9
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'rufus/json'
11
+ require 'rubygems'
12
+
13
+ JSON_LIB = ARGV[0]
14
+ require JSON_LIB
15
+
16
+ if JSON_LIB == 'active_support'
17
+ Rufus::Json.backend = :active
18
+ else
19
+ Rufus::Json.detect_backend
20
+ end
21
+
22
+ puts
23
+ puts ' ' + Rufus::Json.backend.to_s.upcase
24
+ puts
25
+
26
+
27
+ class DoTest < Test::Unit::TestCase
28
+
29
+ #def setup
30
+ #end
31
+
32
+ def test_backend
33
+
34
+ target = JSON_LIB.to_sym
35
+ target = :active if target == :active_support
36
+
37
+ assert_equal target, Rufus::Json.backend
38
+ end
39
+
40
+ def test_decode
41
+
42
+ assert_equal [ 1, 2, 3 ], Rufus::Json.decode("[ 1, 2, 3 ]")
43
+ end
44
+
45
+ def test_encode
46
+
47
+ assert_equal "[1,2,3]", Rufus::Json.encode([ 1, 2, 3 ])
48
+ end
49
+
50
+ def test_dup
51
+
52
+ d0 = { 'id' => 'nada' }
53
+ d1 = { :id => :nada }
54
+
55
+ assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d0))
56
+ assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d1))
57
+ end
58
+
59
+ def test_deep_nesting
60
+
61
+ s = "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{}}}}}}}}}}}}}}}}}}}}}}"
62
+
63
+ h = {}
64
+ p = h
65
+ (1..21).each do |i|
66
+ p['a'] = {}
67
+ p = p['a']
68
+ end
69
+
70
+ assert_equal(s, Rufus::Json.encode(h))
71
+ assert_equal(h, Rufus::Json.decode(s))
72
+ end
73
+
74
+ def test_parser_error
75
+
76
+ return if Rufus::Json.backend == :active
77
+
78
+ s = '{foo:cx1234}'
79
+
80
+ assert_raise Rufus::Json::ParserError do
81
+ p Rufus::Json.decode(s)
82
+ end
83
+ end
84
+
85
+ def test_json_atoms
86
+
87
+ [
88
+ [ '1', 1 ],
89
+ [ '1.1', 1.1 ],
90
+ [ '1.1e10', 1.1e10 ],
91
+ [ '1.1E10', 1.1e10 ],
92
+ [ '1.1E-10', 1.1e-10 ],
93
+ [ '"a"', 'a' ],
94
+ [ 'true', true ],
95
+ [ 'false', false ],
96
+ [ 'null', nil ]
97
+ ].each do |s, v|
98
+ assert_equal v, Rufus::Json.decode(s)
99
+ end
100
+ end
101
+
102
+ def test_pretty_encode
103
+
104
+ s = Rufus::Json.pretty_encode(
105
+ { 'a' => 'b', 'e' => [ 1, 2, 3 ], 'c' => { 'd' => true } })
106
+
107
+ assert(s.index("\n")) if JSON_LIB != 'active_support'
108
+ end
109
+ end
110
+
data/test/test.rb CHANGED
@@ -2,222 +2,23 @@
2
2
  #
3
3
  # testing rufus-json
4
4
  #
5
- # Fri Jul 31 13:05:37 JST 2009
5
+ # Sat Jul 17 14:38:44 JST 2010
6
6
  #
7
7
 
8
- require 'test/unit'
9
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
- require 'rufus/json'
11
8
  require 'rubygems'
12
9
 
10
+ R = `which ruby`.strip
11
+ P = File.dirname(__FILE__)
13
12
 
14
- class JsonTest < Test::Unit::TestCase
15
-
16
- def setup
17
- Rufus::Json.backend = Rufus::Json::NONE
18
- end
19
- #def teardown
20
- #end
21
-
22
- def test_no_backend
23
-
24
- assert_raise RuntimeError do
25
- Rufus::Json.decode('nada')
26
- end
27
- end
28
-
29
- def test_get_backend
30
-
31
- assert_equal :none, Rufus::Json.backend
32
-
33
- require 'json'
34
-
35
- Rufus::Json.detect_backend
36
-
37
- assert_not_equal :none, Rufus::Json.backend
38
- end
39
-
40
- def test_set_backend
41
-
42
- require 'json'
43
-
44
- Rufus::Json.backend = :json
45
-
46
- assert_equal :json, Rufus::Json.backend
47
- end
48
-
49
- def test_decode_json
50
-
51
- do_test_decode('json', Rufus::Json::JSON)
52
- end
53
-
54
- def test_encode_json
55
-
56
- do_test_encode('json', Rufus::Json::JSON)
57
- end
58
-
59
- def test_dup_json
60
-
61
- do_test_dup('json', Rufus::Json::JSON)
62
- end
63
-
64
- def test_deep_nesting_json
65
-
66
- do_test_deep_nesting('json', Rufus::Json::JSON)
67
- end
68
-
69
- def test_parser_error_json
70
-
71
- do_test_parser_error('json', Rufus::Json::JSON)
72
- end
73
-
74
- def test_json_atoms_json
75
-
76
- do_test_json_atoms('json', Rufus::Json::JSON)
77
- end
78
-
79
- def test_decode_yajl
80
-
81
- do_test_decode('yajl', Rufus::Json::YAJL)
82
- end
83
-
84
- def test_encode_yajl
85
-
86
- do_test_encode('yajl', Rufus::Json::YAJL)
87
- end
88
-
89
- def test_dup_yajl
90
-
91
- do_test_dup('yajl', Rufus::Json::YAJL)
92
- end
93
-
94
- def test_deep_nesting_yajl
95
-
96
- do_test_deep_nesting('yajl', Rufus::Json::YAJL)
97
- end
98
-
99
- def test_parser_error_yajl
100
-
101
- do_test_parser_error('yajl', Rufus::Json::YAJL)
102
- end
103
-
104
- def test_json_atoms_yajl
105
-
106
- do_test_json_atoms('yajl', Rufus::Json::YAJL)
107
- end
108
-
109
- def test_decode_as
110
-
111
- do_test_decode('active_support', Rufus::Json::ACTIVE)
112
- end
113
-
114
- def test_encode_as
115
-
116
- do_test_encode('active_support', Rufus::Json::ACTIVE)
117
- end
118
-
119
- def test_dup_as
120
-
121
- do_test_dup('active_support', Rufus::Json::ACTIVE)
122
- end
123
-
124
- def test_deep_nesting_as
125
-
126
- do_test_deep_nesting('active_support', Rufus::Json::ACTIVE)
127
- end
128
-
129
- def test_json_atoms_as
130
-
131
- do_test_json_atoms('active_support', Rufus::Json::ACTIVE)
132
- end
133
-
134
- protected
135
-
136
- def do_test_decode (lib, cons)
137
-
138
- require lib
139
-
140
- assert_raise RuntimeError do
141
- Rufus::Json.decode('nada')
142
- end
143
-
144
- Rufus::Json.backend = cons
145
- assert_equal [ 1, 2, 3 ], Rufus::Json.decode("[ 1, 2, 3 ]")
146
- end
147
-
148
- def do_test_encode (lib, cons)
149
-
150
- require lib
151
-
152
- assert_raise RuntimeError do
153
- Rufus::Json.encode('nada')
154
- end
155
-
156
- Rufus::Json.backend = cons
157
- assert_equal "[1,2,3]", Rufus::Json.encode([ 1, 2, 3 ])
158
- end
159
-
160
- def do_test_dup (lib, cons)
161
-
162
- require lib
163
-
164
- d0 = { 'id' => 'nada' }
165
- d1 = { :id => :nada }
166
-
167
- Rufus::Json.backend = cons
168
-
169
- assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d0))
170
- assert_equal({ 'id' => 'nada' }, Rufus::Json.dup(d1))
171
- end
172
-
173
- def do_test_deep_nesting (lib, cons)
174
-
175
- require lib
176
- Rufus::Json.backend = cons
177
-
178
- s = "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{}}}}}}}}}}}}}}}}}}}}}}"
179
-
180
- h = {}
181
- p = h
182
- (1..21).each do |i|
183
- p['a'] = {}
184
- p = p['a']
185
- end
186
-
187
- assert_equal(s, Rufus::Json.encode(h))
188
- assert_equal(h, Rufus::Json.decode(s))
189
- end
190
-
191
- def do_test_parser_error (lib, cons)
192
-
193
- require lib
194
- Rufus::Json.backend = cons
195
-
196
- s = '{foo:cx1234}'
197
-
198
- assert_raise Rufus::Json::ParserError do
199
- Rufus::Json.decode(s)
200
- end
201
- end
202
-
203
- def do_test_json_atoms (lib, cons)
204
-
205
- require lib
206
- Rufus::Json.backend = cons
207
-
208
- [
209
- [ '1', 1 ],
210
- [ '1.1', 1.1 ],
211
- [ '1.1e10', 1.1e10 ],
212
- [ '1.1E10', 1.1e10 ],
213
- [ '1.1E-10', 1.1e-10 ],
214
- [ '"a"', 'a' ],
215
- [ 'true', true ],
216
- [ 'false', false ],
217
- [ 'null', nil ]
218
- ].each do |s, v|
219
- assert_equal v, Rufus::Json.decode(s)
220
- end
221
- end
13
+ %w[ json active_support yajl ].each do |lib|
14
+ puts
15
+ puts '-' * 80
16
+ puts "#{R} #{P}/do_test.rb #{lib}"
17
+ puts `#{R} #{P}/do_test.rb #{lib}`
222
18
  end
223
19
 
20
+ puts
21
+ puts '-' * 80
22
+ puts "#{R} #{P}/backend_test.rb"
23
+ puts `#{R} #{P}/backend_test.rb`
24
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 3
9
- version: 0.2.3
8
+ - 4
9
+ version: 0.2.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mettraux
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-29 00:00:00 +09:00
18
+ date: 2010-07-17 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -109,6 +109,8 @@ files:
109
109
  - lib/rufus-json.rb
110
110
  - lib/rufus/json.rb
111
111
  - rufus-json.gemspec
112
+ - test/backend_test.rb
113
+ - test/do_test.rb
112
114
  - test/nesting20.rb
113
115
  - test/test.rb
114
116
  has_rdoc: true