Skip to content
Browse files

started to add BigDecimal, Date, and DateTime

  • Loading branch information...
1 parent 2444b99 commit 2c63491fe90015c5c891908812199d9fed450d85 Peter Ohler committed Jun 18, 2012
Showing with 67 additions and 7 deletions.
  1. +1 −5 README.md
  2. +3 −0 ext/oj/dump.c
  3. +63 −2 test/tests.rb
View
6 README.md
@@ -28,11 +28,7 @@ A fast JSON parser and Object marshaller as a Ruby gem.
- Added check for circular on loading of circular dumped JSON.
-#### old notes
-
- - Added json.rb to $" in mimic mode to avoid pulling in the real JSON by accident.
-
- - Oj is now thread safe for all functions.
+ -
## <a name="description">Description</a>
View
3 ext/oj/dump.c
@@ -938,6 +938,9 @@ dump_data_comp(VALUE obj, Out out) {
if (rb_cTime == clas) {
dump_time(obj, out);
+ // TBD BigDecimal
+ // TBD Date
+ // TBD DateTime
} else {
dump_nil(out);
}
View
65 test/tests.rb
@@ -6,6 +6,7 @@
require 'test/unit'
require 'stringio'
+require 'bigdecimal'
require 'oj'
$ruby = RUBY_DESCRIPTION.split(' ')[0]
@@ -210,6 +211,7 @@ def test_array
def test_symbol_strict
begin
json = Oj.dump(:abc, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -234,6 +236,7 @@ def test_time_strict
t = Time.local(2012, 1, 5, 23, 58, 7)
begin
json = Oj.dump(t, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -254,10 +257,11 @@ def test_time_object
dump_and_load(t, false)
end
-# Class
+ # Class
def test_class_strict
begin
json = Oj.dump(Juice, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -275,7 +279,7 @@ def test_class_object
dump_and_load(Juice, false)
end
-# Hash
+ # Hash
def test_hash
Oj.default_options = { :mode => :strict }
dump_and_load({}, false)
@@ -285,20 +289,23 @@ def test_hash
def test_non_str_hash_strict
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_null
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :null)
+ assert(false)
rescue Exception => e
assert(true)
end
end
def test_non_str_hash_compat
begin
json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
+ assert(false)
rescue Exception => e
assert(e.message.include?('Fixnum'))
assert(true)
@@ -326,6 +333,7 @@ def test_json_object_strict
obj = Jeez.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -371,6 +379,7 @@ def test_to_hash_object_strict
obj = Jazz.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -406,6 +415,7 @@ def test_as_json_object_strict
obj = Orange.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -457,6 +467,7 @@ def test_object_strict
obj = Jam.new(true, 58)
begin
json = Oj.dump(obj, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -496,6 +507,7 @@ def test_exception
err = nil
begin
raise StandardError.new('A Message')
+ assert(false)
rescue Exception => e
err = e
end
@@ -515,6 +527,7 @@ def test_exception
def test_range_strict
begin
json = Oj.dump(1..7, :mode => :strict)
+ assert(false)
rescue Exception => e
assert(true)
end
@@ -547,6 +560,54 @@ def test_range_object
end
end
+ # BigNum
+ def test_bignum_strict
+ json = Oj.dump(7 ** 55, :mode => :strict)
+ assert_equal('30226801971775055948247051683954096612865741943', json)
+ end
+ def test_bignum_null
+ json = Oj.dump(7 ** 55, :mode => :null)
+ assert_equal('30226801971775055948247051683954096612865741943', json)
+ end
+ def test_bignum_compat
+ json = Oj.dump(7 ** 55, :mode => :compat)
+ b = Oj.load(json, :mode => :strict)
+ assert_equal(30226801971775055948247051683954096612865741943, b)
+ end
+ def test_bignum_object
+ dump_and_load(7 ** 55, false)
+ end
+
+ # BigDecimal
+ def test_bigdecimal_strict
+ begin
+ json = Oj.dump(BigDecimal.new('3.14159265358979323846'), :mode => :strict)
+ assert(false)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_bigdecimal_null
+ json = Oj.dump(BigDecimal.new('3.14159265358979323846'), :mode => :null)
+ assert_equal('null', json)
+ end
+ def test_bigdecimal_compat
+ begin
+ json = Oj.dump(BigDecimal.new('3.14159265358979323846'), :mode => :compat)
+ assert(false)
+ rescue Exception => e
+ assert(true)
+ end
+ end
+ def test_bigdecimal_object
+ dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
+ dump_and_load(BigDecimal.new('Infinity'), false)
+ dump_and_load(BigDecimal.new('-Infinity'), false)
+ end
+
+ # TBD Date
+ # TBD DateTime
+
# autodefine Oj::Bag
def test_bag
json = %{{

0 comments on commit 2c63491

Please sign in to comment.
Something went wrong with that request. Please try again.