Parsable

1.0.4

Syntax sugar that makes JSON decoding more elegant.
makoni/parsable

What's New

1.0.4

2023-08-19T13:59:38Z

Small refactoring. Added documentation.

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.

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.

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

Last updated: Thu Mar 14 2024 16:45:19 GMT-0900 (Hawaii-Aleutian Daylight Time)