• Stars
    star
    1,054
  • Rank 42,050 (Top 0.9 %)
  • Language
    C
  • License
    MIT License
  • Created over 7 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Sweet and Swifty YAML parser.

Yams

Yams

A sweet and swifty YAML parser built on LibYAML.

SwiftPM xcodebuild pod lib lint Nightly codecov

Installation

Building Yams requires Xcode 12.5+ or a Swift 5.4+ toolchain with the Swift Package Manager or CMake and Ninja.

CMake

CMake 3.17.2 or newer is required, along with Ninja 1.9.0 or newer.

When building for non-Apple platforms:

cmake -B /path/to/build -G Ninja -S /path/to/yams -DCMAKE_BUILD_TYPE=Release -DFoundation_DIR=/path/to/foundation/build/cmake/modules
cmake --build /path/to/build

To build for Apple platforms (macOS, iOS, tvOS, watchOS), there is no need to separately build Foundation because it is included as part of the SDK:

cmake -B /path/to/build -G Ninja -S /path/to/yams -DCMAKE_BUILD_TYPE=Release
cmake --build /path/to/build

Swift Package Manager

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

CocoaPods

Add pod 'Yams' to your Podfile.

Carthage

Add github "jpsim/Yams" to your Cartfile.

Bazel

In your WORKSPACE file

YAMS_GIT_SHA = "SOME_SHA"
http_archive(
    name = "com_github_jpsim_yams",
    urls = [
        "https://github.com/jpsim/Yams/archive/%s.zip" % YAMS_GIT_SHA,
    ],
    strip_prefix = "Yams-%s" % YAMS_GIT_SHA,
)

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 or Data.
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

Integrating with Combine

When Apple's Combine framework is available, YAMLDecoder conforms to the TopLevelDecoder protocol, which allows it to be used with the decode(type:decoder:) operator:

import Combine
import Foundation
import Yams

func fetchBook(from url: URL) -> AnyPublisher<Book, Error> {
    URLSession.shared.dataTaskPublisher(for: url)
        .map(\.data)
        .decode(type: Book.self, decoder: YAMLDecoder())
        .eraseToAnyPublisher()
}

License

Both Yams and libYAML are MIT licensed.

More Repositories

1

SourceKitten

An adorable little framework and command line tool for interacting with SourceKit.
Swift
2,275
star
2

PeerKit

An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps
Swift
869
star
3

CardsAgainst

An iOS game for horrible people
Swift
572
star
4

ZenTuner

A minimal chromatic tuner for iOS & macOS.
Swift
513
star
5

JPSThumbnailAnnotation

A simple mapkit annotation for displaying images and details.
Objective-C
475
star
6

DeckRocket

DeckRocket turns your iPhone into a remote for Deckset presentations
Objective-C
398
star
7

JPSVolumeButtonHandler

JPSVolumeButtonHandler provides an easy block interface to hardware volume buttons on iOS devices. Perfect for camera apps!
Objective-C
331
star
8

SwiftEdit

A proof-of-concept editor, written in Swift, that supports Swift syntax highlighting using SourceKit.
Swift
235
star
9

AWSPics

An AWS CloudFormation stack to run a serverless password-protected photo gallery
JavaScript
217
star
10

Milestones

An app to count down the days until upcoming milestones.
Swift
199
star
11

JPSImagePickerController

A pixel-perfect clone of iOS 7's UIImagePickerController, with a few improvements.
Objective-C
157
star
12

JPSKeyboardLayoutGuide

Easily make your Auto Layout view controllers keyboard aware
Objective-C
69
star
13

SwiftXPC

XPC simplified for Swift. Deal with Swift and NS* objects instead of xpc_object_t.
Swift
63
star
14

Mix2Files

iOS Project to mix several audio files (mp3's, m4a's, caf's) together at different intervals
Objective-C
40
star
15

LetterpressPlayer

Solves Letterpress games via OCR and colour categorization. Work in progress.
Objective-C
37
star
16

talks

Slides for a few of the talks I've given
JSONiq
35
star
17

UICollectionView-Animation-Bug

There's a confirmed bug in UICollectionView. This project highlights a workaround.
Objective-C
33
star
18

jekyll_app_site

Small Jekyll template to present an iPhone app with a static site
CSS
24
star
19

JPSDisplayLink

JPSDisplayLink provides an easy block interface to CADisplayLink-based animations.
Objective-C
23
star
20

AudioEmailer

Very small iOS app to record, playback and email audio. Includes a realtime dB level meter using F3
Objective-C
23
star
21

pod-diffs

Guide and example for a neat way to modify pods using patchfiles
Objective-C
22
star
22

MPCMultipeerClient

Wrapper around MultipeerConnectivity to simplify common use cases
Objective-C
15
star
23

swiftlint-bazel-example

Example SwiftLint integration with custom native rules using Bazel
Starlark
13
star
24

SwiftIvarTypeDetector

Print out the type encoding for properties in a Swift class
Objective-C
12
star
25

Selfish

A tool to automatically insert explicit `self` references in the Swift files in the current directory.
Swift
8
star
26

retest

Re-run failed GitHub Workflow runs on PRs by commenting "/retest".
TypeScript
5
star
27

rust-ios-bazel-demo

A demo iOS app building with Bazel, Rust and Swift
C++
4
star
28

PCCanvas

Simple drawing canvas for core graphics. Easily accepts PaintCode code.
Objective-C
4
star
29

SwiftClassTodo

Todo example from Swift class
Swift
4
star
30

advent-of-code-2021

Swift
3
star
31

Vertigrow-iPad

iPad app I built for Vertigrow a long time ago to help them mockup living walls as a sales tool. Basic image dragging app.
Objective-C
3
star
32

node-docker

Demo Node.JS App using Docker
Shell
3
star
33

laptop

Laptop is a shell script that turns your Mac OS X laptop into an awesome development machine.
Shell
2
star
34

AutoLayoutHelpers

Super space saving auto layout helpers.
Objective-C
2
star
35

rules_xcodeproj_ios_app

Sample rules_xcodeproj iOS app
Starlark
2
star
36

jpsim.github.com

My personal website
Ruby
2
star
37

presto-gem

Gem for interacting with Presto cards.
Ruby
2
star
38

cocoapods-static-xcframework-issue

Issue with CocoaPods and static xcframeworks
Ruby
2
star
39

InputClick

The most barebones project ever with the simplest implementation of the UIInputViewAudioFeedback protocol.
Objective-C
2
star
40

GoogleMapsSDKBugs

Swift
2
star
41

SwiftPMUtilityExample

Swift
2
star
42

fastmac

Shell
1
star
43

retest-action

A GitHub Action to re-run failed GitHub workflow runs on pull requests
TypeScript
1
star
44

applebugs

Collection of sample code for radars I've filed
Objective-C
1
star
45

presto_api

REST API for interacting with Presto cards using the presto ruby gem
Ruby
1
star
46

SourceKit

Swift Package Manager package for SourceKit
Swift
1
star