Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/marcprux/universal
Tiny zero-dependency cross-platform Swift parsers for JSON, XML, and YAML incl. in-memory interchange formats and Either type
https://github.com/marcprux/universal
codable json json-library json-schema swift swift-package-manager yaml
Last synced: about 2 months ago
JSON representation
Tiny zero-dependency cross-platform Swift parsers for JSON, XML, and YAML incl. in-memory interchange formats and Either type
- Host: GitHub
- URL: https://github.com/marcprux/universal
- Owner: marcprux
- License: mit
- Created: 2015-09-14T17:49:56.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-09-27T16:18:55.000Z (3 months ago)
- Last Synced: 2024-10-18T19:33:06.883Z (2 months ago)
- Topics: codable, json, json-library, json-schema, swift, swift-package-manager, yaml
- Language: Swift
- Homepage:
- Size: 25.4 MB
- Stars: 13
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Universal
=========[![Build Status](https://github.com/marcprux/universal/workflows/Universal%20CI/badge.svg?branch=main)](https://github.com/marcprux/universal/actions)
[![Swift Package Manager compatible](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
[![Platform](https://img.shields.io/badge/Platforms-macOS%20|%20Linux%20|%20Windows%20|%20iOS%20|%20tvOS%20|%20watchOS-lightgray.svg)](https://github.com/marcprux/universal/actions)
[![](https://tokei.rs/b1/github/marcprux/universal)](https://github.com/marcprux/universal)**Universal**: A tiny zero-dependency cross-platform Swift parser and decoder for JSON, XML, YAML, and property lists.
## Usage:
Add the following dependency to your `Package.swift`:
```swift
.package(url: "https://github.com/marcprux/universal.git", from: "5.0.5")
```The package provides the modules `Either`, `JSON`, `XML`, `YAML`, `PLIST`,
or `Universal`, which is an umbrella module that re-exports all the other modules.## Example:
```swift
import Universalfunc testUniversalExample() throws {
// JSON Parsing
let json: JSON = try JSON.parse(Data("""
{"parent": {"child": 1}}
""".utf8))assert(json["parent"]?["child"] == 1)
assert(json["parent"]?["child"] == JSON.number(1.0)) // JSON's only number is Double// YAML Parsing
let yaml: YAML = try YAML.parse(Data("""
parent:
child: 1
""".utf8))assert(yaml["parent"]?["child"] == 1)
assert(yaml["parent"]?["child"] == YAML.integer(1)) // YAML can parse integers
assert(yaml["parent"]?["child"] != 1.0) // not the same as a doublelet yamlJSON: JSON = try yaml.json() // convert YAML to JSON struct
assert(yamlJSON == json)// XML Parsing
let xml: XML = try XML.parse(Data("""
1
""".utf8))let xmlJSON: JSON = try xml.json() // convert XML to JSON struct
assert(xml["parent"]?["child"] == XML.string("1")) // XML parses everything as strings
// fixup the XML by changing the JSON to match
assert(json["parent"]?["child"] == 1)
var jsonEdit = json
jsonEdit["parent"]?["child"] = JSON.string("1") // update the JSON to match
assert(jsonEdit["parent"]?["child"] == "1") // now the JSON matchesassert(xmlJSON == jsonEdit)
}
```## Coding
Universal provides the ability to decode from (but not encode to) YAML and XML
through their ability to convert to a `JSON` struct:```swift
import Universalstruct Coded : Decodable, Equatable {
let person: Personstruct Person : Decodable, Equatable {
let firstName: String
let lastName: String
let astrologicalSign: String
}
}let decodedFromJSON = try Coded(json: JSON.parse(Data("""
{
"person": {
"firstName": "Marc",
"lastName": "Prud'hommeaux",
"astrologicalSign": "Sagittarius"
}
}
""".utf8)))let decodedFromYAML = try Coded(json: YAML.parse(Data("""
# A YAML version of a Person
person:
firstName: Marc
lastName: Prud'hommeaux
astrologicalSign: Sagittarius # what's your sign?
""".utf8)).json())
assert(decodedFromJSON == decodedFromYAML)let decodedFromXML = try Coded(json: XML.parse(Data("""
Marc
Prud'hommeaux
Sagittarius
""".utf8)).json())
assert(decodedFromYAML == decodedFromXML)let decodedFromPLISTXML = try Coded(json: PLIST.parse(Data("""
person
firstName
Marc
lastName
Prud'hommeaux
astrologicalSign
Sagittarius
""".utf8)).json())
assert(decodedFromXML == decodedFromPLISTXML)let decodedFromPLISTOpenStep = try Coded(json: PLIST.parse(Data("""
{
person = {
firstName = Marc;
lastName = "Prud'hommeaux";
astrologicalSign = Sagittarius;
};
}
""".utf8)).json())
assert(decodedFromPLISTOpenStep == decodedFromPLISTXML)
```[Swift Package Manager]: https://swift.org/package-manager