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 ¶
- func Marshal(in any, opts ...Options) ([]byte, error)
- func MarshalEncode(out *Encoder, in any, opts ...Options) error
- func Unmarshal(in []byte, out any, opts ...Options) error
- func UnmarshalDecode(in *Decoder, out any, opts ...Options) error
- type Decoder
- type Encoder
- type Options
- type SyntacticError
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalEncode ¶
MarshalEncode encodes in as JSON to the encoder with the options.
Types ¶
type Options ¶
Options is a set of options for the JSON encoder and decoder.
func AllowInvalidUTF8 ¶
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 ¶
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.