jsonmodel

JsonModel is an open-source library for iOS and macOS developers that simplifies the process of parsing and validating JSON data in your Swift projects. With JsonModel, you can easily map JSON objects to Swift classes, saving you time and reducing the complexity of JSON parsing in your applications.

Installation

To install JsonModel, follow these simple steps:

  1. Open your project in Xcode
  2. Go to File > Swift Packages > Add Package Dependency
  3. Enter the URL of the JsonModel GitHub repository: https://github.com/jsonmodel/jsonmodel
  4. Click Next, then choose the latest version of JsonModel
  5. Click Finish to complete the installation

Usage

JsonModel provides a straightforward way to map JSON data to your Swift classes using simple annotations. Here’s how you can use JsonModel in your project:

  1. Create a new Swift class that represents your JSON object.
  2. Add the `@objcMembers` attribute to your class to enable Objective-C runtime features.
  3. Declare properties in your class and annotate them using the `@objc` attribute.
  4. Use additional annotations, such as `@optional` and `@arrayType`, to specify optional properties and array types, respectively.
  5. Implement the `JSONModel` protocol by extending your class and defining the `required init?(dictionary: NSDictionary)` initializer.
  6. Inside the initializer, use the `JSONKeyMapper` class to map JSON keys to your property names.
  7. Instantiate your class using the `JSONModel` static method `model(withDictionary: NSDictionary)` or `modelArray(withDictionaries: NSArray)` for array types.
  8. Enjoy the convenience of accessing your JSON data as Swift objects!

Example

Let’s suppose we have a JSON object representing a user:


{
  "id": 123,
  "name": "John Doe",
  "email": "john@example.com"
}

To map this JSON object to a Swift class using JsonModel, follow these steps:

  1. Create a new Swift class:
        
        @objcMembers
        class User: JSONModel {
            @objc var id: NSNumber?
            @objc var name: String?
            @objc var email: String?
            
            required init?(dictionary: NSDictionary) {
                super.init(dictionary: dictionary)
            }
            
            override func propertyIsOptional(_ propertyName: String) -> Bool {
                return true
            }
        }
        
    
  2. Instantiate the class with the JSON dictionary:
        
        let jsonDict: NSDictionary = ... // JSON dictionary
        let user = User.model(withDictionary: jsonDict)
        
        // Access the properties
        let userId = user?.id
        let userName = user?.name
        let userEmail = user?.email
        
    

Conclusion

JsonModel simplifies JSON parsing and mapping in your Swift projects by providing a straightforward way to map JSON objects to Swift classes. With its annotations and protocol implementation, you can easily convert JSON data into Swift objects, saving development time and effort. Start using JsonModel in your projects today and enjoy the benefits of simplified JSON parsing!

Additional Resources