summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-10-02 14:48:55 +0200
committerFlorian Frank <flori@ping.de>2011-10-02 14:48:55 +0200
commit5dcfa3711a14a6a027ce6b2b62d9117fb78da1b7 (patch)
treefdef1e294758edde001d6449645f444e19f7f76c
parente6ada3ffde2384ff47f9e7e673a73b415846dc1f (diff)
downloadjson-5dcfa3711a14a6a027ce6b2b62d9117fb78da1b7.tar.gz
Add support for BigDecimal numbers
-rw-r--r--ext/json/ext/parser/parser.c3
-rw-r--r--lib/json/add/bigdecimal.rb21
-rwxr-xr-xtests/test_json_addition.rb6
3 files changed, 27 insertions, 3 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index d1d14c7..4ea663c 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -1618,9 +1618,6 @@ static VALUE convert_encoding(VALUE source)
* defaults to true.
* * *object_class*: Defaults to Hash
* * *array_class*: Defaults to Array
- * * *quirks_mode*: Enables quirks_mode for parser, that is for example
- * parsing single JSON values instead of documents is possible.
- *
*/
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
diff --git a/lib/json/add/bigdecimal.rb b/lib/json/add/bigdecimal.rb
new file mode 100644
index 0000000..4aafe53
--- /dev/null
+++ b/lib/json/add/bigdecimal.rb
@@ -0,0 +1,21 @@
+unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
+ require 'json'
+end
+defined?(::BigDecimal) or require 'bigdecimal'
+
+class BigDecimal
+ def self.json_create(object)
+ BigDecimal._load object['b']
+ end
+
+ def as_json(*)
+ {
+ JSON.create_id => self.class.name,
+ 'b' => _dump,
+ }
+ end
+
+ def to_json(*)
+ as_json.to_json
+ end
+end
diff --git a/tests/test_json_addition.rb b/tests/test_json_addition.rb
index 9f578a4..b9cc6d1 100755
--- a/tests/test_json_addition.rb
+++ b/tests/test_json_addition.rb
@@ -6,6 +6,7 @@ require File.join(File.dirname(__FILE__), 'setup_variant')
require 'json/add/core'
require 'json/add/complex'
require 'json/add/rational'
+require 'json/add/bigdecimal'
require 'date'
class TC_JSONAddition < Test::Unit::TestCase
@@ -171,4 +172,9 @@ class TC_JSONAddition < Test::Unit::TestCase
assert_equal Rational(2, 9), JSON(JSON(Rational(2, 9)))
assert_equal Complex(2, 9), JSON(JSON(Complex(2, 9)))
end
+
+ def test_bigdecimal
+ assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)))
+ assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)))
+ end
end