Simplify Your JSON decoding with FlexiJSONCoder
Working with JSON in iOS apps can sometimes be a hassle, especially when dealing with dynamic data types or custom decoding requirements. That’s where the FlexiJSONCoder library comes in!. This lightweight library is designed to simplify JSON encoding and decoding in Swift, making your code cleaner and more manageable.
Why Use FlexiJSONCoder?
- Ease of Use: FlexiJSONCoder provides straightforward functions for JSON encoding and decoding.
- Flexibility: Handles nested and complex JSON structures seamlessly.
- Error Handling: Built-in mechanisms to handle encoding and decoding errors gracefully.
Simple Use Case Example
Let’s look at a basic example to see how FlexiJSONCoder works in practice.
Scenario
Imagine you have the following JSON response & model for your API:
// JSON Response
{
"id": 123,
"name": "John Doe",
"email": "john.doe@example.com",
"storeCount": ,
"isActive": "true",
}
// Model
struct User: Codable {
let id: String?
let name: String?
let email: String?
let isActive: Bool
let isRedable: Bool
let categoty: Int
let storeCount: Int
}
Now lets consider these above scenarios :
- Missing one pair( key, value ) from json but key is available in model with non-optional case
- Missing value from json which leads to Null exception with non-optional case
- Incoming value datatype is not matched with model datatype in both optional and non-optional case
// Decoded Model with FlexiJSONCoder:
do {
let jsonData = try JSONSerialization.data(withJSONObject: incomingDictionary, options: [.fragmentsAllowed])
let decodedModel = try FlexiJSONCoder().decode(User.self, from: jsonData)
print(decodedModel)
} catch {
print("Decoding failed: \(error)")
}
let id: String?
let name: String?
let email: String?
let isActive: Bool
let isRedable: Bool
let categoty: Int
let storeCount: Int
{
"id": 123,
"name": "John Doe",
"email": "john.doe@example.com",
"storeCount": ,
"isActive": "true",
}
// Outputs:
User(
id: "123",
name: "John Doe",
email: "john.doe@example.com",
isActive: true,
isRedable: false,
categoty: 0,
storeCount: 0,
)
Conclusion
FlexiJSONCoder is an tool for any iOS developer looking to streamline JSON handling in their projects. Its simple and flexible for working with dynamic and complex JSON structures.
To learn more, visit the FlexiJSONCoder GitHub repository.