Documentation
¶
Overview ¶
Package json provides encoding/json-compatible entry points.
This package is intended for low-friction migration from the standard library surface. For JSON5/JSONC features and rich diagnostics, use the root jsonkit package directly.
Index ¶
- func Compact(dst *bytes.Buffer, src []byte) error
- func HTMLEscape(dst *bytes.Buffer, src []byte)
- func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
- func Marshal(v any) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func Valid(data []byte) bool
- type Decoder
- type Delim
- type Encoder
- type InvalidUnmarshalError
- type Marshaler
- type MarshalerError
- type Number
- type RawMessage
- type SyntaxError
- type Token
- type UnmarshalTypeError
- type Unmarshaler
- type UnsupportedTypeError
- type UnsupportedValueError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTMLEscape ¶
HTMLEscape mirrors encoding/json.HTMLEscape.
func Marshal ¶
Marshal mirrors encoding/json.Marshal.
Example ¶
package main
import (
"fmt"
jsoncompat "github.com/forgemechanic/jsonkit/compat/json"
)
func main() {
out, err := jsoncompat.Marshal(struct {
ID int `json:"id"`
}{ID: 9})
fmt.Println(err == nil)
fmt.Println(string(out))
}
Output: true {"id":9}
func MarshalIndent ¶
MarshalIndent mirrors encoding/json.MarshalIndent.
func Unmarshal ¶
Unmarshal mirrors encoding/json.Unmarshal.
Example ¶
package main
import (
"fmt"
jsoncompat "github.com/forgemechanic/jsonkit/compat/json"
)
func main() {
var dst struct {
Name string `json:"name"`
}
err := jsoncompat.Unmarshal([]byte(`{"name":"jsonkit"}`), &dst)
fmt.Println(dst.Name)
fmt.Println(err == nil)
}
Output: jsonkit true
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder mirrors encoding/json.Decoder behavior.
Example ¶
package main
import (
"fmt"
"strings"
jsoncompat "github.com/forgemechanic/jsonkit/compat/json"
)
func main() {
dec := jsoncompat.NewDecoder(strings.NewReader(`{"id":7}`))
var dst map[string]any
err := dec.Decode(&dst)
fmt.Println(err == nil)
fmt.Println(dst["id"])
}
Output: true 7
func NewDecoder ¶
NewDecoder mirrors encoding/json.NewDecoder.
func (*Decoder) DisallowUnknownFields ¶
func (d *Decoder) DisallowUnknownFields()
DisallowUnknownFields mirrors encoding/json.Decoder.DisallowUnknownFields.
func (*Decoder) InputOffset ¶
InputOffset mirrors encoding/json.Decoder.InputOffset.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder mirrors encoding/json.Encoder behavior.
Example ¶
package main
import (
"bytes"
"fmt"
jsoncompat "github.com/forgemechanic/jsonkit/compat/json"
)
func main() {
var buf bytes.Buffer
enc := jsoncompat.NewEncoder(&buf)
err := enc.Encode(map[string]int{"id": 5})
fmt.Println(err == nil)
fmt.Printf("%q\n", buf.String())
}
Output: true "{\"id\":5}\n"
func NewEncoder ¶
NewEncoder mirrors encoding/json.NewEncoder.
func (*Encoder) SetEscapeHTML ¶
SetEscapeHTML mirrors encoding/json.Encoder.SetEscapeHTML.
type InvalidUnmarshalError ¶
type InvalidUnmarshalError = stdjson.InvalidUnmarshalError
type MarshalerError ¶
type MarshalerError = stdjson.MarshalerError
type RawMessage ¶
type RawMessage = stdjson.RawMessage
type SyntaxError ¶
type SyntaxError = stdjson.SyntaxError
type UnmarshalTypeError ¶
type UnmarshalTypeError = stdjson.UnmarshalTypeError
type Unmarshaler ¶
type Unmarshaler = stdjson.Unmarshaler
type UnsupportedTypeError ¶
type UnsupportedTypeError = stdjson.UnsupportedTypeError
type UnsupportedValueError ¶
type UnsupportedValueError = stdjson.UnsupportedValueError