tailscale.comtailscale.com/util/jsonutil Index | Files

package jsonutil

import "tailscale.com/util/jsonutil"

Package jsonutil provides utilities to improve JSON performance. It includes an Unmarshal wrapper that amortizes allocated garbage over subsequent runs and a Bytes type to reduce allocations when unmarshalling a non-hex-encoded string into a []byte.

Index

Functions

func Unmarshal

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

Unmarshal is similar to encoding/json.Unmarshal. There are three major differences:

On error, encoding/json.Unmarshal zeros v. This Unmarshal may leave partial data in v. Always check the error before using v! (Future improvements may remove this bug.)

The errors they return don't always match perfectly. If you do error matching more precise than err != nil, don't use this Unmarshal.

This Unmarshal allocates considerably less memory.

Types

type Bytes

type Bytes []byte

Bytes is a byte slice in a json-encoded struct. encoding/json assumes that []byte fields are hex-encoded. Bytes are not hex-encoded; they are treated the same as strings. This can avoid unnecessary allocations due to a round trip through strings.

func (*Bytes) UnmarshalText

func (b *Bytes) UnmarshalText(text []byte) error

Source Files

types.go unmarshal.go

Version
v1.84.0 (latest)
Published
May 21, 2025
Platform
linux/amd64
Imports
3 packages
Last checked
1 day ago

Tools for package owners.