Skip to content
Syntax sugar that makes JSON decoding more elegant.
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.swiftpm/xcode/package.xcworkspace
Sources/Parsable
Tests
.gitignore
Package.swift
README.md

README.md

Parsable

Syntax sugar that makes JSON decoding more elegant.

Swift 5 Swift Package Manager

Installation

Swift Package Manager

Add to the dependencies value of your Package.swift.

Swift 5

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

Usage

Example struct 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
}

Encode model -> JSON:

let sampleModel = SampleErrorModel(error: "error message text", code: 404)
let jsonData = SampleErrorModel.encode(fromEncodable: sampleModel)

Decode JSON -> model:

let decodedModel = SampleErrorModel.decodeFromData(data: jsonData)
You can’t perform that action at this time.