tabnasjsonic

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 4 Imported by: 0

README

jsonic (Go)

Version: 0.1.22

A Go port of jsonic, the lenient JSON parser. Same architecture, same syntax, same results. If you already use jsonic in TypeScript, you know what this does. If you don't, read on.

jsonic accepts all standard JSON -- and then goes further. Unquoted keys, implicit objects, comments, trailing commas, single-quoted strings, multiline strings, path diving, and more. It parses what you meant, not just what you typed.

Install

go get github.com/tabnas/jsonic/go@latest

Quick Example

package main

import (
    "fmt"
    "github.com/tabnas/jsonic/go"
)

func main() {
    result, err := tabnasjsonic.Parse("a:1, b:2")
    if err != nil {
        panic(err)
    }
    fmt.Println(result) // map[a:1 b:2]
}

That's it. No schema, no struct tags, no ceremony.

jsonic is a tabnas plugin

jsonic is the relaxed-JSON grammar plugin for the tabnas engine (github.com/tabnas/parser/go). The engine ships no grammar; the standard-JSON core comes from the @tabnas/json plugin (github.com/tabnas/json/go), and jsonic layers its relaxed extensions on top of that core. Install it on an engine instance the idiomatic way:

import (
    tabnas "github.com/tabnas/parser/go"
    tabnasjsonic "github.com/tabnas/jsonic/go"
)

j := tabnas.Make()
j.Use(tabnasjsonic.Grammar)
out, _ := j.Parse("a:1, b:[x,y,z]")   // map[a:1 b:[x y z]]

Because it is a normal plugin, other grammar plugins can depend on it and layer their own syntax on top of jsonic's value/map/list rules — register jsonic first:

j.Use(tabnasjsonic.Grammar)   // dependency: provides the cell-value grammar
j.Use(csv)              // builds on what jsonic registered

The tabnasjsonic.Make / tabnasjsonic.Parse helpers shown above are a legacy compatibility layer that installs this same plugin. Reach for them when porting existing code; reach for Use(tabnasjsonic.Grammar) when composing grammars.

Building from source. Until tabnas/parser and tabnas/json publish tagged Go modules, this module depends on sibling checkouts via replace directives in go.mod (the same development model the TypeScript package uses). Clone https://github.com/tabnas/parser.git and https://github.com/tabnas/json.git next to this repo so they resolve at ../../parser/go and ../../json/go.

Configured Instance

You don't have to accept the defaults. Make gives you a configured parser instance with whatever behavior you need:

func boolp(b bool) *bool { return &b }

j := tabnasjsonic.Make(tabnasjsonic.Options{
    Number: &tabnasjsonic.NumberOptions{Lex: boolp(false)},
})

result, err := j.Parse("a:1, b:2")
// {"a": "1", "b": "2"} — numbers are kept as strings

Options compose. Turn things off, turn things on. You can always change it later.

Syntax

jsonic accepts all standard JSON plus the relaxations listed in the syntax reference. Here are the highlights:

  • Unquoted keys: a:1{"a": 1}
  • Implicit objects: a:1,b:2{"a": 1, "b": 2}
  • Implicit arrays: a,b,c["a", "b", "c"]
  • Comments: #, //, /* */
  • Single/backtick quotes: 'hello', `hello`
  • Path diving: a:b:1{"a": {"b": 1}}
  • Trailing commas: {a:1,}{"a": 1}
  • All number formats: hex, octal, binary, separators

Documentation

The docs are organized by what you are trying to do:

License

MIT. Copyright (c) Richard Rodger.

Documentation

Overview

Package tabnasjsonic is the relaxed-JSON grammar plugin for the tabnas parsing engine (github.com/tabnas/parser/go). The engine ships no grammar; this package supplies the lenient-JSON one and a legacy Jsonic-style API on top of it.

engine.go re-exports the engine's public surface under the historic jsonic names so existing callers (and this package's own grammar and tests) keep compiling unchanged. The single source of truth for the parser, lexer, rule machinery, options and utilities is the tabnas engine — nothing here re-implements it.

Index

Constants

View Source
const (
	TinBD  = tabnas.TinBD
	TinZZ  = tabnas.TinZZ
	TinUK  = tabnas.TinUK
	TinAA  = tabnas.TinAA
	TinSP  = tabnas.TinSP
	TinLN  = tabnas.TinLN
	TinCM  = tabnas.TinCM
	TinNR  = tabnas.TinNR
	TinST  = tabnas.TinST
	TinTX  = tabnas.TinTX
	TinVL  = tabnas.TinVL
	TinOB  = tabnas.TinOB
	TinCB  = tabnas.TinCB
	TinOS  = tabnas.TinOS
	TinCS  = tabnas.TinCS
	TinCL  = tabnas.TinCL
	TinCA  = tabnas.TinCA
	TinMAX = tabnas.TinMAX

	// Rule states.
	OPEN  = tabnas.OPEN
	CLOSE = tabnas.CLOSE
)

Token identification numbers (Tin) for the standard tokens.

View Source
const Version = "0.2.0"

Version is the current version of the jsonic Go module.

Variables

View Source
var (
	// Undefined is the sentinel for "no value" (distinct from nil).
	Undefined = tabnas.Undefined

	// NoToken / NoRule are the zero sentinels used in rule actions.
	NoToken = tabnas.NoToken
	NoRule  = tabnas.NoRule

	// Skip is the deep-merge skip sentinel.
	Skip = tabnas.Skip

	// FixedTokens is the global fixed-token table (src → Tin).
	FixedTokens = tabnas.FixedTokens

	// TinSetVAL / TinSetKEY are the default value and key token sets.
	TinSetVAL = tabnas.TinSetVAL
	TinSetKEY = tabnas.TinSetKEY

	// BuiltinRefs is the engine's standard `$`-builtin library
	// (@object$/@array$/@reset$/@key$/@setval$/@push$/@value$, plus the
	// tree/probe builtins). jsonic merges these into its local funcref map
	// so its code-built alts can reference them by name (the engine merges
	// them automatically only for a declarative GrammarSpec, not for alts
	// resolved directly via ResolveGrammarAltStatic).
	BuiltinRefs = tabnas.BUILTIN_REFS
)
View Source
var (
	Deep                    = tabnas.Deep
	IsUndefined             = tabnas.IsUndefined
	UnwrapUndefined         = tabnas.UnwrapUndefined
	MakeRule                = tabnas.MakeRule
	MakeRuleCond            = tabnas.MakeRuleCond
	MakeToken               = tabnas.MakeToken
	MapToOptions            = tabnas.MapToOptions
	NewLex                  = tabnas.NewLex
	NewParser               = tabnas.NewParser
	ParseAlts               = tabnas.ParseAlts
	ResolveFuncRefs         = tabnas.ResolveFuncRefs
	ResolveGrammarAltStatic = tabnas.ResolveGrammarAltStatic
	Str                     = tabnas.Str
	Snip                    = tabnas.Snip
	StrInject               = tabnas.StrInject
	Keys                    = tabnas.Keys
	Values                  = tabnas.Values
	Entries                 = tabnas.Entries
	Omap                    = tabnas.Omap
	ValidateGroupTags       = tabnas.ValidateGroupTags
	Scan                    = tabnas.Scan
	BuildCharRunSpec        = tabnas.BuildCharRunSpec
	BuildLineRunSpec        = tabnas.BuildLineRunSpec
	BuildStringBodySpec     = tabnas.BuildStringBodySpec
	NormAlt                 = tabnas.NormAlt
	NormAlts                = tabnas.NormAlts
	ModList                 = tabnas.ModList
	LookupRef               = tabnas.LookupRef
	RequireRef              = tabnas.RequireRef
	IsFuncRef               = tabnas.IsFuncRef
	IsSkip                  = tabnas.IsSkip
	RegisterTextParser      = tabnas.RegisterTextParser
	DefaultLexConfig        = tabnas.DefaultLexConfig
	CEq                     = tabnas.CEq
	CGt                     = tabnas.CGt
	CGte                    = tabnas.CGte
	CLt                     = tabnas.CLt
	CLte                    = tabnas.CLte
	CNe                     = tabnas.CNe
)

Functions

func Grammar

func Grammar(j *Jsonic, opts map[string]any) (err error)

Grammar is the idiomatic tabnas grammar plugin: it applies jsonic's option defaults and registers the relaxed-JSON grammar (the val / map / list / pair / elem rules) on the engine instance. Use it the standard way, and `use` it before any plugin that builds on jsonic's rules:

j := tabnas.Make()
j.Use(jsonic.Grammar)
j.Use(csv) // builds on jsonic's value/map/list rules
out, _ := j.Parse("a:1,b:[x,y,z]")

The Jsonic-style helpers (Make, Parse, MakeJSON) are a legacy compatibility layer that installs this same plugin.

func Parse

func Parse(src string) (any, error)

Parse parses a jsonic source string with default settings and returns the resulting value, or a *JsonicError on failure.

Types

type AltAction

type AltAction = tabnas.AltAction

type AltCond

type AltCond = tabnas.AltCond

type AltError

type AltError = tabnas.AltError

type AltModListOpts

type AltModListOpts = tabnas.AltModListOpts

type AltModifier

type AltModifier = tabnas.AltModifier

type AltSpec

type AltSpec = tabnas.AltSpec

type ColorConfig

type ColorConfig = tabnas.ColorConfig

type ColorOptions

type ColorOptions = tabnas.ColorOptions

type CommentDef

type CommentDef = tabnas.CommentDef

type CommentOptions

type CommentOptions = tabnas.CommentOptions

type ConfigModifier

type ConfigModifier = tabnas.ConfigModifier

type Context

type Context = tabnas.Context

type Entry

type Entry = tabnas.Entry

type ErrMsgOptions

type ErrMsgOptions = tabnas.ErrMsgOptions

type FixedOptions

type FixedOptions = tabnas.FixedOptions

type FuncRef

type FuncRef = tabnas.FuncRef

type GrammarAltListSpec

type GrammarAltListSpec = tabnas.GrammarAltListSpec

type GrammarAltSpec

type GrammarAltSpec = tabnas.GrammarAltSpec

type GrammarInjectSpec

type GrammarInjectSpec = tabnas.GrammarInjectSpec

type GrammarRuleSpec

type GrammarRuleSpec = tabnas.GrammarRuleSpec

type GrammarSetting

type GrammarSetting = tabnas.GrammarSetting

type GrammarSettingAlt

type GrammarSettingAlt = tabnas.GrammarSettingAlt

type GrammarSettingRule

type GrammarSettingRule = tabnas.GrammarSettingRule

type GrammarSpec

type GrammarSpec = tabnas.GrammarSpec

type InfoOptions

type InfoOptions = tabnas.InfoOptions

type Jsonic

type Jsonic = tabnas.Tabnas

Jsonic is a configured parser instance. It is the tabnas engine type; the relaxed-JSON grammar is installed onto it by Make / the Grammar plugin. Kept under the historic name for backward compatibility.

func Empty

func Empty(opts ...Options) *Jsonic

Empty creates a parser instance with the jsonic configuration but no grammar rules, for building a grammar from scratch. Matches the historic jsonic.empty().

func Make

func Make(opts ...Options) *Jsonic

Make creates a relaxed-JSON parser instance: a tabnas engine with the jsonic grammar plugin installed, plus any caller options on top. Equivalent to the historic jsonic.make().

func MakeJSON

func MakeJSON() *Jsonic

MakeJSON creates an instance configured to accept strict JSON only. Mirrors the TypeScript Jsonic.make('json'): it installs the full jsonic grammar, then restricts it to the json-tagged alternates and disables the lenient lexer features. Rejects unquoted keys/values, comments, hex/octal/binary numbers, trailing commas, leading-zero numbers, single-quoted or backtick strings, and empty input.

