jsonv2

package
v0.158.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package jsonv2 is the JSON layer of the module cdproto. It names the types and the funcs of version 2 of the JSON package that the generated code and the programs that use it need, with the same names as the standard library.

The generated code reads and writes JSON with the packages encoding/json/v2 and encoding/json/jsontext. Go 1.27 has both packages in the standard library. Go 1.25 and Go 1.26 have them only when the GOEXPERIMENT environment variable is set to jsonv2. A program that does not set it cannot import them. To support these versions of Go, this package is the only place that imports a JSON package. It imports no other package of the module, so every other package can import it.

The files json_std.go and json_compat.go define the same names. The build chooses one of them:

  • json_std.go builds with Go 1.27 or later, and with GOEXPERIMENT=jsonv2. It uses the packages of the standard library. Its types are aliases of the standard types, so a program on Go 1.27 can pass a Value to any function that takes a jsontext.Value.
  • json_compat.go builds with all other versions of Go. It uses the module github.com/go-json-experiment/json, which is the source of the standard packages. The module is pinned to a version that needs Go 1.25.

The build tag cdproto_jsoncompat selects json_compat.go on every version of Go. Use it to test the second file with a newer toolchain. Go 1.27 turns the jsonv2 experiment on by default, and the pinned module does not build with the standard package of Go 1.27. On Go 1.27, set GOEXPERIMENT=nojsonv2 with the tag.

The two files must behave the same. Both use the default options of version 2 of the JSON package, and neither one changes them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(in any, opts ...Options) ([]byte, error)

Marshal encodes in as JSON with the options.

func MarshalEncode

func MarshalEncode(out *Encoder, in any, opts ...Options) error

MarshalEncode encodes in as JSON to the encoder with the options.

func Unmarshal

func Unmarshal(in []byte, out any, opts ...Options) error

Unmarshal decodes the JSON text in into out with the options.

func UnmarshalDecode

func UnmarshalDecode(in *Decoder, out any, opts ...Options) error

UnmarshalDecode decodes the next JSON value of the decoder into out with the options.

Types

type Decoder

type Decoder = jsontext.Decoder

Decoder reads a stream of JSON values.

type Encoder

type Encoder = jsontext.Encoder

Encoder writes a stream of JSON values.

type Options

type Options = v2.Options

Options is a set of options for the JSON encoder and decoder.

func AllowInvalidUTF8

func AllowInvalidUTF8(v bool) Options

AllowInvalidUTF8 returns the option that lets the encoder and the decoder accept text that is not valid UTF-8.

func DefaultOptionsV2

func DefaultOptionsV2() Options

DefaultOptionsV2 returns the options that define the behavior of version 2 of the JSON package.

func JoinOptions

func JoinOptions(srcs ...Options) Options

JoinOptions combines the options into one set. An option that comes later replaces an option that comes earlier.

type SyntacticError

type SyntacticError = jsontext.SyntacticError

SyntacticError is the error for JSON text that is not valid.

type Value

type Value = jsontext.Value

Value is a raw JSON value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL