Introduction
Welcome to the documentation for the SexyJson Cocoapod. SexyJson is a lightweight library designed to simplify the handling and parsing of JSON data in your Swift projects. With SexyJson, you can easily convert JSON data into Swift models, making it easier to work with JSON responses from APIs or other sources.
Features
- Automatic conversion of JSON data to Swift models
- Supports nested objects and arrays
- Easy mapping of JSON keys to Swift model properties
- Customizable property types and mappings
- Elegant and expressive syntax
Installation
To install SexyJson using Cocoapods, add the following line to your Podfile:
“`ruby
pod ‘sexyjson’
“`
Then, run the following command:
“`terminal
pod install
“`
Import SexyJson in your Swift files to start using it:
“`swift
import SexyJson
“`
Usage
Basic Usage
In order to use SexyJson, you need to create a struct or class that represents your data model. Ensure that your model conforms to the SexyJson
protocol. Here’s an example:
“`swift
// Import SexyJson
import SexyJson
// Define your data model
struct Person: SexyJson {
var name: String
var age: Int
}
// Create JSON data
let jsonData = “””
{
“name”: “John Doe”,
“age”: 25
}
“””.data(using: .utf8)!
// Parse JSON data to your model
let person = try! Person.deserialize(from: jsonData)
// Access the parsed data
print(person.name) // Output: John Doe
print(person.age) // Output: 25
“`
Custom Mapping
In cases where your JSON keys don’t match your model’s properties exactly, you can define custom mappings using the SexyJsonProperty
protocol. Here’s an example:
“`swift
struct Car: SexyJson {
var brand: String
var manufacturingYear: Int
var color: String
enum CodingKeys: String, SexyJsonProperty {
case brand = “car_brand”
case manufacturingYear = “year_of_manufacture”
case color
}
}
“`
Contributions
If you are interested in contributing to SexyJson, please follow our guidelines outlined in the GitHub repository. We welcome bug reports, feature requests, and pull requests to help improve SexyJson for the community.
License
SexyJson is released under the MIT license. Please see the LICENSE file for more details.