Skip to content
voidmergePublic

About

Leafdata is a human-readable data format similar to JSON or TOML that can encode bigints and byte arrays.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Leafdata (@voidmerge/leafdata)

NPM License NPM Version

Leafdata is a human-readable data format similar to JSON or TOML that can encode bigints and byte arrays.

  • Simple, flexible format makes data obvious for human editing.
  • Encode public keys along with other configuration data.
  • Transcode other formats that can contain binary data, such as Message Pack for logging or debugging.
  • Natively parses JSON objects.

Example

# This is leafdata!

# Wouldn't this be nice : )
bugCount = 0

# Big Integer? No Biggie.
huge = bigint@"9876543210000000000000000"

# What about those public key bytes?
pubKey = b64@"68KJMg9WmuadUM3dUVdiaXW7898qanhIuvtftFxCPXw="

# Or, for more human readable binary data...
# Leafdata will pick the smaller of b64@ or pct@ when encoding.
myNullSeparatedData = pct@"age%00color%0A42%00red"

# We gotz tablez.
tab = {
  fruit1 = apple
  fruit2 = grape
}

# Wait a sec... that'd be better as an array:
fruit = [
  apple
  grape
]
import { leafdataParse, leafdataRender } from '@voidmerge/leafdata`;

// Parse
const js = leafdataParse(exampleString);
{
  bugCount: 0,
  huge: 9876543210000000000000000n,
  pubKey: ArrayBuffer(),
  myNullSeparatedData: ArrayBuffer(),
  tab: { fruit1: "apple", fruit2: "grape" },
  fruit: [ "apple", "grape" ],
}
// Render
const output = leafdataRender(js);

Specification

The Antlr4 Lexer and Parser g4 files in this repo are the specification:

Why Antlr4 when there are faster parsers? It is a well recognized format that compiles into many languages. Feel free to write a new, faster parser, just make it compliant with this one.

API Documentation

Namespaces

Functions

leafdataParse()

leafdataParse(doc: string): JsObj

Parse a leafdata document string into a Javascript object.

Parameters

Parameter Type
doc string

Returns

JsObj


leafdataRender()

leafdataRender(obj: JsObj, indent?: string): string

Render a Javascript object into a leafdata document string.

Parameters

Parameter Type Description
obj JsObj the js object to render
indent? string if omitted, the tree will be generated without pretty printing to conserve space. If specified, the content will be pretty printed for humans.

Returns

string

About

Leafdata is a human-readable data format similar to JSON or TOML that can encode bigints and byte arrays.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages