chrisbutcher / YamlSwift

Load YAML and JSON documents using Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

YamlSwift

Load YAML and JSON documents using Swift.

YamlSwift parses a string of YAML document(s) (or a JSON document) and returns a Yaml enum value representing that string.

Install

Use Carthage to build and install.

Or use CocoaPods : Add pod 'Yaml' to your Podfile and run pod install.

API

import

To use it, you should import it using the following statement:

import Yaml

Yaml

A Yaml value can be any of these cases:

enum Yaml {
  case Null
  case Bool(Bool)
  case Int(Int)
  case Double(Double)
  case String(String)
  case Array([Yaml])
  case Dictionary([Yaml: Yaml])
}

Yaml.load

Yaml.load (String) -> Result<Yaml>

Takes a string of a YAML document and returns a Result of Yaml enum value.

let value = Yaml.load("a: 1\nb: 2").value!
println(value["a"])  // Int(1)
println(value["b"])  // Int(2)
println(value["c"])  // Null

If the input document is invalid or contains more than one YAML document, an error is returned.

let value = Yaml.load("a\nb: 2").error!
println(value)  // expected end, near "b: 2"

Yaml.loadMultiple

Yaml.loadMultiple (String) -> Result<[Yaml]>

Takes a string of one or more YAML documents and returns a Result of [Yaml].

let value = Yaml.loadMultiple("---\na: 1\nb: 2\n---\na: 3\nb: 4").value!
println(value[0]["a"])  // Int(1)
println(value[1]["a"])  // Int(3)

If an error is encountered in any of the documents, an error is returned.

Yaml#[Int] -> Yaml

value[Int] -> Yaml
value[Int] = Yaml

If used on a Yaml.Array value, it will return the value at the specified index. If the index is invalid or the value is not a Yaml.Array, Yaml.Null is returned. You can also set a value at a specific index. Enough elements will be added to the wrapped array to set the specified index. If the value is not a Yaml.Array, it will change to it after set.

var value = Yaml.load("- Behrang\n- Maryam")
println(value[0])  // String(Behrang)
println(value[1])  // String(Maryam)
println(value[2])  // Null
value[2] = "Radin"
println(value[2])  // String(Radin)

Yaml#[Yaml] -> Yaml

value[Yaml] -> Yaml
value[Yaml] = Yaml

If used on a Yaml.Dictionary value, it will return the value for the specified key. If a value for the specified key does not exist, or value is not a Yaml.Dictionary, Yaml.Null is returned. You can also set a value for a specific key. If the value is not a Yaml.Dictionary, it will change to it after set.

Since Yaml is a literal convertible type, you can pass simple values to this method.

var value = Yaml.load("first name: Behrang\nlast name: Noruzi Niya")
println(value["first name"])  // String(Behrang)
println(value["last name"])  // String(Noruzi Niya)
println(value["age"])  // Null
value["first name"] = "Radin"
value["age"] = 1
println(value["first name"])  // String(Radin)
println(value["last name"])  // String(Noruzi Niya)
println(value["age"])  // Int(1)

Yaml#bool

value.bool -> Bool?

Returns an Optional<Bool> value. If the value is a Yaml.Bool value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("animate: true\nshow tip: false\nusage: 25")
println(value["animate"].bool)  // Optional(true)
println(value["show tip"].bool)  // Optional(false)
println(value["usage"].bool)  // nil

Yaml#int

value.int -> Int?

Returns an Optional<Int> value. If the value is a Yaml.Int value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("a: 1\nb: 2.0\nc: 2.5")
println(value["a"].int)  // Optional(1)
println(value["b"].int)  // Optional(2)
println(value["c"].int)  // nil

Yaml#double

value.double -> Double?

Returns an Optional<Double> value. If the value is a Yaml.Double value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("a: 1\nb: 2.0\nc: 2.5\nd: true")
println(value["a"].double)  // Optional(1.0)
println(value["b"].double)  // Optional(2.0)
println(value["c"].double)  // Optional(2.5)
println(value["d"].double)  // nil

Yaml#string

value.string -> String?

Returns an Optional<String> value. If the value is a Yaml.String value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("first name: Behrang\nlast name: Noruzi Niya\nage: 33")
println(value["first name"].string)  // Optional("Behrang")
println(value["last name"].string)  // Optional("Noruzi Niya")
println(value["age"].string)  // nil

Yaml#array

value.array -> [Yaml]?

Returns an Optional<Array<Yaml>> value. If the value is a Yaml.Array value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("languages:\n - Swift: true\n - Objective C: false")
println(value.array)  // nil
println(value["languages"].array)  // Optional([Dictionary([String(Swift): Bool(true)]), Dictionary([String(Objective C): Bool(false)])])

Yaml#dictionary

value.dictionary -> [Yaml: Yaml]?

Returns an Optional<Dictionary<Yaml, Yaml>> value. If the value is a Yaml.Dictionary value, the wrapped value is returned. Otherwise nil is returned.

let value = Yaml.load("- Swift: true\n- Objective C: false")
println(value.dictionary)  // nil
println(value[0].dictionary)  // Optional([String(Swift): Bool(true)])

Yaml#count

value.count -> Int?

Returns an Optional<Int> value. If the value is either a Yaml.Array or a Yaml.Dictionary value, the count of elements is returned. Otherwise nil is returned.

let value = Yaml.load("- Swift: true\n- Objective C: false")
println(value.count)  // Optional(2)
println(value[0].count)  // Optional(1)
println(value[0]["Swift"].count)  // nil

License

MIT

About

Load YAML and JSON documents using Swift

License:MIT License


Languages

Language:Swift 96.8%Language:Ruby 2.6%Language:Objective-C 0.6%