Tyson allows you to use TypeScript interfaces for JSON with enhanced features:
- Re-use Typescript interfaces for JSON schemas.
- Allows comments in JSON
- No need for key values to be strings
- Can use variables for JSON
- Import all configs into a singular config
# Install globally
npm install -g @razroo/tyson
# Or install locally in your project
npm install --save @razroo/tyson
// test.interface.ts
export interface TsonTest {
title: string;
position: number;
type: string;
}
// test.tyson or test.tson
import { TsonTest } from './test.interface';
// Comments are allowed!
{: TsonTest
title: "sample title",
position: 0,
type: "sample type", // Inline comments work too
}
# CLI usage
tyson test.tyson test.json
# With options
tyson --input test.tyson --output test.json --interface test.interface.ts --interface-name TsonTest
import { compileTyson, parseTyson } from 'tyson';
// Compile a tyson file to JSON
compileTyson({
inputFile: 'test.tyson',
outputFile: 'test.json',
interfaceFile: 'test.interface.ts',
interfaceName: 'TsonTest'
});
// Or parse a tyson file and get the data
const data = parseTyson({
inputFile: 'test.tyson',
interfaceFile: 'test.interface.ts',
interfaceName: 'TsonTest'
});
The compiled JSON will look like:
{
"title": "sample title",
"position": 0,
"type": "sample type"
}
- Type Checking: Validates your JSON against TypeScript interfaces
- Comments: Include comments in your JSON files that won't appear in the output
- Unquoted Keys: No need to quote object keys
- TypeScript Integration: Seamlessly work with your existing TypeScript codebase
Contributions are welcome! Please feel free to submit a Pull Request.