From 793e1705b4c34405f24d0f95ab532b68e8db673a Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 12 Oct 2026 23:41:10 +0200 Subject: replace all json.Marshaler{ => To} and json.Unmarshaler{ => From} --- jsonmatch/diff_test.go | 11 ++++++----- jsonmatch/interface.go | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'jsonmatch') diff --git a/jsonmatch/diff_test.go b/jsonmatch/diff_test.go index d81c54b..1d89f94 100644 --- a/jsonmatch/diff_test.go +++ b/jsonmatch/diff_test.go @@ -4,7 +4,8 @@ package jsonmatch_test import ( - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "errors" "fmt" "strings" @@ -24,8 +25,8 @@ var ( _ jsonmatch.Diffable = jsonmatch.Scalar("foo") _ jsonmatch.Diffable = jsonmatch.Scalar(42) _ jsonmatch.Diffable = jsonmatch.Scalar(false) - _ = jsonmatch.CaptureField(Some(1).AsPointer()).(json.Marshaler) - _ = jsonmatch.CaptureField(Some(1).AsPointer()).(json.Unmarshaler) + _ = jsonmatch.CaptureField(Some(1).AsPointer()).(json.MarshalerTo) + _ = jsonmatch.CaptureField(Some(1).AsPointer()).(json.UnmarshalerFrom) ) func TestCanonicalizesActualPayload(t *testing.T) { @@ -389,8 +390,8 @@ func TestFailsOnUnmarshalError(t *testing.T) { type unmarshalableObject struct{} -func (unmarshalableObject) MarshalJSON() ([]byte, error) { - return nil, errors.New("this object is unmarshalable") +func (unmarshalableObject) MarshalJSONTo(enc *jsontext.Encoder) error { + return errors.New("this object is unmarshalable") } func TestArrayOnArrayAction(t *testing.T) { diff --git a/jsonmatch/interface.go b/jsonmatch/interface.go index f54da50..fd8ccfe 100644 --- a/jsonmatch/interface.go +++ b/jsonmatch/interface.go @@ -131,8 +131,8 @@ package jsonmatch // import "go.xyrillian.de/gg/jsonmatch" import ( - "encoding/json" "encoding/json/jsontext" + "encoding/json/v2" "errors" "fmt" ) @@ -287,19 +287,19 @@ type capturedField struct { PointerToTarget any } -// MarshalJSON implements the [json.Marshaler] interface by transparently marshaling the contained value. +// MarshalJSONTo implements the [json.MarshalerTo] interface by transparently marshaling the contained value. // // This implementation ensures that `capturedField` looks like its payload // when serialized for a "type mismatch" or "value mismatch" error message. -func (f capturedField) MarshalJSON() ([]byte, error) { - return json.Marshal(f.PointerToTarget) +func (f capturedField) MarshalJSONTo(enc *jsontext.Encoder) error { + return json.MarshalEncode(enc, f.PointerToTarget) } -// UnmarshalJSON implements the [json.Unmarshaler] interface by always throwing an error. +// UnmarshalJSONFrom implements the [json.UnmarshalerFrom] interface by always throwing an error. // // This implementation ensures that `capturedField` is not placed into a // container that DiffAgainst() does not know how to recurse into. -func (f capturedField) UnmarshalJSON(buf []byte) error { +func (f capturedField) UnmarshalJSONFrom(dec *jsontext.Decoder) error { return errors.New("cannot unmarshal into jsonmatch.CaptureField()") } @@ -314,17 +314,17 @@ func Irrelevant() any { return irrelevant{} } -// MarshalJSON implements the [json.Marshaler] interface. +// MarshalJSONTo implements the [json.MarshalerTo] interface. // // This implementation ensures that `irrelevant` renders in a readable way // when a larger value containing it is serialized for a "type mismatch" or "value mismatch" error message. -func (irrelevant) MarshalJSON() ([]byte, error) { - return []byte(`""`), nil +func (irrelevant) MarshalJSONTo(enc *jsontext.Encoder) error { + return enc.WriteToken(jsontext.String("")) } var ( // static assertion that the documented interfaces are indeed implemented - _ json.Marshaler = capturedField{} - _ json.Unmarshaler = capturedField{} - _ json.Marshaler = irrelevant{} + _ json.MarshalerTo = capturedField{} + _ json.UnmarshalerFrom = capturedField{} + _ json.MarshalerTo = irrelevant{} ) -- cgit v1.3.1