Skip to content

Commit

Permalink
Merge pull request #533 from flori/extract-bigdecimal
Browse files Browse the repository at this point in the history
Skip BigDecimal tests when it's missing to load
  • Loading branch information
hsbt committed Jul 18, 2023
2 parents d88fea4 + 3dd36c6 commit 23f7deb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/json/add/bigdecimal.rb
Expand Up @@ -2,7 +2,10 @@
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
defined?(::BigDecimal) or require 'bigdecimal'
begin
require 'bigdecimal'
rescue LoadError
end

class BigDecimal
# Import a JSON Marshalled object.
Expand All @@ -26,4 +29,4 @@ def as_json(*)
def to_json(*args)
as_json.to_json(*args)
end
end
end if defined?(::BigDecimal)
2 changes: 1 addition & 1 deletion tests/json_addition_test.rb
Expand Up @@ -183,7 +183,7 @@ def test_rational_complex
def test_bigdecimal
assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true)
assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true)
end
end if defined?(::BigDecimal)

def test_ostruct
o = OpenStruct.new
Expand Down
7 changes: 5 additions & 2 deletions tests/json_parser_test.rb
Expand Up @@ -4,7 +4,10 @@
require 'stringio'
require 'tempfile'
require 'ostruct'
require 'bigdecimal'
begin
require 'bigdecimal'
rescue LoadError
end

class JSONParserTest < Test::Unit::TestCase
include JSON
Expand Down Expand Up @@ -113,7 +116,7 @@ def test_parse_numbers
def test_parse_bigdecimals
assert_equal(BigDecimal, JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"].class)
assert_equal(BigDecimal("0.901234567890123456789E1"),JSON.parse('{"foo": 9.01234567890123456789}', decimal_class: BigDecimal)["foo"] )
end
end if defined?(::BigDecimal)

def test_parse_string_mixed_unicode
assert_equal(["éé"], JSON.parse("[\"\\u00e9é\"]"))
Expand Down

0 comments on commit 23f7deb

Please sign in to comment.