json-to-toon
v1.0.0
Published
A TypeScript library for converting between JSON and TOON formats - a structured, human-readable representation of JSON data
Downloads
10
Maintainers
Readme
JSON to TOON Converter
A TypeScript library for converting between JSON and TOON formats. The TOON format is a structured, human-readable representation of JSON data that presents information in a tabular and hierarchical format.
Installation
npm install json-to-toonUsage
Converting JSON to TOON
import { toToon } from 'json-to-toon';
const json = {
metadata: { version: 1, author: "test" },
items: [
{ id: 1, name: "Item1" },
{ id: 2, name: "Item2" }
],
tags: ["alpha", "beta", "gamma"]
};
const toon = toToon(json);
console.log(toon);
// Output:
// metadata:
// version: 1
// author: test
// items:
// [2,]{id,name}:
// 1,Item1
// 2,Item2
// tags:
// [3]: alpha,beta,gammaConverting TOON back to JSON
import { fromToon } from 'json-to-toon';
const toon = `metadata:
version: 1
author: test
tags:
[3]: alpha,beta,gamma`;
const json = fromToon(toon);
console.log(json);
// Output:
// {
// metadata: { version: 1, author: 'test' },
// tags: ['alpha', 'beta', 'gamma']
// }API
toToon(json: JsonValue): string
Converts a JSON value to its TOON string representation.
fromToon(toon: string): JsonValue
Converts a TOON string back to its original JSON value.
JsonToToonConverter
A class containing the core conversion methods:
static toToon(json: JsonValue): stringstatic fromToon(toon: string): JsonValue
TOON Format Specification
The TOON format is a structured representation of JSON data with the following rules:
Basic Types
null→nulltrue→truefalse→false- Numbers → Plain numbers (e.g.,
42,3.14) - Strings → Plain strings (e.g.,
hello)
Arrays
- Simple arrays →
[count]: value1,value2,value3 - Object arrays with consistent structure →
[count,]{key1,key2}: value1,value2
Objects
- Simple objects → Key-value pairs with indentation
- Nested objects → Hierarchical structure with proper indentation
Examples
Simple object:
name: John
age: 30
Nested object:
user:
name: John
age: 30
Array of primitives:
tags: [3]: alpha,beta,gamma
Array of objects:
items: [2,]{id,name}:
1,Item1
2,Item2Development
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
# Run tests in watch mode
npm run test:watchLicense
MIT
