jsonc

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package jsonc is a minimal JSON-with-Comments (JSONC) parser.

It accepts the same input as encoding/json.Unmarshal, plus:

  • // line comments to end of line
  • /* block comments */
  • trailing commas in objects and arrays

Output is identical to standard JSON parse for the same input without comments. Comment text is discarded; the result is a normal Go interface{} (or other target type) populated as if the comments were never there.

GrayCode native implementation. bin/lib/settings.js (parseJSONC). Ported to native Go.

validate.go: pre-write validation for Claude Code settings fields.

Settings.json hook-field validation. performs a Zod-style pre-check on Claude Code settings.json before writing. This file ports that check to native Go, using the encoding/json conventions of the existing internal/config package.

Validation is permissive by design: unknown fields are allowed (forward compat), but known fields are type-checked and value validated. The intent is to catch typos and accidental schema changes before they corrupt a user's settings.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidJSON = errors.New("jsonc: invalid input")

ErrInvalidJSON is returned when the input cannot be parsed even after comment/trailing-comma stripping.

Functions

func IsValidation

func IsValidation(err error) bool

IsValidation reports whether err is an ErrValidation.

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

MarshalIndent is a thin wrapper over json.MarshalIndent kept here for API symmetry. The output is plain JSON (no comments).

func PrettyError

func PrettyError(err error) string

PrettyError returns a human-friendly error message for a JSONC parse failure. It is intended for display in CLI output.

func Strip

func Strip(src []byte) ([]byte, error)

Strip removes JSONC comments and trailing commas from src and returns a byte slice suitable for encoding/json.Unmarshal.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal is a drop-in replacement for json.Unmarshal that accepts JSONC input (comments + trailing commas).

func Valid

func Valid(data []byte) bool

Valid reports whether data is valid JSONC.

Types

type ErrValidation

type ErrValidation struct {
	Field  string
	Reason string
}

ErrValidation is the base class for all field validation errors.

func (*ErrValidation) Error

func (e *ErrValidation) Error() string

type ValidationResult

type ValidationResult struct {
	Errors []error
}

ValidationResult is the outcome of validating a Claude Code settings document. Errors are non-empty only if validation failed.

func ValidateClaudeSettings

func ValidateClaudeSettings(doc map[string]interface{}) *ValidationResult

ValidateClaudeSettings validates a parsed Claude Code settings document (a map[string]interface{}). Returns a ValidationResult describing any issues. The function never returns a non-nil error; validation problems are reported through the result.

func (*ValidationResult) Add

func (r *ValidationResult) Add(field, reason string)

Add appends a validation error to r.

func (*ValidationResult) AddErr

func (r *ValidationResult) AddErr(err error)

AddErr appends a pre-built error to r.

func (*ValidationResult) Valid

func (r *ValidationResult) Valid() bool

Valid reports whether r contains no validation errors.

Jump to

Keyboard shortcuts

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