API Reference
YAML parser
LibYAML2.ParserYAML.parse_yaml — Functionparse_yaml(yaml_str::String; kw...)
parse_yaml(yaml_str::Vector{UInt8}; kw...)Parse a YAML string or file (or vector of UInt8) and returns a dictionary, vector or nothing.
- If a given YAML document contains a dictionary, the parser returns a dictionary.
- If a given YAML document contains just a list of variables, the parser returns a vector.
- If a given YAML document contains no information (i.e. empty), the parser returns nothing.
If the input contains multiple documents (multi-document YAML), behavior depends on the keyword argument multi:
- If
multi = false(default), only the first document is returned. - If
multi = true, all documents are parsed and returned as aVector, preserving their individual types (dictionary, vector, or nothing).
Keyword arguments
multi::Bool = false: If YAML is multidocumental, allows the reader to choose between obtaining all documents at once or only the first one.dict_type = Dict{Any,Any}: The type of the dictionary to return.
Examples
julia> yaml_str = """
name: Alice
array:
- 1
- 2
- a: 3
b: null
dict:
a: 1
b:
- w
- d
""";
julia> parse_yaml(yaml_str)
Dict{Any, Any}(
"dict" => Dict{Any, Any}(
"b" => Any["w", "d"],
"a" => "1"
),
"name" => "Alice",
"array" => Any["1", "2", Dict{Any, Any}("b" => nothing, "a" => "3")]
)
julia> yaml_str = """
---
name: Alice
array:
- 1
- 2
- a: 3
b: null
dict:
a: 1
b:
- w
- d
---
name: John
array:
- 1
- 2
- a: 3
b: null
dict:
a: 1
b:
- w
- d
""";
julia> parse_yaml(yaml_str)
Dict{Any, Any} with 3 entries:
"dict" => Dict{Any, Any}("b"=>Any["w", "d"], "a"=>"1")
"name" => "Alice"
"array" => Any["1", "2", Dict{Any, Any}("b"=>nothing, "a"=>"3")]
julia> parse_yaml(yaml_str, multi=true)
2-element Vector{Any}:
Dict{Any, Any}("dict" => Dict{Any, Any}("b" => Any["w", "d"], "a" => "1"), "name" => "Alice", "array" => Any["1", "2", Dict{Any, Any}("b" => nothing, "a" => "3")])
Dict{Any, Any}("dict" => Dict{Any, Any}("b" => Any["w", "d"], "a" => "1"), "name" => "John", "array" => Any["1", "2", Dict{Any, Any}("b" => nothing, "a" => "3")])LibYAML2.ParserYAML.open_yaml — Functionopen_yaml(path::AbstractString; kw...)Read a YAML file from a given path and parse it.
Keyword arguments kw is the same as in parse_yaml.
open_yaml(io::IO; kw...)Reads a YAML file from a given io and parse it.
Keyword arguments kw is the same as in parse_yaml.
YAML errors
LibYAML2.ParserYAML.AbstractYAMLError — TypeAbstractYAMLError <: ExceptionAbstract error type for its subtypes.
LibYAML2.ParserYAML.YAMLError — TypeYAMLError <: AbstractYAMLErrorGeneral YAML error.
Fields
msg::String: The message of the error.
LibYAML2.ParserYAML.YAMLMemoryError — TypeYAMLMemoryError <: AbstractYAMLErrorMemory allocation error returned when resources cannot be allocated.
Fields
context::String: The context of the error.context_mark::YAMLMark: The position (line and column) of the context, where the error occured.problem::String: The message of the error.problem_mark::YAMLMark: The position (line and column) of the place, where the error occured.
LibYAML2.ParserYAML.YAMLReaderError — TypeYAMLReaderError <: AbstractYAMLErrorError reading of a YAML document.
Fields
context::String: The context of the error.context_mark::YAMLMark: The position (line and column) of the context, where the error occured.problem::String: The message of the error.problem_mark::YAMLMark: The position (line and column) of the place, where th error occured.
LibYAML2.ParserYAML.YAMLScannerError — TypeYAMLScannerError <: AbstractYAMLErrorError tokenizing YAML stream.
Fields
context::String: The context of the error.context_mark::YAMLMark: The position (line and column) of the context, where the error occured.problem::String: The message of the error.problem_mark::YAMLMark: The position (line and column) of the place, where th error occured.
LibYAML2.ParserYAML.YAMLParserError — TypeYAMLParserError <: AbstractYAMLErrorError parsing YAML document structure.
Fields
context::String: The context of the error.context_mark::YAMLMark: The position (line and column) of the context, where the error occured.problem::String: The message of the error.problem_mark::YAMLMark: The position (line and column) of the place, where th error occured.