This is a Swift library that provides a Parseable
protocol for simplifying JSON encoding and decoding of your model objects.
- A protocol that defines a common interface for JSON parsing and encoding.
- Allows your models to be easily converted to and from JSON data.
- Cleaner code: Reduces boilerplate code for JSON parsing and encoding.
- Consistent API: Provides a consistent way to work with JSON data across your models.
- Improved maintainability: Easier to maintain code as the JSON format evolves.
-
Conform your models to the Parseable protocol:
- Define an
associatedtype
namedParseableType
that inherits fromCodable
. - Implement the static methods
decodeFromData
andencode
as required by the protocol.
- Define an
-
Use the provided methods for JSON conversion:
- Call
decodeFromData(data:withDateDecodingStrategy:)
to parse JSON data into your model object. - Call
encode(fromEncodable:withDateDecodingStrategy:)
to convert your model object to JSON data.
- Call
Optional DLog
function:
The provided code includes a private static function DLog
for debug logging. This function is intended to be used for debugging purposes and is only enabled in debug builds. You can customize or remove this function based on your needs.
#import Parsable
// Define a model conforming to Codable
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)
// Example of decoding JSON data
let jsonData = // Your JSON data here
if let error = SampleErrorModel.decodeFromData(data: jsonData) {
print("Error code: \(error.code)")
} else {
print("Error decoding user data")
}
// Example of encoding a model object
if let encodedData = SampleErrorModel.encode(fromEncodable: sampleModel) {
// Use the encoded data...
} else {
print("Error encoding user data")
}
This is a basic example. You can customize the implementation of decodeFromData
and encode
methods in your models to handle specific data structures and logic.
Add to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/makoni/parsable.git", from: "1.0.0"),
]
Here's some documentation.