Documentation
¶
Overview ¶
Package jfather is a JSON parser and decoder that provides metadata about the parsed JSON.
Index ¶
Examples ¶
Constants ¶
This section is empty.
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, non-standard parsing behaviour. All options are 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]
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.