| Comments | RSS Feed

Introducing Yams 1.0

Yams: A Sweet & Swifty YAML Parser

Norio Nomura and I (ok, honestly mostly Norio 😅) have been building a Swift library for encoding & decoding YAML for the last 18 months and it’s now stable enough to make a 1.0 release and share with the world.

It’s called Yams, you can find it on GitHub at jpsim/Yams and API docs are located at jpsim.com/Yams.

You could say it’s a Swift binding for LibYAML but I’d argue it’s much more than that.

I’m actually very happy with how this library ended up. It plays nicely with Swift 4’s Codable protocol, meaning that you get fast & type-safe encoding & decoding that feels right at home in modern Swift.

Here’s a simple example of encoding & decoding Codable types:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

Alternatively, you can use it to work with Swift scalar & collection types, which is probably the easiest way to start parsing arbitrary YAML for your projects. Finally, there’s a third mode, which is a Yams-native API that best translates to how LibYAML works.

This library’s been powering a number of popular projects that use YAML for configuration, like SwiftLint, SwiftGen, XcodeGen & used in SourceKitten to parse Swift Package Manager build manifests. So if you’ve wanted to add YAML configuration files to your Swift CLI, or want to interoperate with other tools that process YAML, I encourage you to give Yams a try.