json

package
v0.69.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: Apache-2.0 Imports: 8 Imported by: 13

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SetLocationHook = DecodeHook{
	After: func(_ *jsontext.Decoder, target any, location types.Location) {
		if loc, ok := target.(ObjectLocation); ok {
			loc.SetLocation(location)
		}
	},
}

Functions

func NewLineReader added in v0.66.0

func NewLineReader(r io.Reader) *lineReader

NewLineReader creates a new line reader.

func ToRFC8259 added in v0.63.0

func ToRFC8259(src []byte) []byte

ToRFC8259 converts JSONC (JSON with Comments) to valid JSON following RFC8259. It strips out comments and trailing commas while maintaining the exact character offsets as the input. This ensures that any JSON parser locations will map directly back to the original source file positions.

Both line numbers and character positions are preserved in the output. Comments and trailing commas are replaced with spaces without changing line counts.

Comments can be either: - Single-line: starting with // and continuing to the end of the line - Multi-line: starting with /* and ending with */

Trailing commas are allowed in JSONC but not in standard JSON, so they are replaced with spaces to maintain character offsets.

func Unmarshal

func Unmarshal(data []byte, v any) error

func UnmarshalJSONC added in v0.63.0

func UnmarshalJSONC(data []byte, v any) error

UnmarshalJSONC parses JSONC (JSON with Comments) data into the specified value. It first converts JSONC to standard JSON following RFC8259 and then unmarshals it. This is a convenience function that combines ToRFC8259 and Unmarshal.

The parser preserves line number information, which is essential for reporting errors at their correct locations in the original file.

Usage example:

type Config struct {
    Name    string            `json:"name"`
    Version string            `json:"version"`
    xjson.Location            // Embed Location to get line number info
}

var config Config
if err := xjson.UnmarshalJSONC(data, &config); err != nil {
    return err
}

func UnmarshalRead

func UnmarshalRead(r io.Reader, v any) error

func UnmarshalerWithLocation added in v0.66.0

func UnmarshalerWithLocation[T any](r *lineReader, hooks ...DecodeHook) *json.Unmarshalers

UnmarshalerWithLocation returns a json.Unmarshalers that captures the source code location (start and end lines) of each decoded object and optionally invokes hooks after decoding.

By default, the SetLocationHook is used to record source code locations, unless other hooks are explicitly provided.

To use UnmarshalerWithLocation for primitive types, you must implement the json.UnmarshalerFrom interface for those objects. cf. https://pkg.go.dev/github.com/go-json-experiment/json#UnmarshalerFrom

Types

type DecodeHook added in v0.66.0

type DecodeHook struct {
	After func(dec *jsontext.Decoder, target any, loc types.Location)
}

type Location

type Location types.Location

Location is wrap of types.Location. This struct is required when you need to detect location of your object from json file.

func (*Location) SetLocation

func (l *Location) SetLocation(location types.Location)

type ObjectLocation

type ObjectLocation interface {
	SetLocation(location types.Location)
}

ObjectLocation is required when you need to save Location for your struct.

type TokenType added in v0.63.0

type TokenType int

TokenType represents the type of token being processed

const (
	TokenNormal TokenType = iota
	TokenString
	TokenSingleLineComment
	TokenMultiLineComment
)

Jump to

Keyboard shortcuts

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