From 30686c4cc89e6952ec00846e34016f37d53f31dd Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 8 Sep 2025 17:33:47 -0700 Subject: 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 Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI --- src/encoding/json/v2/example_orderedobject_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/encoding/json/v2/example_orderedobject_test.go') diff --git a/src/encoding/json/v2/example_orderedobject_test.go b/src/encoding/json/v2/example_orderedobject_test.go index d68782f725..fc23132504 100644 --- a/src/encoding/json/v2/example_orderedobject_test.go +++ b/src/encoding/json/v2/example_orderedobject_test.go @@ -53,7 +53,9 @@ func (obj *OrderedObject[V]) MarshalJSONTo(enc *jsontext.Encoder) error { // UnmarshalJSONFrom decodes a JSON object from dec into obj. func (obj *OrderedObject[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error { 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 { return err -- cgit v1.3