TOML
To parse a file, use TOML.parsefile. If the file has a syntax error, an exception is thrown:
julia> TOML.parse("""
value = 0.0.0
""")
ERROR: TOML Parser error:
none:1:16 error: failed to parse value
value = 0.0.0
^
[...]
There are other versions of the parse functions ( and [TOML.tryparsefile
]) that instead of throwing exceptions on parser error returns a TOML.ParserError with information:
julia> using TOML
julia> err = TOML.tryparse("""
value = 0.0.0
""");
julia> err.type
ErrGenericValueError::ErrorType = 14
julia> err.line
julia> err.column
16
The TOML.print function is used to print (or serialize) data into TOML format.
julia> using TOML
julia> data = Dict(
"names" => ["Julia", "Julio"],
"age" => [10, 20],
julia> TOML.print(data)
names = ["Julia", "Julio"]
age = [10, 20]
julia> fname = tempname();
julia> open(fname, "w") do io
TOML.print(io, data)
end
julia> TOML.parsefile(fname)
Dict{String, Any} with 2 entries:
"names" => ["Julia", "Julio"]
Keys can be sorted according to some value
For custom structs, pass a function that converts the struct to a supported type
julia> using TOML
julia> struct MyStruct
a::Int
b::String
end
julia> TOML.print(Dict("foo" => MyStruct(5, "bar"))) do x
x isa MyStruct && return [x.a, x.b]
error("unhandled type $(typeof(x))")
end
foo = [5, "bar"]
TOML.parse — Function
parse(x::Union{AbstractString, IO})
parse(p::Parser, x::Union{AbstractString, IO})
Parse the string or stream , and return the resulting table (dictionary). Throw a upon failure.
TOML.parsefile — Function
parsefile(f::AbstractString)
parsefile(p::Parser, f::AbstractString)
Parse file f
and return the resulting table (dictionary). Throw a upon failure.
See also TOML.tryparsefile.
— Function
Parse the string or stream x
, and return the resulting table (dictionary). Return a ParserError upon failure.
See also .
TOML.tryparsefile — Function
tryparsefile(f::AbstractString)
tryparsefile(p::Parser, f::AbstractString)
See also .
TOML.print — Function
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sorted=false, by=identity)
Write data
as TOML syntax to the stream io
. If the keyword argument sorted
is set to true
, sort tables according to the function given by the keyword argument by
.
The following data types are supported: AbstractDict
, Integer
, AbstractFloat
, Bool
, Dates.DateTime
, Dates.Time
, Dates.Date
. Note that the integers and floats need to be convertible to Float64
and Int64
respectively. For other data types, pass the function to_toml
that takes the data types and returns a value of a supported type.
— Type
Parser()
Constructor for a TOML Parser
. Note that in most cases one does not need to explicitly create a Parser
but instead one directly use use TOML.parsefile or . Using an explicit parser will however reuse some internal data structures which can be beneficial for performance if a larger number of small files are parsed.
TOML.ParserError — Type
pos
, the position in the string when the error happenedtable
, the result that so far was successfully parsedtype
, an error type, different for different types of errors