mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json/v2: document context annotation with SemanticError
When the json package calls Marshaler, MarshalerTo, Unmarshaler, or UnmarshalerFrom methods and a SemanticError is returned, it will automatically annotate the error with context. Document this behavior. Change-Id: Id8e775a7c1c2a6ffc29ea518913591915e8aff87 Reviewed-on: https://go-review.googlesource.com/c/go/+/701956 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
c5737dc21b
commit
30686c4cc8
3 changed files with 26 additions and 1 deletions
|
|
@ -41,6 +41,10 @@ var (
|
||||||
//
|
//
|
||||||
// It is recommended that implementations return a buffer that is safe
|
// It is recommended that implementations return a buffer that is safe
|
||||||
// for the caller to retain and potentially mutate.
|
// for the caller to retain and potentially mutate.
|
||||||
|
//
|
||||||
|
// If the returned error is a [SemanticError], then unpopulated fields
|
||||||
|
// of the error may be populated by [json] with additional context.
|
||||||
|
// Errors of other types are wrapped within a [SemanticError].
|
||||||
type Marshaler interface {
|
type Marshaler interface {
|
||||||
MarshalJSON() ([]byte, error)
|
MarshalJSON() ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
@ -54,6 +58,11 @@ type Marshaler interface {
|
||||||
//
|
//
|
||||||
// The implementation must write only one JSON value to the Encoder and
|
// The implementation must write only one JSON value to the Encoder and
|
||||||
// must not retain the pointer to [jsontext.Encoder].
|
// must not retain the pointer to [jsontext.Encoder].
|
||||||
|
//
|
||||||
|
// If the returned error is a [SemanticError], then unpopulated fields
|
||||||
|
// of the error may be populated by [json] with additional context.
|
||||||
|
// Errors of other types are wrapped within a [SemanticError],
|
||||||
|
// unless it is an IO error.
|
||||||
type MarshalerTo interface {
|
type MarshalerTo interface {
|
||||||
MarshalJSONTo(*jsontext.Encoder) error
|
MarshalJSONTo(*jsontext.Encoder) error
|
||||||
|
|
||||||
|
|
@ -72,6 +81,10 @@ type MarshalerTo interface {
|
||||||
// unmarshaling into a pre-populated value.
|
// unmarshaling into a pre-populated value.
|
||||||
//
|
//
|
||||||
// Implementations must not retain or mutate the input []byte.
|
// Implementations must not retain or mutate the input []byte.
|
||||||
|
//
|
||||||
|
// If the returned error is a [SemanticError], then unpopulated fields
|
||||||
|
// of the error may be populated by [json] with additional context.
|
||||||
|
// Errors of other types are wrapped within a [SemanticError].
|
||||||
type Unmarshaler interface {
|
type Unmarshaler interface {
|
||||||
UnmarshalJSON([]byte) error
|
UnmarshalJSON([]byte) error
|
||||||
}
|
}
|
||||||
|
|
@ -88,6 +101,11 @@ type Unmarshaler interface {
|
||||||
// unmarshaling into a pre-populated value.
|
// unmarshaling into a pre-populated value.
|
||||||
//
|
//
|
||||||
// Implementations must not retain the pointer to [jsontext.Decoder].
|
// Implementations must not retain the pointer to [jsontext.Decoder].
|
||||||
|
//
|
||||||
|
// If the returned error is a [SemanticError], then unpopulated fields
|
||||||
|
// of the error may be populated by [json] with additional context.
|
||||||
|
// Errors of other types are wrapped within a [SemanticError],
|
||||||
|
// unless it is a [jsontext.SyntacticError] or an IO error.
|
||||||
type UnmarshalerFrom interface {
|
type UnmarshalerFrom interface {
|
||||||
UnmarshalJSONFrom(*jsontext.Decoder) error
|
UnmarshalJSONFrom(*jsontext.Decoder) error
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,11 @@ func isFatalError(err error, flags jsonflags.Flags) bool {
|
||||||
// SemanticError describes an error determining the meaning
|
// SemanticError describes an error determining the meaning
|
||||||
// of JSON data as Go data or vice-versa.
|
// of JSON data as Go data or vice-versa.
|
||||||
//
|
//
|
||||||
|
// If a [Marshaler], [MarshalerTo], [Unmarshaler], or [UnmarshalerFrom] method
|
||||||
|
// returns a SemanticError when called by the [json] package,
|
||||||
|
// then the ByteOffset, JSONPointer, and GoType fields are automatically
|
||||||
|
// populated by the calling context if they are the zero value.
|
||||||
|
//
|
||||||
// The contents of this error as produced by this package may change over time.
|
// The contents of this error as produced by this package may change over time.
|
||||||
type SemanticError struct {
|
type SemanticError struct {
|
||||||
requireKeyedLiterals
|
requireKeyedLiterals
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,9 @@ func (obj *OrderedObject[V]) MarshalJSONTo(enc *jsontext.Encoder) error {
|
||||||
// UnmarshalJSONFrom decodes a JSON object from dec into obj.
|
// UnmarshalJSONFrom decodes a JSON object from dec into obj.
|
||||||
func (obj *OrderedObject[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
|
func (obj *OrderedObject[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
|
||||||
if k := dec.PeekKind(); k != '{' {
|
if k := dec.PeekKind(); k != '{' {
|
||||||
return fmt.Errorf("expected object start, but encountered %v", k)
|
// The [json] package automatically populates relevant fields
|
||||||
|
// in a [json.SemanticError] to provide additional context.
|
||||||
|
return &json.SemanticError{JSONKind: k}
|
||||||
}
|
}
|
||||||
if _, err := dec.ReadToken(); err != nil {
|
if _, err := dec.ReadToken(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue