json-to-toon-converter
v1.0.0
Published
A TypeScript package to convert JSON to Toon format and vice versa
Maintainers
Readme
JsonToToon
A TypeScript package that converts JSON to a compact "Toon" format and vice versa. The Toon format is a human-readable, space-efficient alternative to JSON that maintains full compatibility with all JSON data types.
Installation
# Using pnpm
pnpm add JsonToToon
# Using npm
npm install JsonToToon
# Using yarn
yarn add JsonToToonUsage
Convert JSON to Toon Format
import { jsonToToon } from 'JsonToToon';
// From JSON string
const json = '{"name": "John", "age": 30, "active": true}';
const toon = jsonToToon(json);
console.log(toon); // {name:"John",age:30,active:true}
// From JavaScript object
const obj = { name: 'John', age: 30, active: true };
const toon2 = jsonToToon(obj);
console.log(toon2); // {name:"John",age:30,active:true}Convert Toon Format to JSON
import { toonToJson } from 'JsonToToon';
const toon = '{name:"John",age:30,active:true}';
const json = toonToJson(toon);
console.log(json); // {"name":"John","age":30,"active":true}Round-trip Conversion
import { jsonToToon, toonToJson } from 'JsonToToon';
const original = '{"users": [{"name": "John", "scores": [10, 20, 30]}]}';
const toon = jsonToToon(original);
const back = toonToJson(toon);
// The data is preserved
const originalObj = JSON.parse(original);
const backObj = JSON.parse(back);
console.log(originalObj); // Same as backObjToon Format Specification
The Toon format is a compact representation of JSON data:
- Objects:
{key:value,key2:value2}(no quotes on valid identifier keys, minimal whitespace) - Arrays:
[item1,item2,item3] - Strings:
"text"or'text'(quoted) - Numbers:
123,45.67,-10 - Booleans:
true,false - Null:
null - Nested structures: Fully supported
Examples
| JSON | Toon Format |
|------|-------------|
| {"name": "John"} | {name:"John"} |
| [1, 2, 3] | [1,2,3] |
| {"active": true, "count": 42} | {active:true,count:42} |
| {"nested": {"key": "value"}} | {nested:{key:"value"}} |
API Reference
jsonToToon(json: string | object): string
Converts a JSON string or JavaScript object to Toon format.
Parameters:
json- A JSON string or JavaScript object
Returns: A string in Toon format
Throws: Error if the input is invalid JSON
toonToJson(toon: string): string
Converts a Toon format string to JSON string.
Parameters:
toon- A string in Toon format
Returns: A JSON string
Throws: Error if the input is invalid Toon format
Features
- ✅ Full support for all JSON data types
- ✅ Nested objects and arrays
- ✅ TypeScript support with type definitions
- ✅ Zero dependencies
- ✅ Comprehensive test coverage
- ✅ Round-trip conversion maintains data integrity
Development
Building
pnpm run buildTesting
pnpm testWatch Mode
pnpm run test:watchLicense
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
