rufus-json 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,11 @@
2
2
  = rufus-json CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-json - 1.0.5 released 2013/09/05
6
+
7
+ - add support for JrJackson on JRuby (contribution by Nando Sola)
8
+
9
+
5
10
  == rufus-json - 1.0.5 released 2013/08/03
6
11
 
7
12
  - ensure .dup stringifies keys when backend == NONE
data/CREDITS.txt CHANGED
@@ -1,5 +1,10 @@
1
1
 
2
2
  == authors
3
3
 
4
- John Mettraux - http://github.com/jmettraux
4
+ John Mettraux - https://github.com/jmettraux
5
+ Torsten Schoenebaum - https://github.com/tosch
6
+
7
+ == contributors
8
+
9
+ Nando Sola - https://github.com/nandosola - support for JrJackson
5
10
 
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+
2
+ # rufus-json
3
+
4
+ One interface for various JSON Ruby backends.
5
+
6
+ ```ruby
7
+ require 'rubygems'
8
+
9
+ # load your favourite JSON backend
10
+ require 'yajl'
11
+ #require 'json'
12
+ #require 'active_support'
13
+
14
+ require 'rufus-json' # gem install rufus-json
15
+
16
+ p Rufus::Json.decode('{"a":2,"b":true}')
17
+ p Rufus::Json.load('{"a":2,"b":true}')
18
+ # => { 'a' => 2, 'b' => true }
19
+
20
+ p Rufus::Json.encode({ 'a' => 2, 'b' => true })
21
+ p Rufus::Json.dump({ 'a' => 2, 'b' => true })
22
+ # => '{"a":2,"b":true}'
23
+ ```
24
+
25
+ If multiple libs are present, it will favour yajl-ruby and json, and then active_support. It's OK to force a backend.
26
+
27
+ ```
28
+ Rufus::Json.backend = :yajl
29
+ #Rufus::Json.backend = :json
30
+ #Rufus::Json.backend = :active
31
+ ```
32
+
33
+ To know if there is currently a backend set :
34
+
35
+ ```ruby
36
+ Rufus::Json.has_backend?
37
+ ```
38
+
39
+ It's OK to load a lib and force detection :
40
+
41
+ ```ruby
42
+ require 'json'
43
+ Rufus::Json.detect_backend
44
+
45
+ p Rufus::Json.backend
46
+ # => :json
47
+ ```
48
+
49
+
50
+ There is a dup method, it may be useful in an all JSON system (flattening stuff that will anyway get flattened later).
51
+
52
+ ```ruby
53
+ o = Rufus::Json.dup(o)
54
+ ```
55
+
56
+
57
+ ### require 'rufus-json/automatic'
58
+
59
+ ```ruby
60
+ require 'rufus-json/automatic'
61
+ ```
62
+
63
+ will require 'rufus-json' and load the first JSON lib available (in the order yajl, oj, jrjackson, active_support, json, json/pure.
64
+
65
+ It is equivalent to
66
+
67
+ ```ruby
68
+ require 'rufus-json'
69
+ Rufus::Json.load_backend
70
+ ```
71
+
72
+ (the .load_backend method accepts a list/order of backends to try).
73
+
74
+
75
+ ## issue tracker
76
+
77
+ http://github.com/jmettraux/rufus-json/issues
78
+
79
+
80
+ ## irc
81
+
82
+ irc.freenode.net #ruote
83
+
84
+
85
+ ## authors
86
+
87
+ see [CREDITS.txt](CREDITS.txt)
88
+
89
+
90
+ ## license
91
+
92
+ MIT, see [LICENSE.txt](LICENSE.txt)
93
+
data/lib/rufus/json.rb CHANGED
@@ -26,7 +26,7 @@
26
26
  module Rufus
27
27
  module Json
28
28
 
29
- VERSION = '1.0.5'
29
+ VERSION = '1.0.6'
30
30
 
31
31
  # The JSON / JSON pure decoder
32
32
  #
@@ -94,6 +94,19 @@ module Json
94
94
  ::Oj::ParseError }
95
95
  }
96
96
 
97
+ # https://github.com/guyboertje/jrjackson
98
+ #
99
+ JRJACKSON = {
100
+ :encode => lambda { |o, opts|
101
+ fix_raw_value(::JrJackson::Json.dump(syms_to_s(o))) },
102
+ :pretty_encode => lambda { |o|
103
+ fix_raw_value(::JrJackson::Json.dump(syms_to_s(o))) },
104
+ :decode => lambda { |s|
105
+ ::JrJackson::Json.load(s) },
106
+ :error => lambda {
107
+ ::JrJackson::ParseError }
108
+ }
109
+
97
110
  # The "raise an exception because there's no backend" backend
98
111
  #
