@kson_org/kson
v0.2.1
Published
KSON - Extended JSON format with comments and more
Readme
JavaScript/TypeScript bindings for kson-lib API
KSON (Kson Structured Object Notation) combines the best of JSON and YAML—robust and efficient like JSON, clean and readable like YAML.
Example Usage
Write some code:
import { Kson, Result } from '@kson_org/kson';
const kson = Kson.getInstance();
// Convert KSON to JSON
const result = kson.toJson('key: [1, 2, 3, 4]');
if (result instanceof Result.Success) {
console.log(result.output);
}Running this should print the following:
{
"key": [
1,
2,
3,
4
]
}TypeScript Usage
This package includes TypeScript definitions out of the box:
import { Kson, Result, FormatOptions, IndentType, FormattingStyle } from 'kson';
const kson = Kson.getInstance();
const ksonString = `
person:
name: 'Leonardo Bonacci'
nickname: Fibonacci
age: 42
`;
// Convert KSON to JSON
const jsonResult: Result = kson.toJson(ksonString);
if (jsonResult instanceof Result.Success) {
const data = JSON.parse(jsonResult.output);
console.log(data);
} else if (jsonResult instanceof Result.Failure) {
console.error('Errors:', jsonResult.errors);
}
// Format KSON (returns string directly)
const formatted: string = kson.format(ksonString);
console.log(formatted);
// Format with options
const options = new FormatOptions(
new IndentType.Spaces(2),
FormattingStyle.PLAIN
);
const formattedWithOptions = kson.format(ksonString, options);
console.log(formattedWithOptions);
// Convert to YAML
const yamlResult = kson.toYaml(ksonString);
if (yamlResult instanceof Result.Success) {
console.log(yamlResult.output);
}API Reference
Core Methods
toJson(kson: string, retainEmbedTags?: boolean): ResultConverts KSON to formatted JSON. Returns aResultwhich can be either:Result.Successwith anoutputproperty containing the JSON stringResult.Failurewith anerrorsproperty containing a list of error messages
toYaml(kson: string, retainEmbedTags?: boolean): ResultConverts KSON to YAML format. Returns aResultwith Success/Failure variants.format(kson: string, formatOptions?: FormatOptions): stringFormats KSON string according to specified style. Returns the formatted string directly.analyze(kson: string): AnalysisAnalyzes KSON and returns diagnostic information including tokens and any errors.parseSchema(schemaKson: string): SchemaResultParses and validates a KSON schema. Returns aSchemaResultwhich can be:SchemaResult.Successwith aschemaValidatorfor validating KSON documentsSchemaResult.Failurewith error messages
Helper Types
FormatOptions: Configuration for formattingindentType: EitherIndentType.Spaces(size)orIndentType.TabsformattingStyle: One ofFormattingStyle.PLAIN,FormattingStyle.DELIMITED, orFormattingStyle.COMPACT
Result: Success/Failure union type for operations that can failResult.Success: Containsoutputproperty with the result stringResult.Failure: Containserrorsproperty with error messages
SchemaResult: Success/Failure for schema parsingSchemaResult.Success: ContainsschemaValidatorfor validating documentsSchemaResult.Failure: Contains error messages
KSON Syntax Highlights
# Comments are supported
person:
name: 'Leonardo Bonacci'
nickname: Fibonacci # Quotes are optional for simple strings
age: 42
# Arrays can be written in multiple styles
favorite_books:
- title: Elements
author: Euclid
- title: Metaphysics
author: Aristotle
# Nested arrays and complex values
favorite_numbers: [0, 1, 1, 2, 3, 5, 8]
golden_ratio: '(1 + √5)/2'Environment Support
This package uses conditional exports to provide optimized builds for different environments:
- Node.js: ES modules optimized for server-side use
- Browser: ES modules optimized for client-side use
- TypeScript: Full type definitions included
The correct version is automatically selected based on your environment.
Platform Compatibility
The JavaScript bindings are compiled from Kotlin/JS and work across all modern JavaScript environments. The package includes:
- Pre-compiled ES modules for both browser and Node.js
- TypeScript type definitions
- Source maps for debugging
No additional setup or native binaries are required - everything works out of the box on all platforms that support JavaScript.
Links
License
Apache-2.0
