Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/calavera/json_generate
Generate go structs based on JSON examples
https://github.com/calavera/json_generate
Last synced: about 2 months ago
JSON representation
Generate go structs based on JSON examples
- Host: GitHub
- URL: https://github.com/calavera/json_generate
- Owner: calavera
- License: mit
- Created: 2015-01-16T19:49:11.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-01-16T19:52:12.000Z (almost 10 years ago)
- Last Synced: 2024-04-14T09:16:00.976Z (9 months ago)
- Language: Go
- Homepage:
- Size: 133 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# JSON Generate
JSON Generate is a tool to create go structs based on JSON examples.
## Installation
```
$ go get github.com/calavera/json_generate
```## Usage
Add a JSON example as a constant in your go code. Something like:
```go
const JSONExample_User = `{
"name": "calavera"
}`
```The constant MUST have the prefix `JSONExample_` in its name.
Then run the `json_generate` script with something like:
```
$ json_generate filename.go
```Or add the `generate` header to the source file:
```go
// go:generate json_generate
```This will generate a `filename_json.go` file with the go struct parsed. Something like:
```go
type User struct {
Name string `json:"name,omitempty"`
}
```## Options
- output: Flag to specify the output where the structs are written.
- example: Flag to limit the json examples you want to parse.
- alias: Flag to convert inner json maps into other structs.
- types: Flag to convert fields into specific go types.
- debug: Print the genearted structs in STDOUT rather than into the output file.## FAQ
**Is this program any good?**
It avoided me the hassle of writing a bunch of json structs by hand. It won't probably work for 100% of your use cases, but it can save you some time.
**Did you spend more time working on this than it would have taken you to write those structs by hand?**
Definitely.
**Did you learn something new about go by doing this rather than writing a bunch of boilerplate code?**
Definitely.
## LICENSE
MIT