summaryrefslogtreecommitdiff
path: root/ext/psych
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-01-18 03:52:01 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-01-18 03:52:01 +0000
commit8cf05741bc4d94ba2b0aa53d876738b7fd5fc747 (patch)
treecd2c2554dfdfeedd1914c3357e95f8c67e46c96a /ext/psych
parent99840a64b6687f3ae229f779152c7a37f7046d0b (diff)
* ext/psych/lib/psych/visitors/to_ruby.rb: Added support for loading
subclasses of String with ivars * ext/psych/lib/psych/visitors/yaml_tree.rb: Added support for dumping subclasses of String with ivars * test/psych/test_string.rb: corresponding tests git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34330 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych')
-rw-r--r--ext/psych/lib/psych/visitors/to_ruby.rb18
-rw-r--r--ext/psych/lib/psych/visitors/yaml_tree.rb8
2 files changed, 22 insertions, 4 deletions
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb
index bb29c8b52d..3db67a3698 100644
--- a/ext/psych/lib/psych/visitors/to_ruby.rb
+++ b/ext/psych/lib/psych/visitors/to_ruby.rb
@@ -50,8 +50,13 @@ module Psych
case o.tag
when '!binary', 'tag:yaml.org,2002:binary'
o.value.unpack('m').first
- when '!str', 'tag:yaml.org,2002:str'
- o.value
+ when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str'
+ klass = resolve_class($1)
+ if klass
+ klass.allocate.replace o.value
+ else
+ o.value
+ end
when '!ruby/object:BigDecimal'
require 'bigdecimal'
BigDecimal._load o.value
@@ -136,9 +141,16 @@ module Psych
return revive_hash({}, o) unless o.tag
case o.tag
- when '!str', 'tag:yaml.org,2002:str'
+ when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str'
+ klass = resolve_class($1)
members = Hash[*o.children.map { |c| accept c }]
string = members.delete 'str'
+
+ if klass
+ string = klass.allocate
+ string.replace string
+ end
+
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
when /^!ruby\/array:(.*)$/
klass = resolve_class($1)
diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb
index 1e22501ad4..80af0466eb 100644
--- a/ext/psych/lib/psych/visitors/yaml_tree.rb
+++ b/ext/psych/lib/psych/visitors/yaml_tree.rb
@@ -245,9 +245,15 @@ module Psych
ivars = find_ivars o
if ivars.empty?
+ unless o.class == ::String
+ tag = "!ruby/string:#{o.class}"
+ end
@emitter.scalar str, nil, tag, plain, quote, style
else
- @emitter.start_mapping nil, '!str', false, Nodes::Mapping::BLOCK
+ maptag = '!ruby/string'
+ maptag << ":#{o.class}" unless o.class == ::String
+
+ @emitter.start_mapping nil, maptag, false, Nodes::Mapping::BLOCK
@emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY
@emitter.scalar str, nil, tag, plain, quote, style