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_tag tag, klass
def self.add_tag tag, klass @load_tags[tag] = klass.name @dump_tags[klass] = tag 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:
Default: false.
[:header] Write %YAML [version] at the beginning of document.
Default: false.
strictly formal).
[:canonical] Write "canonical" YAML form (very verbose, yet
Default: 0 (meaning "wrap at 81").
[:line_width] Max character to wrap line at.
Default: 2.
otherwise option is ignored.
Acceptable value should be in 0..9 range,
[:indentation] Number of space characters used to indent.
Currently supported options are:
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, fallback: false, symbolize_names: false
Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
true value, returns symbols for keys in Hash objects (default: strings).
When the optional +symbolize_names+ keyword argument is set to a
end
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, fallback: false, symbolize_names: false result = parse(yaml, filename, fallback: fallback) result = result.to_ruby if result symbolize_names!(result) if symbolize_names result end
def self.load_file filename, fallback: false
+filename+ as a Ruby object, or if the file is empty, it returns
Load the document contained in +filename+. Returns the yaml contained in
##
def self.load_file filename, fallback: false File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename, fallback: FALLBACK.new(fallback) } 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.parse yaml, filename = nil, fallback: false
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, fallback: false parse_stream(yaml, filename) do |node| return node end fallback 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.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, symbolize_names: false
Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
true value, returns symbols for keys in Hash objects (default: strings).
When the optional +symbolize_names+ keyword argument is set to a
while parsing.
+filename+ will be used in the exception message if any exception is raised
but the +aliases+ parameter is set to false.
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, symbolize_names: false 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 result = visitor.accept result symbolize_names!(result) if symbolize_names result end
def self.symbolize_names!(result)
def self.symbolize_names!(result) case result when Hash result.keys.each do |key| result[key.to_sym] = symbolize_names!(result.delete(key)) end when Array result.map! { |r| symbolize_names!(r) } end result end
def self.to_json object
##
def self.to_json object visitor = Psych::Visitors::JSONTree.create visitor << object visitor.tree.yaml end