Skip to content

Commit

Permalink
add support for JrJackson (https://github.com/guyboertje/jrjackson), …
Browse files Browse the repository at this point in the history
…conditionally bundle platform-dependent gems
  • Loading branch information
nandosola committed Sep 4, 2013
1 parent 0f48488 commit b1987c4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
11 changes: 9 additions & 2 deletions Gemfile
Expand Up @@ -3,7 +3,14 @@ source 'https://rubygems.org'

gem 'json'
gem 'json_pure'
gem 'yajl-ruby', :require => 'yajl'
gem 'active_support'
gem 'oj'

platforms :ruby do
gem 'yajl-ruby', :require => 'yajl'
gem 'oj'
end

platforms :jruby do
gem 'jrjackson'
end

35 changes: 32 additions & 3 deletions lib/rufus/json.rb
Expand Up @@ -94,6 +94,19 @@ module Json
::Oj::ParseError }
}

# https://github.com/guyboertje/jrjackson
#
JRJACKSON = {
:encode => lambda { |o, opts|
fix_raw_value(::JrJackson::Json.dump(syms_to_s(o))) },
:pretty_encode => lambda { |o|
fix_raw_value(::JrJackson::Json.dump(syms_to_s(o))) },
:decode => lambda { |s|
::JrJackson::Json.load(s) },
:error => lambda {
::JrJackson::ParseError }
}

# The "raise an exception because there's no backend" backend
#
NONE = {
Expand Down Expand Up @@ -134,7 +147,9 @@ def self.load_backend(*order)
#
def self.detect_backend

@backend = if defined?(::Oj)
@backend = if defined?(::JrJackson)
JRJACKSON
elsif defined?(::Oj)
OJ
elsif defined?(::Yajl)
YAJL
Expand All @@ -161,7 +176,7 @@ def self.has_backend?
#
def self.backend

%w[ yajl json active oj none ].find { |b|
%w[ yajl json active oj jrjackson none ].find { |b|
Rufus::Json.const_get(b.upcase) == @backend
}.to_sym
end
Expand All @@ -177,7 +192,7 @@ def self.backend=(b)
'yajl' => YAJL, 'yajl-ruby' => YAJL,
'json' => JSON, 'json-pure' => JSON,
'active' => ACTIVE, 'active-support' => ACTIVE,
'oj' => OJ, 'none' => NONE
'oj' => OJ, 'jrjackson' => JRJACKSON, 'none' => NONE
}[b.to_s.gsub(/[_\/]/, '-')] if b.is_a?(String) or b.is_a?(Symbol)

@backend = b
Expand Down Expand Up @@ -253,6 +268,20 @@ def self.syms_to_s(o)
o.inject({}) { |h, (k, v)| h[k.to_s] = syms_to_s(v); h }
end

# Used to handle parsers that do not support raw value encoding
# (i.e. JrJackson)
def self.fix_raw_value(o)
case o
when FalseClass, TrueClass, Fixnum, Float
o.to_s
when NilClass
'null'
else
o
end

end

# Wraps parser errors during decode
#
class ParserError < StandardError; end
Expand Down
3 changes: 2 additions & 1 deletion rufus-json.gemspec
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.summary = 'One interface to various JSON ruby libs, with a preference for yajl.'

s.description = %{
One interface to various JSON ruby libs (yajl, oj, json, json_pure, json-jruby, active_support). Has a preference for yajl.
One interface to various JSON ruby libs (yajl, oj, jrjackson, json, json_pure, json-jruby, active_support). Has a preference for yajl.
}.strip

#s.files = `git ls-files`.split("\n")
Expand All @@ -26,6 +26,7 @@ One interface to various JSON ruby libs (yajl, oj, json, json_pure, json-jruby,
'*.gemspec', '*.txt', '*.rdoc', '*.md'
]

#s.add_development_dependency 'jrjackson'
#s.add_development_dependency 'oj'
#s.add_development_dependency 'json'
#s.add_development_dependency 'json_pure'
Expand Down
9 changes: 7 additions & 2 deletions test/backend_test.rb
Expand Up @@ -59,8 +59,13 @@ def test_load_backend

r = Rufus::Json.load_backend

assert_equal 'yajl', r
assert_equal :yajl, Rufus::Json.backend
if RUBY_PLATFORM == 'ruby'
assert_equal 'yajl', r
assert_equal :yajl, Rufus::Json.backend
elsif RUBY_PLATFORM == 'jruby'
assert_equal 'active_support', r
assert_equal :active, Rufus::Json.backend
end
end

def test_load_backend_with_different_order
Expand Down
2 changes: 1 addition & 1 deletion test/do_test.rb
Expand Up @@ -128,7 +128,7 @@ def test_pretty_encode
s = Rufus::Json.pretty_encode(
{ 'a' => 'b', 'e' => [ 1, 2, 3 ], 'c' => { 'd' => true } })

assert(s.index("\n")) if JSON_LIB != 'active_support'
assert(s.index("\n")) if JSON_LIB != 'active_support' && JSON_LIB != 'jrjackson' #no pretty encoding supported
end
end

1 change: 1 addition & 0 deletions test/test.rb
Expand Up @@ -25,6 +25,7 @@ def do_test(command)

LIBS = %w[ json active_support json/pure ]
LIBS.concat %w[ yajl oj ] if RUBY_PLATFORM != 'java'
LIBS.concat %w[ jrjackson ] if RUBY_PLATFORM == 'java'

LIBS.each do |lib|
do_test "export JSON=#{lib}; #{R} #{P}/do_test.rb"
Expand Down

0 comments on commit b1987c4

Please sign in to comment.