ademanuele/GenSON
A Swift library that generates JSON from Decodable models.
β¨ Features
- β
Automatically generates JSON from any
Decodabletype - β Supports nested models, optionals, arrays, dictionaries, etc.
- β Configurable generated JSON values
- β Zero setup required
π§ Installation
Swift Package Manager
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/ademanuele/GenSON.git", from: "0.0.1")
]Then import it in your code:
import GenSONπ Usage
Generate JSON for a Type
struct User: Decodable {
let id: String
let name: String
let isActive: Bool
}
let json: String = try User.generate()
print(json)Output:
{
"id" : "1uTih9ZU1yJcIEFsVFHSoYelf32NPHssVR8eqLUGk",
"isActive" : true,
"name" : "qiMkyHIthSfxkdx2TbICDVq5KuxvBavZwkqrYpbXUXTIxIOlGXTrIyiZgpraF"
}Optionally provide JSON generation options
let json: String = try User.generate(options: .init(stringLength: 3...5))
print(json)Output:
{
"id" : "yhGf",
"isActive" : true,
"name" : "Ldc"
}π€― A Note About Enums
Generating values from a given enum type is really difficult in Swift it seems. For this library to generate correct values for your Decodable enums they must:
- Have no associated types
- Inherit from a type that is
Decodable, such asStringorInt - Conform to the
CaseIterableprotocol
π§ How It Works
GenSON works by passing the given Decodable model through a custom Foundation Decoder which navigates the object structure and generates dummy values for every property of the model.
π Known Limitations & Issues
- Decoding of
enumtypes is not supported yet. - Providing your own values for a given object property is not supported yet.
π€ Contributing
Contributions are very welcome!
- Fork the repo
- Create a new branch (git checkout -b feature/my-feature)
- Make your changes and commit (git commit -am 'Add new feature')
- Push to GitHub (git push origin feature/my-feature)
- Submit a Pull Request π
Made with β€οΈ in Swift.
Package Metadata
Repository: ademanuele/GenSON
Stars: 4
Forks: 0
Open issues: 1
Default branch: main
Primary language: swift
License: GPL-3.0
README: README.md