YAML
Parsing
Serde.SerdeYaml.parse_yaml — Function
parse_yaml(x::Union{AbstractString, Vector{UInt8}}; dict_type = Dict{String,Any}, kw...) -> AnyParse a YAML string or byte vector into Julia data structures without mapping to a target type.
Returns the raw parsed value: a Dict{String,Any} for YAML mappings, a Vector{Any} for sequences, or a scalar for plain values. Delegates to the YAML.jl library internally.
Arguments
x: YAML text as aStringorVector{UInt8}.
Keyword arguments
dict_type::Type{<:AbstractDict}: concrete dict type for YAML mappings (defaultDict{String,Any}).- Additional keyword arguments are forwarded to
YAML.load.
Returns
The parsed Julia value.
Throws
ParseError: ifxcontains malformed YAML.
Examples
julia> parse_yaml("x: 1\ny: 2")
Dict{String, Any}("x" => 1, "y" => 2)See also: from_yaml, try_from_yaml.
Deserialization
Serde.SerdeYaml.from_yaml — Function
from_yaml(::Type{T}, x; kw...) -> T
from_yaml(strategy, ::Type{T}, x; kw...) -> T
from_yaml(f::Function, x; kw...) -> AnyParse a YAML string and deserialize it into type T.
When called with a function f, the raw parsed value is passed to f to determine the target type dynamically: to_deser(f(parsed), parsed).
Pass a context object strategy (e.g. CamelCase()) as the first argument to apply global field-renaming during deserialization.
Arguments
::Type{T}: target type to construct.x: YAML text as aStringorVector{UInt8}.strategy: optional context object.
Returns
A value of type T.
Throws
ParseError: ifxis malformed YAML.MissingFieldError: if a required struct field is absent.TypeMismatchError: if a value cannot be coerced to the field type.
Examples
julia> struct Point; x::Int; y::Int; end
julia> from_yaml(Point, "x: 1\ny: 2")
Point(1, 2)See also: try_from_yaml, to_yaml, parse_yaml.
Serde.SerdeYaml.try_from_yaml — Function
try_from_yaml(::Type{T}, x; kw...) -> Union{T, SerdeError}
try_from_yaml(strategy, ::Type{T}, x; kw...) -> Union{T, SerdeError}Like from_yaml but returns a SerdeError instead of throwing on failure.
Returns
Ton success.- A
ParseErrororDeserErrorsubtype on failure.
Examples
julia> struct Point; x::Int; y::Int; end
julia> try_from_yaml(Point, "x: 1\ny: 2")
Point(1, 2)
julia> try_from_yaml(Point, ": invalid yaml :") isa SerdeError
trueSee also: from_yaml, SerdeError.
Serialization
Serde.SerdeYaml.to_yaml — Function
to_yaml(data) -> String
to_yaml(strategy, data) -> StringSerialize data into a YAML string.
All serialization traits (Serde.ser_name, Serde.ser_value, Serde.ser_type, Serde.ser_skip) are applied.
Pass a context object strategy (e.g. CamelCase()) as the first argument to apply global field-renaming.
Arguments
data: the value to serialize.strategy: optional context object.
Returns
A YAML String.
Examples
julia> struct Server; host::String; port::Int; end
julia> to_yaml(CamelCase(), Server("localhost", 8080)) |> print
host: "localhost"
port: 8080See also: from_yaml.