json2ts

command module
v0.0.0-...-0eb1d6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 1 Imported by: 0

README

json2ts

json2ts converts JSON into valid Typescript classes with decorators.

Installation

If you have go already installed you can simply run

go get github.com/sno6/json2ts

If you prefer a binary, you can also find those in the releases section

Example

Take some JSON, such as this example from an HTTP response

{
  "data": [{
	  "status": "success",
	  "flag": true,
	  "array": [{
		  "ofData": "Also supports any level of depth!"
	  }]
  }]
}

Pass the JSON through json2ts

# Either from the clipboard (OSX)
>>> pbpaste | json2ts -d

# Or from a file
>>> json2ts -i my_file.json -d

and voila, your Typescript generated code

export class BaseClass {
	@IsDefined()
	@IsArray()
	@Type(() => Data)
	@ValidateNested({ each: true })
	public data!: Data[];
}

export class Data {
	@IsDefined()
	@IsString()
	public status!: string;

	@IsDefined()
	@IsBoolean()
	public flag!: boolean;

	@IsDefined()
	@IsArray()
	@Type(() => Array)
	@ValidateNested({ each: true })
	public array!: Array[];
}

export class Array {
	@IsDefined()
	@IsString()
	public ofData!: string;
}

Usage

Transform JSON into typescript classes

Usage:
  json2ts [flags]

Flags:
  -d, --decorators      Add decorators to class parameters
  -i, --input string    Optional input file
  -o, --output string   Optional output file (defaults to stdout)
  -p, --prefix string   Prefix for all class names
  -r, --root string     Name of the root class
  -h, --help            help for json2ts

License

MIT

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL