Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

Parsable

Syntax sugar that makes JSON decoding more elegant.

Swift 5 Swift Package Manager Build and test

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)
// or
let jsonData = SampleErrorModel.encode(fromEncodable: error, withDateEncodingStrategy: .millisecondsSince1970)

Decode JSON -> model:

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

About

Syntax sugar that makes JSON decoding more elegant.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages