Movatterモバイル変換


[0]ホーム

URL:


GitHub

TOML

TOML.jl is a Julia standard library for parsing and writingTOML v1.0 files.

Parsing TOML data

julia> using TOMLjulia> data = """           [database]           server = "192.168.1.1"           ports = [ 8001, 8001, 8002 ]       """;julia> TOML.parse(data)Dict{String, Any} with 1 entry:  "database" => Dict{String, Any}("server"=>"192.168.1.1", "ports"=>[8001, 8001…

To parse a file, useTOML.parsefile. If the file has a syntax error, an exception is thrown:

julia> using TOMLjulia> 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 (TOML.tryparse andTOML.tryparsefile) that instead of throwing exceptions on parser error returns aTOML.ParserError with information:

julia> using TOMLjulia> err = TOML.tryparse("""           value = 0.0.0       """);julia> err.typeErrGenericValueError::ErrorType = 14julia> err.line1julia> err.column16

Exporting data to TOML file

TheTOML.print function is used to print (or serialize) data into TOML format.

julia> using TOMLjulia> 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)       endjulia> TOML.parsefile(fname)Dict{String, Any} with 2 entries:  "names" => ["Julia", "Julio"]  "age"   => [10, 20]

Keys can be sorted according to some value

julia> using TOMLjulia> TOML.print(Dict(       "abc"  => 1,       "ab"   => 2,       "abcd" => 3,       ); sorted=true, by=length)ab = 2abc = 1abcd = 3

For custom structs, pass a function that converts the struct to a supported type

julia> using TOMLjulia> struct MyStruct           a::Int           b::String       endjulia> TOML.print(Dict("foo" => MyStruct(5, "bar"))) do x           x isa MyStruct && return [x.a, x.b]           error("unhandled type $(typeof(x))")       endfoo = [5, "bar"]

References

TOML.parseFunction
parse(x::Union{AbstractString, IO})parse(p::Parser, x::Union{AbstractString, IO})

Parse the string or streamx, and return the resulting table (dictionary). Throw aParserError upon failure.

See alsoTOML.tryparse.

TOML.parsefileFunction
parsefile(f::AbstractString)parsefile(p::Parser, f::AbstractString)

Parse filef and return the resulting table (dictionary). Throw aParserError upon failure.

See alsoTOML.tryparsefile.

TOML.tryparseFunction
tryparse(x::Union{AbstractString, IO})tryparse(p::Parser, x::Union{AbstractString, IO})

Parse the string or streamx, and return the resulting table (dictionary). Return aParserError upon failure.

See alsoTOML.parse.

TOML.tryparsefileFunction
tryparsefile(f::AbstractString)tryparsefile(p::Parser, f::AbstractString)

Parse filef and return the resulting table (dictionary). Return aParserError upon failure.

See alsoTOML.parsefile.

TOML.printFunction
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sorted=false, by=identity, inline_tables::IdSet{<:AbstractDict})

Writedata as TOML syntax to the streamio. If the keyword argumentsorted is set totrue, sort tables according to the function given by the keyword argumentby. If the keyword argumentinline_tables is given, it should be a set of tables that should be printed "inline".

The following data types are supported:AbstractDict,AbstractVector,AbstractString,Integer,AbstractFloat,Bool,Dates.DateTime,Dates.Time,Dates.Date. Note that the integers and floats need to be convertible toFloat64 andInt64 respectively. For other data types, pass the functionto_toml that takes the data types and returns a value of a supported type.

TOML.ParserType
Parser()

Constructor for a TOMLParser. Note that in most cases one does not need to explicitly create aParser but instead one directly use useTOML.parsefile orTOML.parse. 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.ParserErrorType
ParserError

Type that is returned fromtryparse andtryparsefile when parsing fails. It contains (among others) the following fields:

  • pos, the position in the string when the error happened
  • table, the result that so far was successfully parsed
  • type, an error type, different for different types of errors

Settings


This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.


[8]ページ先頭

©2009-2025 Movatter.jp