Contents

ademanuele/GenSON

A Swift library that generates JSON from Decodable models.

✨ Features

  • βœ… Automatically generates JSON from any Decodable type
  • βœ… 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 as String or Int
  • Conform to the CaseIterable protocol

🧠 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 enum types 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