Skip to content

Repository files navigation

Static BadgeStatic BadgeStatic BadgeStatic BadgeStatic BadgeStatic BadgeStatic Badge

FlexiJSONCoder for Swift

FlexiJSONCoder 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.

Features:

  • Default Value Handling: Automatically applies default values for any missing keys or values in the JSON.
  • Type Coercion: Converts incoming data types (e.g., String, Int, Float, Double, Bool) to the model's expected type seamlessly.
  • Null Value Management: Replaces null values with predefined default values, ensuring smooth processing.
  • Data Integrity: Generates clear and actionable errors for unsupported type conversions while maintaining data consistency.
  • Easy Integration: Compatible with your existing Swift codebase and leverages the Decodable protocol.
  • Customizable: Supports extensions for additional data types, such as Date, URL, and more, for future-proof development

Supported Datatypes :

Version 0.0.1: This release with support for String, Int, Float, Double, and Bool coercion.

Example

Download the example project

Installation

FlexiJSONCoder is available through Swift Package Manager

Features

Swift Package Manager

  • In Xcode select:
File > SwiftPackages > AddPackageDependency...
  • Then paste this URL:
https://github.com/Forgwhiz/FlexiJSONCoder.git

Cocoapods

Add the following line to your Podfile:

pod'FlexiJSONCoder','~> 0.0.1'

Then run:

pod install

or

pod update

Demo

Basic Usage

Usage of the library is simple and can be seen in the example provided below. Here's how to use FlexiJSONCoder in your Swift model:

import FlexiJSONCoder
structWelcomeModel:Codable{letid:String?lettitle:Stringlettype:Int?letcategory:Float?}letjsonData="""{"id": "123","title": "Welcome","category": "45.67","isActive": "true",}""".data(using:.utf8)!
do{letjsonData=tryJSONSerialization.data(withJSONObject: dictionary, options:[.fragmentsAllowed])letdecodedModel=tryFlexiJSONCoder().decode(WelcomeModel.self, from: jsonData)print(decodedModel) // Outputs: WelcomeModel(id: "123", title: "Welcome", type: 0, category: 45.67, isActive: true)
}catch{print("Decoding failed: \(error)")}

Options for Custom Types

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. FlexiJSONCoder is built with extensibility in mind.

Supported Types

CommandDescription
StringCan be converted from String, Int, Float, Double
IntCan be converted from String, Float, Double
FloatCan be converted from String, Int, Double
DoubleCan be converted from String, Int, Float
BoolCan be converted from String ("true"/"false") or Int (1 for true, 0 for false)

Customizing for Future Types

You can extend the library to support additional types (like Date, URL, etc.)

License

FlexiJSONCoder is available under the MIT license. See the LICENSE file for more information.


About

FlexiJSONCoder is a Swift library that simplifies decoding JSON data with type coercion and fallback support. It handles inconsistent data structures by automatically converting fields to the expected types

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages