Swiftpack.co - makoni/parsable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by makoni.
makoni/parsable 1.0.4
Syntax sugar that makes JSON decoding more elegant.
⭐️ 1
🕓 35 weeks ago
.package(url: "https://github.com/makoni/parsable.git", from: "1.0.4")

Parsable

Syntax sugar that makes JSON decoding more elegant.

Build and test

Installation

Swift Package Manager

Add to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/makoni/parsable.git", from: "1.0.0"),
]

Usage

An example struct or class should conform to Codable and Parseable protocols:

#import Parsable

// Example struct that conforms to Codable protocol
struct SampleErrorModel: Codable {
    var error: String?
    var code: Int?
}

// Parseable protocol conformance. That's it.
extension SampleErrorModel: Parseable {
    typealias ParseableType = Self
}

let sampleModel = SampleErrorModel(error: "error message text", code: 404)

Struct -> JSON data:

let jsonData = SampleErrorModel.encode(
    fromEncodable: error, 
    withDateEncodingStrategy: .millisecondsSince1970
)

JSON data -> struct:

let decodedModel = SampleErrorModel.decodeFromData(
    data: jsonData,
    withDateDecodingStrategy: .millisecondsSince1970
)

Date decoding strategy is optional. Default value is:

let jsonData = SampleErrorModel.encode(fromEncodable: sampleModel)
let decodedModel = SampleErrorModel.decodeFromData(data: jsonData)

Here's some documentation.

GitHub

link
Stars: 1
Last commit: 35 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.0.4
35 weeks ago

Small refactoring. Added documentation.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics