Skip to content

Go API Reference

You can use JSSON as a library in your Go projects to transpile JSSON content programmatically.

Terminal window
go get github.com/carlosedujs/jsson

The core package is jsson/internal/transpiler.

package main
import (
"fmt"
"jsson/internal/lexer"
"jsson/internal/parser"
"jsson/internal/transpiler"
)
func main() {
input := `
server {
port = 8080
debug = true
}
`
// 1. Lexer
l := lexer.New(input)
// 2. Parser
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) > 0 {
panic(p.Errors())
}
// 3. Transpiler
// New(program, workingDir, includeMode, sourceFile)
t := transpiler.New(program, ".", "keep", "main.jsson")
// Transpile to JSON (Default)
jsonOutput, _ := t.Transpile()
fmt.Println(string(jsonOutput))
}

The Transpiler struct provides methods for different output formats:

Transpiles the AST to JSON.

output, err := t.Transpile()
// output is []byte containing JSON

Transpiles the AST to YAML.

output, err := t.TranspileToYAML()
// output is []byte containing YAML

Transpiles the AST to TOML.

output, err := t.TranspileToTOML()
// output is []byte containing TOML

Transpiles the AST to TypeScript with type definitions.

output, err := t.TranspileToTypeScript()
// output is []byte containing TypeScript code

All transpile methods return ([]byte, error). You should always check the error returned.

output, err := t.Transpile()
if err != nil {
log.Fatalf("Transpilation failed: %v", err)
}