type JsonicError

type JsonicError = tabnas.TabnasError

JsonicError is the structured error returned by Parse on failure. It is the engine's error type; the [jsonic/<code>] tag comes from the errmsg.name option that the grammar plugin sets to "jsonic".

type Lex

type Lex = tabnas.Lex

type LexCheck

type LexCheck = tabnas.LexCheck

type LexCheckResult

type LexCheckResult = tabnas.LexCheckResult

type LexConfig

type LexConfig = tabnas.LexConfig

type LexMatcher

type LexMatcher = tabnas.LexMatcher

type LexOptions

type LexOptions = tabnas.LexOptions

type LexSub

type LexSub = tabnas.LexSub

type LineOptions

type LineOptions = tabnas.LineOptions

type ListOptions

type ListOptions = tabnas.ListOptions

type ListRef

type ListRef = tabnas.ListRef

type MakeLexMatcher

type MakeLexMatcher = tabnas.MakeLexMatcher

type MapMergeFunc

type MapMergeFunc = tabnas.MapMergeFunc

type MapOptions

type MapOptions = tabnas.MapOptions

type MapRef

type MapRef = tabnas.MapRef

type MatchOptions

type MatchOptions = tabnas.MatchOptions

type MatchSpec

type MatchSpec = tabnas.MatchSpec

type MatchTokenEntry

type MatchTokenEntry = tabnas.MatchTokenEntry

type MatchValueEntry

type MatchValueEntry = tabnas.MatchValueEntry

type MatchValueSpec

type MatchValueSpec = tabnas.MatchValueSpec

type MatcherEntry

type MatcherEntry = tabnas.MatcherEntry

type ModListOpts

type ModListOpts = tabnas.ModListOpts

type NumberOptions

type NumberOptions = tabnas.NumberOptions

type Options

type Options = tabnas.Options

type ParseOptions

type ParseOptions = tabnas.ParseOptions

type Parser

type Parser = tabnas.Parser

type ParserOptions

type ParserOptions = tabnas.ParserOptions

type Plugin

type Plugin = tabnas.Plugin

Plugin is a function that configures a parser instance. It is the engine's plugin type; jsonic's own grammar (see Grammar) is one.

type Point

type Point = tabnas.Point

type PropertyOptions

type PropertyOptions = tabnas.PropertyOptions

type ResultOptions

type ResultOptions = tabnas.ResultOptions

type Rule

type Rule = tabnas.Rule

type RuleDefiner

type RuleDefiner = tabnas.RuleDefiner

type RuleOptions

type RuleOptions = tabnas.RuleOptions

type RuleSpec

type RuleSpec = tabnas.RuleSpec

type RuleState

type RuleState = tabnas.RuleState

type RuleSub

type RuleSub = tabnas.RuleSub

type SafeOptions

type SafeOptions = tabnas.SafeOptions

type ScanOut

type ScanOut = tabnas.ScanOut

type ScanSpec

type ScanSpec = tabnas.ScanSpec

type SpaceOptions

type SpaceOptions = tabnas.SpaceOptions

type StateAction

type StateAction = tabnas.StateAction

type StringOptions

type StringOptions = tabnas.StringOptions

type Text

type Text = tabnas.Text

type TextOptions

type TextOptions = tabnas.TextOptions

type Tin

type Tin = tabnas.Tin

type Token

type Token = tabnas.Token

type TokenValFunc

type TokenValFunc = tabnas.TokenValFunc

type UtilBag

type UtilBag = tabnas.UtilBag

type ValModifier

type ValModifier = tabnas.ValModifier

type ValueDef

type ValueDef = tabnas.ValueDef

type ValueDefEntry

type ValueDefEntry = tabnas.ValueDefEntry

type ValueOptions

type ValueOptions = tabnas.ValueOptions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL