Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fiorix/jsonschema2go
Code generator for JSON schema
https://github.com/fiorix/jsonschema2go
code-generation golang json-schema thrift
Last synced: about 2 months ago
JSON representation
Code generator for JSON schema
- Host: GitHub
- URL: https://github.com/fiorix/jsonschema2go
- Owner: fiorix
- License: other
- Created: 2018-08-06T00:09:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-07T23:18:27.000Z (about 6 years ago)
- Last Synced: 2024-10-15T11:21:39.491Z (2 months ago)
- Topics: code-generation, golang, json-schema, thrift
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 39
- Watchers: 6
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonschema2go
[![Build Status](https://secure.travis-ci.org/fiorix/jsonschema2go.png)](http://travis-ci.org/fiorix/jsonschema2go)
jsonschema2go is a code generator for JSON schemas. Supports schemas from local files or URL, and generates Go code, or thrift spec.
This is a very naive and incomplete implementation. I wrote this code specifically to codegen the [NVD JSON schema](https://nvd.nist.gov/vuln/data-feeds#JSON_FEED), based on a few requirements:
* The output is a single file
* Consistent output given same input
* Capable of generating at least Go and Thrift### Download, install
Requires Go 1.10 or newer. The generated thrift spec requires thrift compiler 0.11 or newer.
Assuming you have a working Go environment:
```
go get github.com/fiorix/jsonschema2go
go install github.com/fiorix/jsonschema2go
```Output binary is $GOPATH/bin/jsonschema2go.
### Usage
```
use: ./jsonschema2go [flags] schema.json
The input schema may be a local file or URL.
-gen string
set output format: go, thrift (default "go")
-gofmt
run gofmt on generated go code
-gopkg string
set name of generated go package (default "schema")
-goptr
generate go struct fields as pointers
-o string
set name of output file (default "-")
-thriftns string
set comma separated list of thrift namespaces (default "go schema,py schema")
```---
Generate Go code:
```
jsonschema2go -gen go https://csrc.nist.gov/schema/nvd/feed/1.0/nvd_cve_feed_json_1.0.schema
```Generate Thrift spec:
```
jsonschema2go -gen thrift https://csrc.nist.gov/schema/nvd/feed/1.0/nvd_cve_feed_json_1.0.schema
```### Generated type names
Naming is hard. In jsonschema2go I made the choice to name generated Go and Thrift types after their respective filenames. In JSON schema, the body of the document is a type, like a struct, and it has no name. Hence using the filename with some normalization to idiomatic Go, with an adjusted list of keywords (e.g. CVE) for initialisms.
You can see what it looks like in the golden files in the testdata directory.