jsonparserswift

Welcome to the documentation for the JSONParserSwift library. This library provides a lightweight and efficient way to parse JSON data in your Swift projects. Whether you are working with API responses, data serialization, or any other JSON-related tasks, JSONParserSwift is here to help.

Installation

To install JSONParserSwift, you have a few options:

pod 'JSONParserSwift'
  • Using Swift Package Manager (SPM):
.package(url: "https://github.com/example/JSONParserSwift.git", from: "1.0.0")
  • Manual installation:

You can download the library’s source code from the GitHub repository and manually add the files to your project.

Usage

Using JSONParserSwift is simple and straightforward. Here’s an example:

import JSONParserSwift

let jsonString = """
{
  "name": "John",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York"
  },
  "phoneNumbers": ["123", "456"]
}
"""

struct Person: JSONDecodable {
    var name: String
    var age: Int
    var address: Address
    var phoneNumbers: [String]

    init(json: JSON) throws {
        name = try json.value(forKey: "name")
        age = try json.value(forKey: "age")
        address = try json.value(forKey: "address")
        phoneNumbers = try json.value(forKey: "phoneNumbers")
    }
}

struct Address: JSONDecodable {
    var street: String
    var city: String

    init(json: JSON) throws {
        street = try json.value(forKey: "street")
        city = try json.value(forKey: "city")
    }
}

do {
    let json = try JSON(parse: jsonString)
    let person = try json.decode(to: Person.self)
    print(person)
} catch {
    print("Failed to parse JSON: \(error)")
}

The example above demonstrates how to parse a JSON string into a Swift object. The `Person` struct conforms to the `JSONDecodable` protocol, allowing it to be initialized from JSON data. JSONParserSwift provides various helper methods for retrieving values based on their keys. In this example, we retrieve values for `name`, `age`, `address`, and `phoneNumbers`.

Additional Functionality

Encoding JSON

In addition to parsing JSON, JSONParserSwift also supports encoding Swift objects into JSON data. Here’s an example:

struct Employee: JSONEncodable {
    var name: String
    var age: Int
    var address: Address
}

let employee = Employee(name: "Jane", age: 25, address: Address(street: "456 Elm St", city: "San Francisco"))

do {
    let json = try JSON(encode: employee)
    print(json)
} catch {
    print("Failed to encode JSON: \(error)")
}

In this example, the `Employee` struct conforms to the `JSONEncodable` protocol, allowing it to be encoded into JSON data. The resulting JSON object can be printed or used in any other JSON-related tasks.

Contributing

We welcome contributions to JSONParserSwift. If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository. Additionally, feel free to submit pull requests with bug fixes or new features.

License

JSONParserSwift is released under the MIT license. See the LICENSE file for more details.