Syntax sugar that makes JSON decoding more elegant.
Add to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/makoni/parsable.git", from: "1.0.0"),
]
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.
link |
Stars: 1 |
Last commit: 6 weeks ago |
Small refactoring. Added documentation.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics