Package jsonopts

import "encoding/json/internal/jsonopts"
Overview
Index

Overview ▾

Variables

DefaultOptionsV1 is the set of all options that define default v1 behavior.

var DefaultOptionsV1 = Struct{
    Flags: jsonflags.Flags{
        Presence: uint64(jsonflags.DefaultV1Flags),
        Values:   uint64(jsonflags.DefaultV1Flags),
    },
}

DefaultOptionsV2 is the set of all options that define default v2 behavior.

var DefaultOptionsV2 = Struct{
    Flags: jsonflags.Flags{
        Presence: uint64(jsonflags.DefaultV1Flags),
        Values:   uint64(0),
    },
}

GetUnknownOption is injected by the "json" package to handle Options declared in that package so that "jsonopts" can handle them.

var GetUnknownOption = func(Struct, Options) (any, bool) { panic("unknown option") }

JoinUnknownOption is injected by the "json" package to handle Options declared in that package so that "jsonopts" can handle them.

var JoinUnknownOption = func(Struct, Options) Struct { panic("unknown option") }

func ChangedWhitespace

func ChangedWhitespace(s1, s2 Struct) bool

ChangedWhitespace reports whether whitespace values have changed.

func GetOption

func GetOption[T any](opts Options, setter func(T) Options) (T, bool)

type ArshalValues

type ArshalValues struct {
    Marshalers   any // jsonflags.Marshalers
    Unmarshalers any // jsonflags.Unmarshalers

    Format string // valid if jsonflags.FormatTag is set
}

type ByteLimit

type ByteLimit int64 // jsontext.WithByteLimit

func (ByteLimit) JSONOptions

func (ByteLimit) JSONOptions(internal.NotForPublicUse)

type CoderValues

type CoderValues struct {
    Indent       string // jsonflags.Indent
    IndentPrefix string // jsonflags.IndentPrefix
    ByteLimit    int64  // jsonflags.ByteLimit
    DepthLimit   int    // jsonflags.DepthLimit
}

type DepthLimit

type DepthLimit int // jsontext.WithDepthLimit

func (DepthLimit) JSONOptions

func (DepthLimit) JSONOptions(internal.NotForPublicUse)

type Indent

type Indent string // jsontext.WithIndent

func (Indent) JSONOptions

func (Indent) JSONOptions(internal.NotForPublicUse)

type IndentPrefix

type IndentPrefix string // jsontext.WithIndentPrefix

func (IndentPrefix) JSONOptions

func (IndentPrefix) JSONOptions(internal.NotForPublicUse)

type Options

Options is the common options type shared across json packages.

type Options interface {
    // JSONOptions is exported so related json packages can implement Options.
    JSONOptions(internal.NotForPublicUse)
}

func ExperimentalSupportFormatTag

func ExperimentalSupportFormatTag(v bool) Options

ExperimentalSupportFormatTag enables support for the `format` tag.

WARNING: This is an experimental feature and will be removed in the future as either a failed experiment or be formally included in "encoding/json/v2" in some semantically similar form, in which case, users of this option must migrate to the officially supported feature.

The `format` tag was originally part of the "encoding/json/v2" experiment but support for it was removed (see https://go.dev/issue/79071) for the initial release of "encoding/json/v2" in light of the anticipation that the Go language might support typed struct tags (https://go.dev/issue/74472).

Typed struct tags are a more expressive and type-safe way to express format attributes than the bespoke `format` tag option that implements a miniature domain-specific language (DSL) within the "json" package itself.

While "encoding/json/v2" was in the experimental phase, some users were already depending on the `format` tag option. This experimental option exists to provide a temporary workaround until the (hopeful) inclusion of typed struct tags and support for formatting directives in "encoding/json/v2" using that language mechanism.

This option enables support for the `format` tag option, which specifies a format flag used to specialize the formatting of the field value. The option is a key-value pair specified as "format:value" where the value must be either a literal consisting of letters and numbers (e.g., `format:RFC3339`) or a single-quoted string literal (e.g., `format:'2006-01-02'`). The interpretation of the format flag is determined by the struct field type.

Go types with alternative representations are as follows:

type Struct

Struct is the combination of all options in struct form. This is efficient to pass down the call stack and to query.

type Struct struct {
    Flags jsonflags.Flags

    CoderValues
    ArshalValues
}

func (*Struct) InitializeMultiline

func (s *Struct) InitializeMultiline()

InitializeMultiline sets default options implied by Multiline.

func (*Struct) JSONOptions

func (*Struct) JSONOptions(internal.NotForPublicUse)

func (*Struct) Join

func (dst *Struct) Join(srcs ...Options)