Description
RDHJSONAccessors is a Swift library that provides an easy way to access data in JSON format. With this library, you can simplify the process of retrieving data from JSON structures and avoid nested optional unwrapping.
Features
- Simplified access to JSON data
- Avoidance of nested optional unwrapping
- Support for complex and nested JSON structures
- Automatic type casting of JSON values
- Easy handling of optional and default values
- Convenient error handling for missing or incompatible data
Getting Started
- Install RDHJSONAccessors using CocoaPods or manually include the library in your project.
- Import RDHJSONAccessors in your Swift file:
import RDHJSONAccessors
- Retrieve JSON data from a source (e.g., network response, local file).
- Fetch the desired values using the provided accessors and utilities.
Usage
// Assuming you have the following JSON structure:
let jsonData = """
{
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com",
"address": {
"street": "123 Main St",
"city": "New York",
"country": "USA"
},
"interests": ["programming", "reading", "travelling"]
}
"""
// Access the JSON data using RDHJSONAccessors:
let json = try JSONSerialization.jsonObject(with: jsonData.data(using: .utf8)!, options: [])
let accessor = RDHJSONAccessor(json: json)
// Access simple values
let name: String = try accessor.value("name")
let age: Int = try accessor.value("age")
let email: String = try accessor.value("email")
// Access nested values
let street: String = try accessor.value("address.street")
let city: String = try accessor.value("address.city")
let country: String = try accessor.value("address.country")
// Access array values
let interests: [String] = try accessor.value("interests")
// Access optional values with default fallback
let middleName: String = try accessor.value("middleName", default: "")
// Access optional values and handle missing or incompatible data
let optAge: Int? = try? accessor.value("age")
let nonExistingKey: String? = try? accessor.value("nonExistingKey")
Requirements
- Swift 5.0+
Installation
RDHJSONAccessors can be installed via CocoaPods or manually by including the source files in your project.
CocoaPods
Add the following line to your Podfile:
pod 'RDHJSONAccessors'
Then run the following command:
$ pod install
Manual Installation
- Download and extract the source files from the RDHJSONAccessors repository.
- Copy the RDHJSONAccessors.swift file into your project.
Author
RDHJSONAccessors is developed and maintained by Your Name.
License
RDHJSONAccessors is released under the MIT License. See the LICENSE file for details.