An SPM package that converts between YAML and the equivalent Foundation objects. YAMLSerialization depends on the LibYAML C library and JSONSerialization to do the heavy lifting. The API was made to be similiar to JSONSerialization for familiarity.
Requires a mininum version of Swift 4.2.
YAMLSerialization depends on the external LibYAML system library and pkg-config. Installation instructions for macOS, Ubuntu, and Fedora are below.
$ brew install libyaml
$ brew install pkg-config
# apt-get update
# apt-get install libyaml-dev
# apt-get install pkg-config
# dnf install libyaml-devel
# dnf install pkg-config
import YAMLSerialization
let yamlStr = """
---
bool_val: true
double_val: 10.4
int_val: 3
string_val: Hello, World
fruits_array:
- Apple
- Banana
- Strawberry
- Mango
ages:
jane: 18
josh: 16
echo: 100
"""
let yaml = try! YAMLSerialization.yamlObject(with: yamlStr) as! [String: Any]
// Extract individual values from YAML object
let bool = yaml["bool_val"] as! Bool // true
let double yaml["double_val"] as! Double // 10.4
let int = yaml["int_val"] as! Int // 3
let str = yaml["string_val"] as! String // Hello, World
// Arrays and Dictionaries
let fruits = yaml["fruits_array"] as! [String]
print(fruits[2]) // Strawberry
let ages = yaml["ages"] as! [String: Int]
print(ages["josh"]) // 14
dump(yaml)
Alternatively, you can also pass a Data object as the first argument:
let yamlData = yamlStr.data(using: .utf8)
let yaml = try! YAMLSerialization.yamlObject(with: yamlData) as! [String: Any]
- Parse multiple YAML documents from a single Data or String object and return a
[[String: Any]]
array. - Implement
isValidYAMLObject(_:)
- Creating YAML data via
data(withYAMLObject:options:)
- Proper tests
- Example
- Reading YAML objects from Data
- Reading YAML objects from String