jfather

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 7 Imported by: 61

README

jfather

Parse JSON with line numbers and more!

This is a JSON parsing module that provides additional information during the unmarshalling process, such as line numbers, columns etc.

You can use jfather to unmarshal JSON just like the encoding/json package, and add your own unmarshalling functionality to gather metadata by implementing the jfather.Unmarshaler interface. This requires a single method with the signature UnmarshalJSONWithMetadata(node jfather.Node) error. A full example is below.

You should not use this package unless you need the line/column metadata, as unmarshalling is typically much slower than the encoding/json package:

BenchmarkUnmarshal_JFather-11    	  120945	      9401 ns/op	   14016 B/op	     176 allocs/op
BenchmarkUnmarshal_Traditional-11     326814	      3699 ns/op	    2552 B/op	      56 allocs/op

Full Example

package main

import (
	"fmt"

	"github.com/liamg/jfather"
)

type ExampleParent struct {
	Child ExampleChild `json:"child"`
}

type ExampleChild struct {
	Name   string
	Line   int
	Column int
}

func (t *ExampleChild) UnmarshalJSONWithMetadata(node jfather.Node) error {
	t.Line = node.Range().Start.Line
	t.Column = node.Range().Start.Column
	return node.Decode(&t.Name)
}

func main() {
	input := []byte(`{
	"child": "secret"
}`)
	var parent ExampleParent
	if err := jfather.Unmarshal(input, &parent); err != nil {
		panic(err)
	}

	fmt.Printf("Child value is at line %d, column %d, and is set to '%s'\n",
		parent.Child.Line, parent.Child.Column, parent.Child.Name)

	// outputs:
	//  Child value is at line 2, column 11, and is set to 'secret'
}

Documentation

Overview

Package jfather is a JSON parser and decoder that provides metadata about the parsed JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

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

Unmarshal unmarshals the given JSON data into the target value. It will attempt to use UnmarshalJSONWithMetadata if the target implements it, otherwise it will use the default json unmarshaler.

Types

type Kind

type Kind uint8

Kind represents the type of a node.

const (
	// KindUnknown represents an unknown kind.
	KindUnknown Kind = iota
	// KindNull represents a null value.
	KindNull
	// KindNumber represents a number value.
	KindNumber
	// KindString represents a string value.
	KindString
	// KindBoolean represents a boolean value.
	KindBoolean
	// KindArray represents an array value.
	KindArray
	// KindObject represents an object value.
	KindObject
)

type Node

type Node interface {
	Range() Range
	Decode(target any) error
	Kind() Kind
	Content() []Node
}

Node represents a node in the AST.

type PeekReader

type PeekReader struct {
	// contains filtered or unexported fields
}

PeekReader is a reader that allows peeking at the next rune.

func NewPeekReader

func NewPeekReader(reader io.Reader) *PeekReader

NewPeekReader returns a new PeekReader.

func (*PeekReader) Next

func (r *PeekReader) Next() (rune, error)

Next returns the next rune.

func (*PeekReader) Peek

func (r *PeekReader) Peek() (rune, error)

Peek returns the next rune without advancing the reader.

func (*PeekReader) Undo

func (r *PeekReader) Undo() error

Undo undoes the last Next call.

type Position

type Position struct {
	Line   int
	Column int
}

Position represents a position in the source code. Note that both lines and columns are 1-indexed.

type Range

type Range struct {
	Start Position
	End   Position
}

Range represents a range of positions in the source.

type Unmarshaler added in v0.0.8

type Unmarshaler interface {
	UnmarshalJSONWithMetadata(node Node) error
}

Unmarshaler is an interface that can be implemented by types that can unmarshal a JSON description of themselves.

Directories

Path Synopsis
_examples
basic command

Jump to

Keyboard shortcuts

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