GithubHelp home page GithubHelp logo

lebje / tomlkit Goto Github PK

View Code? Open in Web Editor NEW
32.0 1.0 6.0 5.79 MB

A small, simple TOML parser and serializer for Swift. Powered by toml++.

Home Page: https://lebje.github.io/TOMLKit/documentation/tomlkit

License: MIT License

Ruby 0.01% Swift 18.72% C++ 81.27%
toml toml-parser swift swift5 swift-package toml-parsing serializer macos linux windows windows-10 toml-serializer parser tomlplusplus

tomlkit's Introduction

TOMLKit

A small, simple TOML parser and serializer for Swift. Powered by toml++.

Swift 5.4 SPM Compatible Build and Test

TOMLKit is a Swift wrapper around toml++, allowing you to parse and serialize TOML files in Swift.

Table of Contents

Created by gh-md-toc

Installation

Swift Package Manager

Add this to the dependencies array in Package.swift:

.package(url: "https://github.com/LebJe/TOMLKit.git", from: "0.5.0")

Also add this to the targets array in the aforementioned file:

.product(name: "TOMLKit", package: "TOMLKit")

Quick Start

import TOMLKit
import struct Foundation.Date

let toml = """
string = "Hello, World!"
int = 523053
double = 3250.34
"""

struct Test: Codable {
    let string: String
    let int: Int
    let double: Double
}

do {
   let table = try TOMLTable(string: toml)
   table.remove(at: "double")

   table["dateTime"] = TOMLDateTime(date: Foundation.Date())
   table.insert(
      TOMLArray([1, 2, "3", TOMLTable(["time": TOMLTime(hour: 10, minute: 26, second: 49, nanoSecond: 203)])]),
      at: "array"
   )

   let test = Test(string: "Goodbye, World!", int: 24598, double: 18.247)
   let encodedTest: TOMLTable = try TOMLEncoder().encode(test)
   let decodedTest = try TOMLDecoder().decode(Test.self, from: encodedTest)

   print("----Encoding & Decoding----\n")

   print("Encoded Test: \n\(encodedTest)\n")
   print("Decoded Test: \(decodedTest)")

   print("\n----Conversion----\n")

   print("TOML:\n\(table.convert(to: .toml))\n")
   print("JSON:\n\(table.convert(to: .json))\n")
   print("YAML:\n\(table.convert(to: .yaml))")
} catch let error as TOMLParseError {
   // TOMLParseError occurs when the TOML document is invalid.
   // `error.source.begin` contains the line and column where the error started,
   // and `error.source.end` contains the line where the error ended.
    print(error.source.begin)
    print(error.source.end)
}

// ----Encoding & Decoding----
//
// Encoded Test:
// double = 18.247
// int = 24598
// string = 'Goodbye, World!'
//
// Decoded Test: Test(string: "Goodbye, World!", int: 24598, double: 18.247)
//
// ----Conversion----
//
// TOML:
// array = [ 1, 2, '3', { time = 10:26:49.000000203 } ]
// dateTime = 2022-03-31T12:19:38.160653948Z
// int = 523053
// string = 'Hello, World!'
//
// JSON:
// {
//     "array" : [
//         1,
//         2,
//         "3",
//         {
//             "time" : "10:26:49.000000203"
//         }
//     ],
//     "dateTime" : "2022-03-31T12:19:38.160653948Z",
//     "int" : 523053,
//     "string" : "Hello, World!"
// }
//
// YAML:
// array:
//   - 1
//   - 2
//   - 3
//   - time: '10:26:49.000000203'
// dateTime: '2022-03-31T12:19:38.160653948Z'
// int: 523053
// string: 'Hello, World!'

Full Documentation

Full documentation is available here.

Dependencies

Licenses

The toml++ license is available in the tomlplusplus directory in the LICENSE file.

Contributing

Before committing, please install pre-commit, swift-format, clang-format, and Prettier, then install the pre-commit hook:

$ brew bundle # install the packages specified in Brewfile
$ pre-commit install

# Commit your changes.

To install pre-commit on other platforms, refer to the documentation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.