macOS Linux Apache 2 Build Status

UniYAML

适用于 Swift 的原生 YAML/JSON (反)序列化器。它始于一个天真的问题:“编写 YAML 解析器有多难?”,紧接着是一个天真的回答:“应该不难。” ;-) 它还不是一个完整的 YAML 1.[012] 实现,但它已被证明在以相当快的速度读写基本的 YAML/JSON 结构时是可用的。

用法

解析 YAML 或 JSON 并直接访问数据

import UniYAML

let json = """
{ 
  \"first name\": Antonio, 
  \"middle name\": Lucio, 
  surname: Vivaldi, 
  nickname: 'il Prete Rosso', 
  born: 1678
}
"""

do {

	let obj = try UniYAML.decode(json)

	print("person details available: \(obj.keys)")
	print("\(obj["first name"]?.string) \(obj["surname"]?.string) aka \(obj["nickname"]?.string) was born on \(obj["born"]?.uint)")

} catch UniYAMLError.error(let detail) {
	print(detail)
}
import UniYAML

let yaml = """
---

classic:
  -
    author: Ludwig van Beethoven
    title: Für Elise
    published: 1867
    duration: 3:30
  -
    author: Wolfgang Amdeus Mozart
    title: Eine kleine Nachtmusik
    published: 1787
    duration: 5:45
  -
    author: Bedřich Smetana
    title: Vltava
    published: 1874
    duration: 12:50
"""

do {
	let obj = try UniYAML.decode(yaml)

	let collection = obj["classic"]!.array!

	print("there are \(collection.count) entries in 'classic' collection")

	let first = collection[0].dictionary!
	let last = collection.last!.dictionary!

	print("shortest (\(first["duration"]?.string)) is \(first["title"]?.string) from \(first["author"]?.string)")
	print("longest (\(last["duration"]?.string)) is \(last["title"]?.string) from \(last["author"]?.string)")

} catch UniYAMLError.error(let detail) {
	print(detail)
}

轻松地将解析的内容转换为期望的类型

import UniYAML

let types = """
---
no choice: don't say anything
variants: 'say ''yes'' or ''no'''
split string: >
  two
  words
rows: |
   first
   second
   last
int: -12345
uint: 67890
double: 3.14159265
positive: yes
negative: off
"""

do {

	let obj = try UniYAML.decode(types)

	print("string: '\(obj["variants"]?.string)'")
	print("multi-line string: '\(obj["rows"]?.string)'")
	print("integer: \(obj["int"]?.int)")
	print("unsigned integer: \(obj["uint"]?.uint)")
	print("double: \(obj["double"]?.double)")
	print("bool: \(obj["negative"]?.bool)")

} catch UniYAMLError.error(let detail) {
	print(detail)
}

序列化自定义数据结构

import UniYAML

do {

	let obj1 = YAML([1, 2, "A", "B"])!
	let json = try UniYAML.encode(obj1)

	let obj2 = YAML(["key": "value", "int": -987, "double": -543.21])!
	let yaml = try UniYAML.encode(obj2, with: .yaml)

	let obj3 = YAML(["key": "value", "array": YAML([10, 11, 12])!, "dict": YAML(["number": 1, "text": "test", "double": 12.34])!])!
	let complex = try UniYAML.encode(obj3)

} catch UniYAMLError.error(let detail) {
	print(detail)
}

鸣谢

Daniel Fojt 编写,版权归 Seznam.cz 所有,根据 Apache License 2.0 条款获得许可。