Source file src/encoding/json/internal/internal.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.jsonv2
     6  
     7  package internal
     8  
     9  import "errors"
    10  
    11  // NotForPublicUse is a marker type that an API is for internal use only.
    12  // It does not perfectly prevent usage of that API, but helps to restrict usage.
    13  // Anything with this marker is not covered by the Go compatibility agreement.
    14  type NotForPublicUse struct{}
    15  
    16  // AllowInternalUse is passed from "json" to "jsontext" to authenticate
    17  // that the caller can have access to internal functionality.
    18  var AllowInternalUse NotForPublicUse
    19  
    20  // Sentinel error values internally shared between jsonv1 and jsonv2.
    21  var (
    22  	ErrCycle           = errors.New("encountered a cycle")
    23  	ErrNonNilReference = errors.New("value must be passed as a non-nil pointer reference")
    24  	ErrNilInterface    = errors.New("cannot derive concrete type for nil interface with finite type set")
    25  )
    26  
    27  // ValueError wraps the error with a Go value relevant to the error.
    28  // This is only used for v1 compatibility purposes.
    29  type ValueError struct {
    30  	Val any
    31  	Err error
    32  }
    33  
    34  func (ve *ValueError) Error() string { return ve.Err.Error() }
    35  func (ve *ValueError) Unwrap() error { return ve.Err }
    36  
    37  var (
    38  	// TransformMarshalError converts a v2 error into a v1 error.
    39  	// It is called only at the top-level of a Marshal function.
    40  	TransformMarshalError func(any, error) error
    41  	// NewMarshalerError constructs a jsonv1.MarshalerError.
    42  	// It is called after a user-defined Marshal method/function fails.
    43  	NewMarshalerError func(any, error, string) error
    44  	// TransformUnmarshalError converts a v2 error into a v1 error.
    45  	// It is called only at the top-level of an Unmarshal function.
    46  	TransformUnmarshalError func(any, error) error
    47  
    48  	// NewRawNumber returns new(jsonv1.Number).
    49  	NewRawNumber func() any
    50  	// RawNumberOf returns jsonv1.Number(b).
    51  	RawNumberOf func(b []byte) any
    52  )
    53  

View as plain text