99
112
  NONE = {
@@ -116,7 +129,8 @@ module Json
116
129
  #
117
130
  def self.load_backend(*order)
118
131
 
119
- order = %w[ yajl active_support json json/pure ] if order.empty?
132
+ order =
133
+ %w[ yajl oj jrjackson active_support json json/pure ] if order.empty?
120
134
 
121
135
  order.each do |lib|
122
136
  begin
@@ -134,17 +148,20 @@ module Json
134
148
  #
135
149
  def self.detect_backend
136
150
 
137
- @backend = if defined?(::Oj)
138
- OJ
139
- elsif defined?(::Yajl)
140
- YAJL
141
- elsif defined?(::JSON)
142
- JSON
143
- elsif defined?(ActiveSupport::JSON)
144
- ACTIVE_SUPPORT
145
- else
146
- NONE
147
- end
151
+ @backend =
152
+ if defined?(::JrJackson)
153
+ JRJACKSON
154
+ elsif defined?(::Oj)
155
+ OJ
156
+ elsif defined?(::Yajl)
157
+ YAJL
158
+ elsif defined?(::JSON)
159
+ JSON
160
+ elsif defined?(ActiveSupport::JSON)
161
+ ACTIVE_SUPPORT
162
+ else
163
+ NONE
164
+ end
148
165
  end
149
166
 
150
167
  detect_backend
@@ -161,7 +178,7 @@ module Json
161
178
  #
162
179
  def self.backend
163
180
 
164
- %w[ yajl json active oj none ].find { |b|
181
+ %w[ yajl json active oj jrjackson none ].find { |b|
165
182
  Rufus::Json.const_get(b.upcase) == @backend
166
183
  }.to_sym
167
184
  end
@@ -173,12 +190,13 @@ module Json
173
190
  #
174
191
  def self.backend=(b)
175
192
 
176
- b = {
177
- 'yajl' => YAJL, 'yajl-ruby' => YAJL,
178
- 'json' => JSON, 'json-pure' => JSON,
179
- 'active' => ACTIVE, 'active-support' => ACTIVE,
180
- 'oj' => OJ, 'none' => NONE
181
- }[b.to_s.gsub(/[_\/]/, '-')] if b.is_a?(String) or b.is_a?(Symbol)
193
+ b =
194
+ {
195
+ 'yajl' => YAJL, 'yajl-ruby' => YAJL,
196
+ 'json' => JSON, 'json-pure' => JSON,
197
+ 'active' => ACTIVE, 'active-support' => ACTIVE,
198
+ 'oj' => OJ, 'jrjackson' => JRJACKSON, 'none' => NONE
199
+ }[b.to_s.gsub(/[_\/]/, '-')] if b.is_a?(String) or b.is_a?(Symbol)
182
200
 
183
201
  @backend = b
184
202
  end
@@ -253,6 +271,21 @@ module Json
253
271
  o.inject({}) { |h, (k, v)| h[k.to_s] = syms_to_s(v); h }
254
272
  end
255
273
 
274
+ # Used to handle parsers that do not support raw value encoding
275
+ # (i.e. JrJackson)
276
+ #
277
+ def self.fix_raw_value(o)
278
+
279
+ case o
280
+ when FalseClass, TrueClass, Fixnum, Float
281
+ o.to_s
282
+ when NilClass
283
+ 'null'
284
+ else
285
+ o
286
+ end
287
+ end
288
+
256
289
  # Wraps parser errors during decode
257
290
  #
258
291
  class ParserError < StandardError; end
data/rufus-json.gemspec CHANGED
@@ -12,10 +12,11 @@ Gem::Specification.new do |s|
12
12
  s.email = [ 'jmettraux@gmail.com' ]
13
13
  s.homepage = 'http://github.com/jmettraux/rufus-json'
14
14
  s.rubyforge_project = 'rufus'
15
+ s.license = 'MIT'
15
16
  s.summary = 'One interface to various JSON ruby libs, with a preference for yajl.'
16
17
 
17
18
  s.description = %{
18
- One interface to various JSON ruby libs (yajl, oj, json, json_pure, json-jruby, active_support). Has a preference for yajl.
19
+ One interface to various JSON ruby libs (yajl, oj, jrjackson, json, json_pure, json-jruby, active_support). Has a preference for yajl.
19
20
  }.strip
20
21
 
21
22
  #s.files = `git ls-files`.split("\n")
@@ -25,6 +26,7 @@ One interface to various JSON ruby libs (yajl, oj, json, json_pure, json-jruby,
25
26
  '*.gemspec', '*.txt', '*.rdoc', '*.md'
26
27
  ]
27
28
 
29
+ #s.add_development_dependency 'jrjackson'
28
30
  #s.add_development_dependency 'oj'
29
31
  #s.add_development_dependency 'json'
30
32
  #s.add_development_dependency 'json_pure'
data/test/backend_test.rb CHANGED
@@ -59,8 +59,13 @@ class BackendTest < Test::Unit::TestCase
59
59
 
60
60
  r = Rufus::Json.load_backend
61
61
 
62
- assert_equal 'yajl', r
63
- assert_equal :yajl, Rufus::Json.backend
62
+ if RUBY_PLATFORM == 'ruby'
63
+ assert_equal 'yajl', r
64
+ assert_equal :yajl, Rufus::Json.backend
65
+ elsif RUBY_PLATFORM == 'jruby'
66
+ assert_equal 'active_support', r
67
+ assert_equal :active, Rufus::Json.backend
68
+ end
64
69
  end
65
70
 
66
71
  def test_load_backend_with_different_order
data/test/do_test.rb CHANGED
@@ -128,7 +128,7 @@ class DoTest < Test::Unit::TestCase
128
128
  s = Rufus::Json.pretty_encode(
129
129
  { 'a' => 'b', 'e' => [ 1, 2, 3 ], 'c' => { 'd' => true } })
130
130
 
131
- assert(s.index("\n")) if JSON_LIB != 'active_support'
131
+ assert(s.index("\n")) if JSON_LIB != 'active_support' && JSON_LIB != 'jrjackson' #no pretty encoding supported
132
132
  end
133
133
  end
134
134
 
data/test/test.rb CHANGED
@@ -25,6 +25,7 @@ end
25
25
 
26
26
  LIBS = %w[ json active_support json/pure ]
27
27
  LIBS.concat %w[ yajl oj ] if RUBY_PLATFORM != 'java'
28
+ LIBS.concat %w[ jrjackson ] if RUBY_PLATFORM == 'java'
28
29
 
29
30
  LIBS.each do |lib|
30
31
  do_test "export JSON=#{lib}; #{R} #{P}/do_test.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-08-03 00:00:00.000000000 Z
13
+ date: 2013-09-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -28,8 +28,8 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
- description: One interface to various JSON ruby libs (yajl, oj, json, json_pure, json-jruby,
32
- active_support). Has a preference for yajl.
31
+ description: One interface to various JSON ruby libs (yajl, oj, jrjackson, json, json_pure,
32
+ json-jruby, active_support). Has a preference for yajl.
33
33
  email:
34
34
  - jmettraux@gmail.com
35
35
  executables: []
@@ -50,9 +50,10 @@ files:
50
50
  - CHANGELOG.txt
51
51
  - LICENSE.txt
52
52
  - CREDITS.txt
53
- - README.rdoc
53
+ - README.md
54
54
  homepage: http://github.com/jmettraux/rufus-json
55
- licenses: []
55
+ licenses:
56
+ - MIT
56
57
  post_install_message:
57
58
  rdoc_options: []
58
59
  require_paths:
data/README.rdoc DELETED
@@ -1,100 +0,0 @@
1
-
2
- = rufus-json
3
-
4
- One interface for various JSON Ruby backends.
5
-
6
- require 'rubygems'
7
-
8
- # load your favourite JSON backend
9
- require 'yajl'
10
- #require 'json'
11
- #require 'active_support'
12
-
13
- require 'rufus-json' # gem install rufus-json
14
-
15
- p Rufus::Json.decode('{"a":2,"b":true}')
16
- p Rufus::Json.load('{"a":2,"b":true}')
17
- # => { 'a' => 2, 'b' => true }
18
-
19
- p Rufus::Json.encode({ 'a' => 2, 'b' => true })
20
- p Rufus::Json.dump({ 'a' => 2, 'b' => true })
21
- # => '{"a":2,"b":true}'
22
-
23
-
24
- If multiple libs are present, it will favour yajl-ruby and json, and then active_support. It's OK to force a backend.
25
-
26
- Rufus::Json.backend = :yajl
27
- #Rufus::Json.backend = :json
28
- #Rufus::Json.backend = :active
29
-
30
-
31
- To know if there is currently a backend set :
32
-
33
- Rufus::Json.has_backend?
34
-
35
-
36
- It's OK to load a lib and force detection :
37
-
38
- require 'json'
39
- Rufus::Json.detect_backend
40
-
41
- p Rufus::Json.backend
42
- # => :json
43
-
44
-
45
- There is a dup method, it may be useful in an all JSON system (flattening stuff that will anyway get flattened later).
46
-
47
- o = Rufus::Json.dup(o)
48
-
49
-
50
- === require 'rufus-json/automatic'
51
-
52
- require 'rufus-json/automatic'
53
-
54
- will require 'rufus-json' and load the first JSON lib available (in the order yajl, active_support, json, json/pure.
55
-
56
- It is equivalent to
57
-
58
- require 'rufus-json'
59
- Rufus::Json.load_backend
60
-
61
- (the .load_backend method accepts a list/order of backends to try).
62
-
63
-
64
- == rdoc
65
-
66
- http://rufus.rubyforge.org/rufus-json/
67
-
68
-
69
- == mailing list
70
-
71
- On the rufus-ruby list :
72
-
73
- http://groups.google.com/group/rufus-ruby
74
-
75
-
76
- == issue tracker
77
-
78
- http://github.com/jmettraux/rufus-json/issues
79
-
80
-
81
- == irc
82
-
83
- irc.freenode.net #ruote
84
-
85
-
86
- == the rest of Rufus
87
-
88
- http://rufus.rubyforge.org
89
-
90
-
91
- == authors
92
-
93
- * John Mettraux, http://jmettraux.wordpress.com/
94
- * Torsten Schoenebaum, https://github.com/tosch
95
-
96
-
97
- == license
98
-
99
- MIT
100
-