FlexiDecodable is a lightweight, flexible, and easy-to-use library for decoding JSON data into Swift types, providing type coercion with fallback support. It allows you to seamlessly handle different data types coming from a JSON response and automatically convert them to the expected types in your model.
This library is especially useful when dealing with API responses that may contain multiple types for a single field (e.g., String, Int, Float, Double, etc.), making it easier to work with inconsistent data structures.
[!IMPORTANT] The current version is still in development. There may be breaking changes in version updates until version 1.0.
- Type Coercion with Fallback: Automatically convert
String,Int,Float,Double, andBooltypes to your model's expected type. - Flexible Data Handling: Coerce data from different types (e.g.,
"true"string toBool,"1"string toInt, etc.). - Error Handling: Throws clear errors if the type conversion fails, ensuring data integrity.
- Easy Integration: Seamless integration with your existing Swift codebase using the standard
Decodableprotocol. - Customizable for Future Types: Easily extendable to support other types like
Date,URL, etc.
[!CAUTION] 🚨 Breaking changes:
- Version 0.1.0: Initial release with support for
String,Int,Float,Double, andBoolcoercion.
Usage of the library is simple and can be seen in the example provided below:
struct WelcomeModel: FlexiDecodable {
let id: String?
let title: String
let type: Int?
let category: Float?
}
let jsonData = """
{
"id": "123",
"title": "Welcome",
"type": "1",
"category": "45.67"
}
""".data(using: .utf8)!
do {
let decodedModel = try JSONDecoder().decode(WelcomeModel.self, from: jsonData)
print(decodedModel) // Outputs: WelcomeModel(id: "123", title: "Welcome", type: 1, category: 45.67)
} catch {
print("Decoding failed: \(error)")
}Add the following to your Package.swift:
dependencies: [
.package(url: "https://github.com/your-username/FlexiDecodable.git", from: "1.0.0")
]Add the following line to your Podfile:
pod 'FlexiDecodable', '~> 1.0.0'Then run:
pod installor
pod updateHere's how to use FlexiDecodable in your Swift model:
struct MyModel: FlexiDecodable {
let id: String?
let isActive: Bool?
let amount: Float?
}
let jsonData = """
{
"id": "456",
"isActive": "true",
"amount": "99.99"
}
""".data(using: .utf8)!
do {
let model = try JSONDecoder().decode(MyModel.self, from: jsonData)
print(model) // Outputs: MyModel(id: "456", isActive: true, amount: 99.99)
} catch {
print("Decoding failed: \(error)")
}While the basic functionality works out of the box for String, Int, Float, Double, and Bool, you can extend the functionality in the future as needed. FlexiDecodable is built with extensibility in mind.
- Can be converted from
String,Int,Float,Double.
- Can be converted from
String,Float,Double.
- Can be converted from
String,Int,Double.
- Can be converted from
String,Int,Float.
- Can be converted from
String("true"/"false") orInt(1 fortrue, 0 forfalse).
You can extend the library to support additional types (like Date, URL, etc.) by adding new decodeWithFallback functions for each type.
You can extend the decodeWithFallback method to handle more types in the future as needed. Right now, it supports:
- String: Converts from
String,Int,Float,Double. - Int: Converts from
String,Float,Double. - Float: Converts from
String,Int,Double. - Bool: Converts from
String("true"/"false") orInt(1 fortrue, 0 forfalse).
FlexiDecodable is available under the MIT license. See the LICENSE file for more information.