Documentation
¶
Overview ¶
Package jfather is a JSON parser and decoder that provides metadata about the parsed JSON.
Index ¶
Examples ¶
Constants ¶
const DefaultMaxDepth = 10000
DefaultMaxDepth is the maximum nesting depth of arrays and objects accepted by Unmarshal unless overridden with the MaxDepth option. The parser (and any code that later walks the resulting tree) recurses once per nesting level, so depth must be bounded to prevent maliciously deep input from overflowing the stack. The value matches encoding/json's nesting limit.
Variables ¶
This section is empty.
Functions ¶
func Unmarshal ¶
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.
By default the input must be strict JSON. Pass options such as AllowComments or AllowTrailingCommas to accept common JSON dialects.
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 Option ¶ added in v0.0.10
type Option func(*parser)
Option configures optional parsing behaviour. The dialect options (AllowComments, AllowTrailingCommas, AllowUnescapedControlChars) are all off by default, so Unmarshal stays strict JSON unless a caller opts in.
func AllowComments ¶ added in v0.0.10
func AllowComments() Option
AllowComments permits JavaScript-style comments — // to end of line and /* ... */ block comments — anywhere whitespace is allowed. Standard JSON forbids comments, but several dialects permit them (JSONC, tsconfig, and ARM/Bicep deployment templates).
Example ¶
package main
import (
"fmt"
"github.com/liamg/jfather"
)
func main() {
input := []byte(`{
// the service to deploy
"name": "example" /* block comment */
}`)
var target struct {
Name string `json:"name"`
}
if err := jfather.Unmarshal(input, &target, jfather.AllowComments()); err != nil {
panic(err)
}
fmt.Println(target.Name)
}
Output: example
func AllowTrailingCommas ¶ added in v0.0.10
func AllowTrailingCommas() Option
AllowTrailingCommas permits a single trailing comma before a closing ']' or '}'. Standard JSON forbids it.
Example ¶
package main
import (
"fmt"
"github.com/liamg/jfather"
)
func main() {
input := []byte(`{ "tags": [ "a", "b", ], }`)
var target struct {
Tags []string `json:"tags"`
}
if err := jfather.Unmarshal(input, &target, jfather.AllowTrailingCommas()); err != nil {
panic(err)
}
fmt.Println(target.Tags)
}
Output: [a b]
func AllowUnescapedControlChars ¶ added in v0.0.11
func AllowUnescapedControlChars() Option
AllowUnescapedControlChars permits literal control characters (U+0000 through U+001F), such as raw newlines and tabs, to appear inside string values. Standard JSON requires these to be escaped, but some producers emit them literally — for example Azure ARM/Bicep templates routinely break a long expression across several lines inside a single string value. This mirrors Python's json.loads(..., strict=False).
Example ¶
package main
import (
"fmt"
"github.com/liamg/jfather"
)
func main() {
// A raw newline inside the string value (an ARM-style expression
// broken across lines) would fail strict JSON.
input := []byte("{ \"expr\": \"[concat('a',\n'b')]\" }")
var target struct {
Expr string `json:"expr"`
}
if err := jfather.Unmarshal(input, &target, jfather.AllowUnescapedControlChars()); err != nil {
panic(err)
}
fmt.Printf("%q\n", target.Expr)
}
Output: "[concat('a',\n'b')]"
func MaxDepth ¶ added in v0.0.12
MaxDepth overrides the maximum nesting depth of arrays and objects, which defaults to DefaultMaxDepth. Input nested deeper than the limit is rejected with an error rather than parsed, protecting the parser — and callers that recursively walk the resulting tree — from stack exhaustion on maliciously deep input. Values less than 1 are ignored, leaving the default in place; the limit cannot be disabled.
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) Peek ¶
func (r *PeekReader) Peek() (rune, error)
Peek returns the next rune without advancing the reader.
type Position ¶
Position represents a position in the source code. Note that both lines and columns are 1-indexed.
type Unmarshaler ¶ added in v0.0.8
Unmarshaler is an interface that can be implemented by types that can unmarshal a JSON description of themselves.