Go to file
JP Simard c61caea937
Fix renamed SwiftLint analyzer rule (#213)
2019-10-20 10:57:12 -07:00
Sources Remove unused declaration (#214) 2019-10-20 10:42:03 -07:00
Tests Fix renamed SwiftLint analyzer rule (#213) 2019-10-20 10:57:12 -07:00
Yams.xcodeproj Enable code coverage 2019-06-21 08:34:57 +09:00
.gitignore Add UUID support (#205) 2019-10-19 22:53:59 -07:00
.jazzy.yaml Change jazzy github_file_prefix to master 2018-05-09 20:57:34 -07:00
.swiftlint.yml Fix renamed SwiftLint analyzer rule (#213) 2019-10-20 10:57:12 -07:00
CHANGELOG.md Add UUID support (#205) 2019-10-19 22:53:59 -07:00
CONTRIBUTING.md Add CONTRIBUTING.md 2017-11-02 22:25:37 -07:00
Docs.md Point to API docs link from Docs.md 2018-05-12 16:26:23 -07:00
Gemfile Add jazzy to Gemfile 2018-05-06 11:26:49 -07:00
Gemfile.lock [Azure Pipelines] Add Xcode 11 job (#201) 2019-07-13 15:26:10 -07:00
LICENSE first commit 2016-11-19 13:59:58 -08:00
Package.swift Fix indentation in Package.swift 2019-06-16 14:32:54 -07:00
Package@swift-4.2.swift Fix indentation 2018-09-11 10:51:28 -07:00
Package@swift-5.swift Add Package@swift-5.swift 2018-12-25 09:17:16 +09:00
README.md Update minimum required Swift version in a few places (#190) 2019-06-21 08:43:58 -07:00
Yams.podspec Update minimum required Swift version in a few places (#190) 2019-06-21 08:43:58 -07:00
azure-pipelines.yml [CI] Don't specify a destination for watchOS job (#206) 2019-09-08 15:43:01 -04:00
yams.jpg add image to readme 2016-11-20 11:26:22 -08:00

README.md

Yams

Yams

A sweet and swifty YAML parser built on LibYAML.

Azure Pipelines

Installation

Building Yams requires Xcode 10.x or a Swift 4.1+/5.x toolchain with the Swift Package Manager.

Swift Package Manager

Add .package(url: "https://github.com/jpsim/Yams.git", from: "2.0.0") to your Package.swift file's dependencies.

CocoaPods

Add pod 'Yams' to your Podfile.

Carthage

Add github "jpsim/Yams" to your Cartfile.

Usage

Yams has three groups of conversion APIs: one for use with Codable types, another for Swift Standard Library types, and a third one for a Yams-native representation.

Codable types

  • Codable is an encoding & decoding strategy introduced in Swift 4 enabling easy conversion between YAML and other Encoders like JSONEncoder and PropertyListEncoder.
  • Lowest computational overhead, equivalent to Yams.Node.
  • Encoding: YAMLEncoder.encode(_:) Produces a YAML String from an instance of type conforming to Encodable.
  • Decoding: YAMLDecoder.decode(_:from:) Decodes an instance of type conforming to Decodable from YAML String.
import Foundation
import Yams

struct S: Codable {
    var p: String
}

let s = S(p: "test")
let encoder = YAMLEncoder()
let encodedYAML = try encoder.encode(s)
encodedYAML == """
p: test

"""
let decoder = YAMLDecoder()
let decoded = try decoder.decode(S.self, from: encodedYAML)
s.p == decoded.p

Swift Standard Library types

  • The type of Swift Standard Library is inferred from the contents of the internal Yams.Node representation by matching regular expressions.
  • This method has the largest computational overhead When decoding YAML, because the type inference of all objects is done up-front.
  • It may be easier to use in such a way as to handle objects created from JSONSerialization or if the input is already standard library types (Any, Dictionary, Array, etc.).
  • Encoding: Yams.dump(object:) Produces a YAML String from an instance of Swift Standard Library types.
  • Decoding: Yams.load(yaml:) Produces an instance of Swift Standard Library types as Any from YAML String.
// [String: Any]
let dictionary: [String: Any] = ["key": "value"]
let mapYAML: String = try Yams.dump(object: dictionary)
mapYAML == """
key: value

"""
let loadedDictionary = try Yams.load(yaml: mapYAML) as? [String: Any]

// [Any]
let array: [Int] = [1, 2, 3]
let sequenceYAML: String = try Yams.dump(object: array)
sequenceYAML == """
- 1
- 2
- 3

"""
let loadedArray: [Int]? = try Yams.load(yaml: sequenceYAML) as? [Int]

// Any
let string = "string"
let scalarYAML: String = try Yams.dump(object: string)
scalarYAML == """
string

"""
let loadedString: String? = try Yams.load(yaml: scalarYAML) as? String

Yams.Node

  • Yams' native model representing Nodes of YAML which provides all functions such as detection and customization of the YAML format.
  • Depending on how it is used, computational overhead can be minimized.
  • Encoding: Yams.serialize(node:) Produces a YAML String from an instance of Node.
  • Decoding Yams.compose(yaml:) Produces an instance of Node from YAML String.
var map: Yams.Node = [
    "array": [
        1, 2, 3
    ]
]
map.mapping?.style = .flow
map["array"]?.sequence?.style = .flow
let yaml = try Yams.serialize(node: map)
yaml == """
{array: [1, 2, 3]}

"""
let node = try Yams.compose(yaml: yaml)
map == node

License

Both Yams and libYAML are MIT licensed.