Jenerator
JSON Document ⇨ Crystal type definitions
Jump-start your next JSON API client by generating types from a sample API response!
The results this outputs are meant as a starting off point, to eliminate having
to write a lot of the boilerplate involved in creating a new project to parse
the response from an API, without resorting to the runtime overhead of relying
on JSON.parse
. You will probably want to review the generated code and make
some revisions, especially if your data sample includes arrays of objects (see
Known Issues).
Installation
-
Add the dependency to your
shard.yml
:dependencies: jenerator: github: dscottboggs/jenerator
-
Run
shards install
Usage
require "jenerator"
sample = %[{"data": "just some text", "listOfData": ["one", 2, 3.0]}]
puts Jenerator.process sample
The above code sample would output this Crystal code:
require "json"
class Document
include JSON::Serializable
@data : String
@[JSON::Field(key: "listOfData")]
@list_of_data : Array(String | Int64 | Float64)
end
There's also a compiled script which you can download as a static binary from
the "releases"
page which will generate new Crystal source files for each .json
file in a
directory.
Known issues:
When generating code for an array of objects, a new class or struct is generated for each object member of that array. The resulting type eliminates redundant types from the union, but the types are still declared.
See jenerator_spec.cr for an example.
Contributing
- Fork it (https://github.com/dscottboggs/jenerator/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- D. Scott Boggs - creator and maintainer