summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-06-08 18:58:47 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-06-08 18:58:57 -0700
commitb199105ae8331d07d8036b2c3d41602699155d4c (patch)
treed9ad555e3ce248d682dbd8fd721b06c3da57c271
parent81a8408b358df3710b7ebdd76bec99aeea3580ed (diff)
downloadpsych-b199105ae8331d07d8036b2c3d41602699155d4c.tar.gz
fixing Hash subclass dump and load support. fixes #14
-rw-r--r--lib/psych/visitors/to_ruby.rb52
-rw-r--r--lib/psych/visitors/yaml_tree.rb5
-rw-r--r--test/psych/test_hash.rb14
3 files changed, 48 insertions, 23 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 3f5be4d..2562816 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -120,6 +120,7 @@ module Psych
def visit_Psych_Nodes_Mapping o
return revive(Psych.load_tags[o.tag], o) if Psych.load_tags[o.tag]
+ return revive_hash({}, o) unless o.tag
case o.tag
when '!str', 'tag:yaml.org,2002:str'
@@ -183,30 +184,12 @@ module Psych
obj = revive((resolve_class(name) || Object), o)
@st[o.anchor] = obj if o.anchor
obj
- else
- hash = {}
- @st[o.anchor] = hash if o.anchor
- o.children.each_slice(2) { |k,v|
- key = accept(k)
-
- if key == '<<'
- case v
- when Nodes::Alias
- hash.merge! accept(v)
- when Nodes::Sequence
- accept(v).reverse_each do |value|
- hash.merge! value
- end
- else
- hash[key] = accept(v)
- end
- else
- hash[key] = accept(v)
- end
+ when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
+ revive_hash resolve_class($1).new, o
- }
- hash
+ else
+ revive_hash({}, o)
end
end
@@ -223,6 +206,31 @@ module Psych
end
private
+ def revive_hash hash, o
+ @st[o.anchor] = hash if o.anchor
+
+ o.children.each_slice(2) { |k,v|
+ key = accept(k)
+
+ if key == '<<'
+ case v
+ when Nodes::Alias
+ hash.merge! accept(v)
+ when Nodes::Sequence
+ accept(v).reverse_each do |value|
+ hash.merge! value
+ end
+ else
+ hash[key] = accept(v)
+ end
+ else
+ hash[key] = accept(v)
+ end
+
+ }
+ hash
+ end
+
def revive klass, node
s = klass.allocate
h = Hash[*node.children.map { |c| accept c }]
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 4019859..eef6125 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -265,7 +265,10 @@ module Psych
end
def visit_Hash o
- register(o, @emitter.start_mapping(nil, nil, true, Psych::Nodes::Mapping::BLOCK))
+ tag = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
+ implicit = !tag
+
+ register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))
o.each do |k,v|
accept k
diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb
index 3147076..4bd4edf 100644
--- a/test/psych/test_hash.rb
+++ b/test/psych/test_hash.rb
@@ -2,11 +2,25 @@ require 'psych/helper'
module Psych
class TestHash < TestCase
+ class X < Hash
+ end
+
def setup
super
@hash = { :a => 'b' }
end
+ def test_empty_subclass
+ assert_match "!ruby/hash:#{X}", Psych.dump(X.new)
+ x = Psych.load Psych.dump X.new
+ assert_equal X, x.class
+ end
+
+ def test_map
+ x = Psych.load "--- !map:#{X} { }\n"
+ assert_equal X, x.class
+ end
+
def test_self_referential
@hash['self'] = @hash
assert_cycle(@hash)