summaryrefslogtreecommitdiff
path: root/ext/psych/lib/psych/visitors/to_ruby.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/psych/lib/psych/visitors/to_ruby.rb')
-rw-r--r--ext/psych/lib/psych/visitors/to_ruby.rb36
1 files changed, 20 insertions, 16 deletions
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb
index ec80701917..f0fda9bdbc 100644
--- a/ext/psych/lib/psych/visitors/to_ruby.rb
+++ b/ext/psych/lib/psych/visitors/to_ruby.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
-require 'psych/scalar_scanner'
-require 'psych/class_loader'
-require 'psych/exception'
+require_relative '../scalar_scanner'
+require_relative '../class_loader'
+require_relative '../exception'
unless defined?(Regexp::NOENCODING)
Regexp::NOENCODING = 32
@@ -12,9 +12,9 @@ module Psych
###
# This class walks a YAML AST, converting each node to Ruby
class ToRuby < Psych::Visitors::Visitor
- def self.create(symbolize_names: false, freeze: false)
+ def self.create(symbolize_names: false, freeze: false, strict_integer: false)
class_loader = ClassLoader.new
- scanner = ScalarScanner.new class_loader
+ scanner = ScalarScanner.new class_loader, strict_integer: strict_integer
new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze)
end
@@ -24,6 +24,7 @@ module Psych
super()
@st = {}
@ss = ss
+ @load_tags = Psych.load_tags
@domain_types = Psych.domain_types
@class_loader = class_loader
@symbolize_names = symbolize_names
@@ -48,7 +49,7 @@ module Psych
end
def deserialize o
- if klass = resolve_class(Psych.load_tags[o.tag])
+ if klass = resolve_class(@load_tags[o.tag])
instance = klass.allocate
if instance.respond_to?(:init_with)
@@ -79,7 +80,9 @@ module Psych
when "!ruby/object:DateTime"
class_loader.date_time
require 'date' unless defined? DateTime
- @ss.parse_time(o.value).to_datetime
+ t = @ss.parse_time(o.value)
+ DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
+ (t.subsec/86400)
when '!ruby/encoding'
::Encoding.find o.value
when "!ruby/object:Complex"
@@ -98,7 +101,7 @@ module Psych
source = $1
options = 0
lang = nil
- ($2 || '').split('').each do |option|
+ $2&.each_char do |option|
case option
when 'x' then options |= Regexp::EXTENDED
when 'i' then options |= Regexp::IGNORECASE
@@ -128,7 +131,7 @@ module Psych
end
def visit_Psych_Nodes_Sequence o
- if klass = resolve_class(Psych.load_tags[o.tag])
+ if klass = resolve_class(@load_tags[o.tag])
instance = klass.allocate
if instance.respond_to?(:init_with)
@@ -160,8 +163,8 @@ module Psych
end
def visit_Psych_Nodes_Mapping o
- if Psych.load_tags[o.tag]
- return revive(resolve_class(Psych.load_tags[o.tag]), o)
+ if @load_tags[o.tag]
+ return revive(resolve_class(@load_tags[o.tag]), o)
end
return revive_hash(register(o, {}), o) unless o.tag
@@ -322,10 +325,11 @@ module Psych
end
def visit_Psych_Nodes_Alias o
- @st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" }
+ @st.fetch(o.anchor) { raise AnchorNotDefined, o.anchor }
end
private
+
def register node, object
@st[node.anchor] = object if node.anchor
object
@@ -337,7 +341,7 @@ module Psych
list
end
- def revive_hash hash, o
+ def revive_hash hash, o, tagged= false
o.children.each_slice(2) { |k,v|
key = accept(k)
val = accept(v)
@@ -364,7 +368,7 @@ module Psych
hash[key] = val
end
else
- if @symbolize_names
+ if !tagged && @symbolize_names && key.is_a?(String)
key = key.to_sym
elsif !@freeze
key = deduplicate(key)
@@ -402,7 +406,7 @@ module Psych
def revive klass, node
s = register(node, klass.allocate)
- init_with(s, revive_hash({}, node), node)
+ init_with(s, revive_hash({}, node, true), node)
end
def init_with o, h, node
@@ -425,7 +429,7 @@ module Psych
class NoAliasRuby < ToRuby
def visit_Psych_Nodes_Alias o
- raise BadAlias, "Unknown alias: #{o.anchor}"
+ raise AliasesNotEnabled
end
end
end