TOML

TOML.jl 是 Julia 標準函式庫,用於剖析和撰寫 TOML v1.0 檔案。

剖析 TOML 資料

julia> using TOML

julia> 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…

若要剖析檔案,請使用 TOML.parsefile。如果檔案有語法錯誤,將擲回例外

julia> using TOML

julia> TOML.parse("""
           value = 0.0.0
       """)
ERROR: TOML Parser error:
none:1:16 error: failed to parse value
      value = 0.0.0
                 ^
[...]

剖析函式還有其他版本 (TOML.tryparse 和 [TOML.tryparsefile]),這些版本在剖析器錯誤時不會擲回例外,而是傳回包含資訊的 TOML.ParserError

julia> using TOML

julia> err = TOML.tryparse("""
           value = 0.0.0
       """);

julia> err.type
ErrGenericValueError::ErrorType = 14

julia> err.line
1

julia> err.column
16

將資料匯出到 TOML 檔案

函數 TOML.print 用於將資料列印(或序列化)成 TOML 格式。

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"]
  "age"   => [10, 20]

可以根據某個值對金鑰進行排序

julia> using TOML

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

對於自訂結構,傳遞一個將結構轉換成受支援類型的函數

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函數
parse(x::Union{AbstractString, IO})
parse(p::Parser, x::Union{AbstractString, IO})

剖析字串或串流 x,並傳回結果表格(字典)。如果失敗,會擲回 ParserError

另請參閱 TOML.tryparse

TOML.parsefile函數
parsefile(f::AbstractString)
parsefile(p::Parser, f::AbstractString)

剖析檔案 f,並傳回結果表格(字典)。如果失敗,會擲回 ParserError

另請參閱 TOML.tryparsefile

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

剖析字串或串流 x,並傳回結果表格(字典)。如果失敗,會傳回 ParserError

另請參閱 TOML.parse

TOML.tryparsefile函數
tryparsefile(f::AbstractString)
tryparsefile(p::Parser, f::AbstractString)

剖析檔案 f,並傳回結果表格(字典)。如果失敗,會傳回 ParserError

另請參閱 TOML.parsefile

TOML.print函數
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sorted=false, by=identity)

data 以 TOML 語法寫入串流 io。如果關鍵字引數 sorted 設為 true,則根據關鍵字引數 by 所提供的函數來對表格進行排序。

支援下列資料類型:AbstractDictAbstractVectorAbstractStringIntegerAbstractFloatBoolDates.DateTimeDates.TimeDates.Date。請注意,整數和浮點數需要分別轉換為 Float64Int64。對於其他資料類型,請傳遞函數 to_toml,該函數會取得資料類型並傳回受支援類型的值。

TOML.Parser類型
Parser()

TOML Parser 的建構函數。請注意,在多數情況下,不需要明確建立 Parser,而是直接使用 TOML.parsefileTOML.parse。不過,使用明確的剖析器會重新使用一些內部資料結構,如果剖析大量的小檔案,這將有助於提升效能。

TOML.ParserError類型
ParserError

當剖析失敗時,從 tryparsetryparsefile 傳回的類型。它包含(除其他外)下列欄位:

  • pos,發生錯誤時字串中的位置
  • table,迄今已成功剖析的結果
  • type,錯誤類型,因錯誤類型而異