diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-10-12 23:41:10 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-10-12 23:41:10 +0200 |
| commit | 793e1705b4c34405f24d0f95ab532b68e8db673a (patch) | |
| tree | 45e3f64777ad3aa64aa3f1beec8d2b0ee39e737a | |
| parent | 8f6f95650d876376664f1e5a04d9c4cf369468d8 (diff) | |
| download | go-gg-793e1705b4c34405f24d0f95ab532b68e8db673a.tar.gz | |
replace all json.Marshaler{ => To} and json.Unmarshaler{ => From}
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | columnar/columnar.go | 21 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | jsonmatch/diff_test.go | 11 | ||||
| -rw-r--r-- | jsonmatch/interface.go | 24 | ||||
| -rw-r--r-- | option/option.go | 37 |
6 files changed, 57 insertions, 44 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 93c5b34..9e968d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,16 @@ SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> SPDX-License-Identifier: Apache-2.0 --> -# v1.20.0 (TBD), TODO: double-check reason for Go version increase +# v1.20.0 (TBD) Changes: - The minimum Go version was increased from 1.26 to 1.27 to facilitate the use of new APIs surrounding encoding/json/v2. - jsonmatch: Pointer is now a synonym for jsontext.Pointer from the standard library. +- All implementations of encoding/json.Marshaler and encoding/json.Unmarshaler on types in this module + have been replaced with implementations of encoding/json/v2.MarshalerTo and encoding/json/v2.UnmarshalerFrom. + This is technically a backwards-incompatibility for applications that call these methods directly, + but all actual users call these functions indirectly through encoding/json or encoding/json/v2. # v1.19.0 (2026-10-06) diff --git a/columnar/columnar.go b/columnar/columnar.go index fad6627..c62d327 100644 --- a/columnar/columnar.go +++ b/columnar/columnar.go @@ -29,7 +29,8 @@ package columnar import ( - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "fmt" "maps" "reflect" @@ -43,8 +44,8 @@ import ( // prove interface implementations var _ interface { - json.Marshaler - json.Unmarshaler + json.MarshalerTo + json.UnmarshalerFrom } = &List[bool]{} // cache for auto-generated columnar struct types @@ -89,8 +90,8 @@ func getColumnarType(t reflect.Type) reflect.Type { return result } -// MarshalJSON implements the [json.Marshaler] interface. -func (l List[T]) MarshalJSON() ([]byte, error) { +// MarshalJSONTo implements the [json.MarshalerTo] interface. +func (l List[T]) MarshalJSONTo(enc *jsontext.Encoder) error { t := reflect.TypeFor[T]() for t.Kind() == reflect.Pointer { t = t.Elem() @@ -106,7 +107,7 @@ func (l List[T]) MarshalJSON() ([]byte, error) { }) if len(columns) == 0 { zero := reflect.New(t).Elem().Interface() - return nil, fmt.Errorf("%[1]T has no exported fields", zero) + return fmt.Errorf("%[1]T has no exported fields", zero) } for idx, elem := range l { @@ -119,11 +120,11 @@ func (l List[T]) MarshalJSON() ([]byte, error) { }) } - return json.Marshal(cv.Interface()) + return json.MarshalEncode(enc, cv.Interface()) } -// UnmarshalJSON implements the [json.Unmarshaler] interface. -func (l *List[T]) UnmarshalJSON(buf []byte) error { +// UnmarshalJSONFrom implements the [json.UnmarshalerFrom] interface. +func (l *List[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error { t := reflect.TypeFor[T]() for t.Kind() == reflect.Pointer { t = t.Elem() @@ -131,7 +132,7 @@ func (l *List[T]) UnmarshalJSON(buf []byte) error { ct := getColumnarType(t) cv := reflect.New(ct) - err := json.Unmarshal(buf, cv.Interface()) + err := json.UnmarshalDecode(dec, cv.Interface()) if err != nil { return err } @@ -1,4 +1,4 @@ module go.xyrillian.de/gg -// TODO for 1.27: deprecate gg/options and offer methods on Option instead; replace json.{Unm,M}arshaler with json/v2.{Unm,M}arshaler{From,To}; add pathrouter.Context.Value[T]() +// TODO for 1.27: deprecate gg/options and offer methods on Option instead; add pathrouter.Context.Value[T]() go 1.27 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(`"<irrelevant>"`), nil +func (irrelevant) MarshalJSONTo(enc *jsontext.Encoder) error { + return enc.WriteToken(jsontext.String("<irrelevant>")) } 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{} ) diff --git a/option/option.go b/option/option.go index c41dd61..fc4cf3e 100644 --- a/option/option.go +++ b/option/option.go @@ -111,7 +111,8 @@ package option // import "go.xyrillian.de/gg/option" import ( "database/sql" "database/sql/driver" - "encoding/json" + "encoding/json/jsontext" + "encoding/json/v2" "fmt" "iter" ) @@ -299,11 +300,11 @@ func (o Option[T]) Xor(other Option[T]) Option[T] { // These are static assertions that Option implements the intended interfaces. // (The YAML interfaces are not checked because we don't want to add 3rd-party lib deps here.) var ( - _ fmt.Formatter = Option[bool]{} - _ sql.Scanner = &Option[bool]{} - _ driver.Valuer = Option[bool]{} - _ json.Marshaler = Option[bool]{} - _ json.Unmarshaler = &Option[bool]{} + _ fmt.Formatter = Option[bool]{} + _ sql.Scanner = &Option[bool]{} + _ driver.Valuer = Option[bool]{} + _ json.MarshalerTo = Option[bool]{} + _ json.UnmarshalerFrom = &Option[bool]{} ) // Format implements the [fmt.Formatter] interface. @@ -353,23 +354,29 @@ func (o Option[T]) Value() (driver.Value, error) { } } -// MarshalJSON implements the [json.Marshaler] interface. -func (o Option[T]) MarshalJSON() ([]byte, error) { +// MarshalJSONTo implements the [json.MarshalerTo] interface. +func (o Option[T]) MarshalJSONTo(enc *jsontext.Encoder) error { if o.isSome { - return json.Marshal(o.value) + return json.MarshalEncode(enc, o.value) } else { - return []byte("null"), nil + return enc.WriteToken(jsontext.Null) } } -// UnmarshalJSON implements the [json.Unmarshaler] interface. -func (o *Option[T]) UnmarshalJSON(buf []byte) error { - var data *T - err := json.Unmarshal(buf, &data) +// UnmarshalJSONFrom implements the [json.UnmarshalerFrom] interface. +func (o *Option[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error { + if dec.PeekKind() == jsontext.KindNull { + *o = None[T]() + _, err := dec.ReadToken() + return err + } + + var data T + err := json.UnmarshalDecode(dec, &data) if err != nil { return err } - *o = fromPointer(data) + *o = Some(data) return nil } |
