module Psych
def self.add_builtin_type type_tag, &block
def self.add_builtin_type type_tag, &block domain = 'yaml.org,2002' key = ['tag', domain, type_tag].join ':' @domain_types[key] = [key, block] end
def self.add_domain_type domain, type_tag, &block
def self.add_domain_type domain, type_tag, &block key = ['tag', domain, type_tag].join ':' @domain_types[key] = [key, block] @domain_types["tag:#{type_tag}"] = [key, block] end
def self.add_private_type type_tag, &block
def self.add_private_type type_tag, &block warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE domain = 'x-private' key = [domain, type_tag].join ':' @domain_types[key] = [key, block] end
def self.add_ruby_type type_tag, &block
def self.add_ruby_type type_tag, &block warn "#{caller[0]}: add_ruby_type is deprecated, use add_domain_type" if $VERBOSE domain = 'ruby.yaml.org,2002' key = ['tag', domain, type_tag].join ':' @domain_types[key] = [key, block] end
def self.add_tag tag, klass
def self.add_tag tag, klass @load_tags[tag] = klass.name @dump_tags[klass] = tag end
def self.detect_implicit thing
def self.detect_implicit thing warn "#{caller[0]}: detect_implicit is deprecated" if $VERBOSE return '' unless String === thing return 'null' if '' == thing ss = ScalarScanner.new(ClassLoader.new) ss.tokenize(thing).class.name.downcase end
def self.dump o, io = nil, options = {}
# Dump an array to an IO with indentation set
Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
# Dump an array with indentation set
Psych.dump(['a', 'b'], StringIO.new) # => #
# Dump an array to an IO object
Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
# Dump an array, get back a YAML string
Example:
be dumped to that IO object.
to control the output format. If an IO object is passed in, the YAML will
Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
Psych.dump(o, io, options) -> io object passed in
Psych.dump(o, io) -> io object passed in
Psych.dump(o, options) -> string of yaml
Psych.dump(o) -> string of yaml
call-seq:
##
def self.dump o, io = nil, options = {} if Hash === io options = io io = nil end visitor = Psych::Visitors::YAMLTree.create options visitor << o visitor.tree.yaml io, options end
def self.dump_stream *objects
Example:
Dump a list of objects as separate documents to a document stream.
##
def self.dump_stream *objects visitor = Psych::Visitors::YAMLTree.create({}) objects.each do |o| visitor << o end visitor.tree.yaml end
def self.load yaml, filename = nil
ex.message # => "(file.txt): found character that cannot start any token"
ex.file # => 'file.txt'
rescue Psych::SyntaxError => ex
Psych.load("--- `", "file.txt")
begin
Psych.load("---\n - a\n - b") # => ['a', 'b']
Psych.load("--- a") # => 'a'
Example:
Raises a Psych::SyntaxError when a YAML syntax error is detected.
while parsing.
+filename+ will be used in the exception message if any exception is raised
provided, the object contained in the first document will be returned.
Load +yaml+ in to a Ruby data structure. If multiple documents are
##
def self.load yaml, filename = nil result = parse(yaml, filename) result ? result.to_ruby : result end
def self.load_documents yaml, &block
def self.load_documents yaml, &block if $VERBOSE warn "#{caller[0]}: load_documents is deprecated, use load_stream" end list = load_stream yaml return list unless block_given? list.each(&block) end
def self.load_file filename
Load the document contained in +filename+. Returns the yaml contained in
##
def self.load_file filename File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename } end
def self.load_stream yaml, filename = nil
list # => ['foo', 'bar']
end
list << ruby
Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby|
list = []
Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
Example:
and passed to the block during parsing
as a list. If a block is given, each document will be converted to Ruby
Load multiple documents given in +yaml+. Returns the parsed documents
##
def self.load_stream yaml, filename = nil if block_given? parse_stream(yaml, filename) do |node| yield node.to_ruby end else parse_stream(yaml, filename).children.map { |child| child.to_ruby } end end
def self.object_maker klass, hash
def self.object_maker klass, hash warn "#{caller[0]}: object_maker is deprecated" if $VERBOSE klass.allocate.tap do |obj| hash.each { |k,v| obj.instance_variable_set(:"@#{k}", v) } end end
def self.parse yaml, filename = nil
end
ex.message # => "(file.txt): found character that cannot start any token"
ex.file # => 'file.txt'
rescue Psych::SyntaxError => ex
Psych.parse("--- `", "file.txt")
begin
Psych.parse("---\n - a\n - b") # => #
Example:
Raises a Psych::SyntaxError when a YAML syntax error is detected.
raised.
+filename+ is used in the exception message if a Psych::SyntaxError is
Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
##
def self.parse yaml, filename = nil parse_stream(yaml, filename) do |node| return node end false end
def self.parse_file filename
Parse a file at +filename+. Returns the Psych::Nodes::Document.
##
def self.parse_file filename File.open filename, 'r:bom|utf-8' do |f| parse f, filename end end
def self.parse_stream yaml, filename = nil, &block
end
ex.message # => "(file.txt): found character that cannot start any token"
ex.file # => 'file.txt'
rescue Psych::SyntaxError => ex
Psych.parse_stream("--- `", "file.txt")
begin
end
node # => #
Psych.parse_stream("--- a\n--- b") do |node|
Psych.parse_stream("---\n - a\n - b") # => #
Example:
Raises a Psych::SyntaxError when a YAML syntax error is detected.
block as it's being parsed.
If a block is given, a Psych::Nodes::Document node will be yielded to the
raised.
+filename+ is used in the exception message if a Psych::SyntaxError is
This method can handle multiple YAML documents contained in +yaml+.
Parse a YAML string in +yaml+. Returns the Psych::Nodes::Stream.
##
def self.parse_stream yaml, filename = nil, &block if block_given? parser = Psych::Parser.new(Handlers::DocumentStream.new(&block)) parser.parse yaml, filename else parser = self.parser parser.parse yaml, filename parser.handler.root end end
def self.parser
##
def self.parser Psych::Parser.new(TreeBuilder.new) end
def self.quick_emit thing, opts = {}, &block # :nodoc:
def self.quick_emit thing, opts = {}, &block # :nodoc: warn "#{caller[0]}: YAML.quick_emit is deprecated" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__)) target = eval 'self', block.binding target.extend DeprecatedMethods metaclass = class << target; self; end metaclass.send(:define_method, :encode_with) do |coder| target.taguri = coder.tag target.to_yaml_style = coder.style block.call coder end target.psych_to_yaml unless opts[:nodump] end
def self.read_type_class type, reference
def self.read_type_class type, reference warn "#{caller[0]}: read_type_class is deprecated" if $VERBOSE _, _, type, name = type.split ':', 4 reference = name.split('::').inject(reference) do |k,n| k.const_get(n.to_sym) end if name [type, reference] end
def self.remove_type type_tag
def self.remove_type type_tag @domain_types.delete type_tag end
def self.safe_load yaml, whitelist_classes = [], whitelist_symbols = [], aliases = false, filename = nil
A Psych::BadAlias exception will be raised if the yaml contains aliases
class that isn't in the whitelist.
A Psych::DisallowedClass exception will be raised if the yaml contains a
Psych.safe_load yaml, [], [], true # => loads the aliases
Psych.safe_load yaml # => raises an exception
yaml = Psych.dump x
x << x
x = []
For example:
Aliases can be explicitly allowed by changing the +aliases+ parameter.
Now the Date class can be loaded in addition to the classes listed above.
Psych.safe_load(yaml, [Date])
additive. For example, to allow Date deserialization:
can be allowed by adding those classes to the +whitelist+. They are
Recursive data structures are not allowed by default. Arbitrary classes
* Hash
* Array
* String
* Numeric
* NilClass
* FalseClass
* TrueClass
classes are allowed to be deserialized:
Safely load the yaml string in +yaml+. By default, only the following
##
def self.safe_load yaml, whitelist_classes = [], whitelist_symbols = [], aliases = false, filename = nil result = parse(yaml, filename) return unless result class_loader = ClassLoader::Restricted.new(whitelist_classes.map(&:to_s), whitelist_symbols.map(&:to_s)) scanner = ScalarScanner.new class_loader if aliases visitor = Visitors::ToRuby.new scanner, class_loader else visitor = Visitors::NoAliasRuby.new scanner, class_loader end visitor.accept result end
def self.tagurize thing
def self.tagurize thing warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE return thing unless String === thing "tag:yaml.org,2002:#{thing}" end
def self.to_json object
##
def self.to_json object visitor = Psych::Visitors::JSONTree.create visitor << object visitor.tree.yaml end