Skip to content

Commit

Permalink
* ext/psych/lib/psych/visitors/to_ruby.rb: BigDecimals can be restored
Browse files Browse the repository at this point in the history
  from YAML.
* ext/psych/lib/psych/visitors/yaml_tree.rb: BigDecimals can be dumped
  to YAML.
* test/psych/test_numeric.rb: tests for BigDecimal serialization

Fixes #31
  • Loading branch information
tenderlove committed Dec 18, 2011
1 parent 1caecdc commit 33ce865
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rdoc
@@ -1,3 +1,11 @@
Sun Dec 18 12:42:48 2011 Aaron Patterson <aaron@tenderlovemaking.com>

* ext/psych/lib/psych/visitors/to_ruby.rb: BigDecimals can be restored
from YAML.
* ext/psych/lib/psych/visitors/yaml_tree.rb: BigDecimals can be dumped
to YAML.
* test/psych/test_numeric.rb: tests for BigDecimal serialization

Sun Dec 18 12:03:13 2011 Aaron Patterson <aaron@tenderlovemaking.com>

* ext/psych/lib/psych/scalar_scanner.rb: Strings that look like dates
Expand Down
3 changes: 3 additions & 0 deletions lib/psych/visitors/to_ruby.rb
Expand Up @@ -52,6 +52,9 @@ def deserialize o
o.value.unpack('m').first
when '!str', 'tag:yaml.org,2002:str'
o.value
when '!ruby/object:BigDecimal'
require 'bigdecimal'
BigDecimal._load o.value
when "!ruby/object:DateTime"
require 'date'
@ss.parse_time(o.value).to_datetime
Expand Down
4 changes: 4 additions & 0 deletions lib/psych/visitors/yaml_tree.rb
Expand Up @@ -214,6 +214,10 @@ def visit_Float o
end
end

def visit_BigDecimal o
@emitter.scalar o._dump, nil, '!ruby/object:BigDecimal', false, false, Nodes::Scalar::ANY
end

def binary? string
string.encoding == Encoding::ASCII_8BIT ||
string.index("\x00") ||
Expand Down
11 changes: 11 additions & 0 deletions test/psych/test_numeric.rb
@@ -1,4 +1,5 @@
require 'psych/helper'
require 'bigdecimal'

module Psych
###
Expand All @@ -10,5 +11,15 @@ def test_non_float_with_0
str = Psych.load('--- 090')
assert_equal '090', str
end

def test_big_decimal_tag
decimal = BigDecimal("12.34")
assert_match "!ruby/object:BigDecimal", Psych.dump(decimal)
end

def test_big_decimal_round_trip
decimal = BigDecimal("12.34")
assert_cycle decimal
end
end
end

0 comments on commit 33ce865

Please sign in to comment.