Simplify Your JSON decoding with FlexiJSONCoder

Sathish Kumar
2 min readDec 13, 2024

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sathish Kumar
Sathish Kumar

Written by Sathish Kumar

Hands on experience in iOS | Swift UI

No responses yet

Write a response