-{"abstract":[{"text":"From zero to decoding TOML with TOMLDecoder, a minimal example.","type":"text"}],"hierarchy":{"paths":[["doc://TOMLDecoder/documentation/TOMLDecoder"]]},"identifier":{"interfaceLanguage":"swift","url":"doc://TOMLDecoder/documentation/TOMLDecoder/GettingStarted"},"kind":"article","metadata":{"modules":[{"name":"TOMLDecoder"}],"role":"article","roleHeading":"Article","title":"Getting Started"},"primaryContentSections":[{"content":[{"anchor":"Install","level":2,"text":"Install","type":"heading"},{"anchor":"via-SwiftPM","level":3,"text":"via SwiftPM","type":"heading"},{"inlineContent":[{"text":"TOMLDecoder is a Swift package with minimal dependencies.","type":"text"}],"type":"paragraph"},{"inlineContent":[{"text":"Let’s assume you are starting with a ","type":"text"},{"identifier":"https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/","isActive":true,"type":"reference"},{"text":" project.","type":"text"}],"type":"paragraph"},{"inlineContent":[{"text":"Add the following to your package dependencies:","type":"text"}],"type":"paragraph"},{"code":[".package(url: \"https://github.com/dduan/TOMLDecoder\", exactly: \"0.4.2\")),"],"syntax":"swift","type":"codeListing"},{"inlineContent":[{"text":"In a target’s dependencies, add:","type":"text"}],"type":"paragraph"},{"code":[".product(name: \"TOMLDecoder\", package: \"TOMLDecoder\"),"],"syntax":"swift","type":"codeListing"},{"inlineContent":[{"text":"Run ","type":"text"},{"code":"swift build","type":"codeVoice"},{"text":".","type":"text"},{"text":" ","type":"text"},{"text":"You may have to update your package’s ","type":"text"},{"code":"platforms","type":"codeVoice"},{"text":" if you see a related error.","type":"text"}],"type":"paragraph"},{"anchor":"via-Bazel-Central-Registry","level":3,"text":"via Bazel Central Registry","type":"heading"},{"inlineContent":[{"text":"You can use TOMLDecoder from the Bazel Central Registry.","type":"text"},{"text":" ","type":"text"},{"text":"Include it in your MODULE.bazel","type":"text"}],"type":"paragraph"},{"code":["bazel_dep(name = 'swift-tomldecoder', version = '0.4.1')"],"syntax":null,"type":"codeListing"},{"inlineContent":[{"text":"And add ","type":"text"},{"code":"@swift-tomldecoder//:TOMLDecoder","type":"codeVoice"},{"text":" as a dependency to your target as needed.","type":"text"}],"type":"paragraph"},{"anchor":"Using-TOMLDecoder","level":2,"text":"Using TOMLDecoder","type":"heading"},{"anchor":"Decoding-TOML","level":3,"text":"Decoding TOML","type":"heading"},{"inlineContent":[{"text":"TOMLDecoder can turn TOML into your types that conforms to ","type":"text"},{"code":"Swift.Codable","type":"codeVoice"},{"text":".","type":"text"}],"type":"paragraph"},{"code":["import TOMLDecoder","","let tomlString = \"\"\"","title = \"TOML example\"","","[owner]","name = \"Tom Preston-Werner\"","dob = 1979-05-27T07:32:00-08:00","","[database]","enabled = true","ports = [ 8000, 8001, 8002 ]","temp_targets = { cpu = 79.5, case = 72.0 }","\"\"\"","","// A normal `Codable` or `Decodable` type.","struct Config: Codable {"," let title: String"," let owner: Owner"," let database: Database",""," struct Owner: Codable {"," let name: String"," let dob: Date"," }",""," struct Database: Codable {"," var enabled: Bool"," var ports: [Int]"," var tempTargets: [String: Double]"," }","}","","let config = try TOMLDecoder().decode(Config.self, from: tomlString)","","print(config.owner.name) // Tom Preston-Werner"],"syntax":"swift","type":"codeListing"},{"anchor":"Parsing-for-specific-fields","level":3,"text":"Parsing for specific fields","type":"heading"},{"inlineContent":[{"text":"If you don’t need,","type":"text"},{"text":" ","type":"text"},{"text":"or can’t,","type":"text"},{"text":" ","type":"text"},{"text":"work with ","type":"text"},{"code":"Codable","type":"codeVoice"},{"text":",","type":"text"},{"text":" ","type":"text"},{"text":"you can use the lower-level APIs to deserialize TOML.","type":"text"}],"type":"paragraph"},{"code":["// TOML is always a table at the root","let rootTable = try TOMLTable(source: tomlString)","","// Get the `owner` table. It's a TOMLTable","let owner = try rootTable.table(forKey: \"owner\")","","// Get a string from `owner`","print(try owner.string(forKey: \"name\")) // Tom Preston-Werner"],"syntax":"swift","type":"codeListing"}],"kind":"content"}],"references":{"doc://TOMLDecoder/documentation/TOMLDecoder":{"abstract":[{"text":"TOML:","type":"text"},{"text":" ","type":"text"},{"text":"“a minimal configuration file format that’s easy to read due to obvious semantics”.","type":"text"}],"identifier":"doc://TOMLDecoder/documentation/TOMLDecoder","kind":"symbol","role":"collection","title":"TOMLDecoder","type":"topic","url":"/documentation/tomldecoder"},"https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/":{"identifier":"https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/","title":"SwiftPM","titleInlineContent":[{"text":"SwiftPM","type":"text"}],"type":"link","url":"https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/"}},"schemaVersion":{"major":0,"minor":3,"patch":0},"sections":[],"variants":[{"paths":["/documentation/tomldecoder/gettingstarted"],"traits":[{"interfaceLanguage":"swift"}]}]}
0 commit comments