summaryrefslogtreecommitdiffhomepage
path: root/types
diff options
context:
space:
mode:
Diffstat (limited to 'types')
-rw-r--r--types/opt/value.go16
-rw-r--r--types/opt/value_test.go5
-rw-r--r--types/prefs/item.go16
-rw-r--r--types/prefs/list.go21
-rw-r--r--types/prefs/map.go16
-rw-r--r--types/prefs/prefs.go21
-rw-r--r--types/prefs/prefs_example/prefs_types.go25
-rw-r--r--types/prefs/prefs_test.go35
-rw-r--r--types/prefs/struct_list.go16
-rw-r--r--types/prefs/struct_map.go16
10 files changed, 113 insertions, 74 deletions
diff --git a/types/opt/value.go b/types/opt/value.go
index b47b03c81..c71c53e51 100644
--- a/types/opt/value.go
+++ b/types/opt/value.go
@@ -100,31 +100,31 @@ func (o Value[T]) Equal(v Value[T]) bool {
return false
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (o Value[T]) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error {
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (o Value[T]) MarshalJSONTo(enc *jsontext.Encoder) error {
if !o.set {
return enc.WriteToken(jsontext.Null)
}
- return jsonv2.MarshalEncode(enc, &o.value, opts)
+ return jsonv2.MarshalEncode(enc, &o.value)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (o *Value[T]) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (o *Value[T]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
if dec.PeekKind() == 'n' {
*o = Value[T]{}
_, err := dec.ReadToken() // read null
return err
}
o.set = true
- return jsonv2.UnmarshalDecode(dec, &o.value, opts)
+ return jsonv2.UnmarshalDecode(dec, &o.value)
}
// MarshalJSON implements [json.Marshaler].
func (o Value[T]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(o) // uses MarshalJSONV2
+ return jsonv2.Marshal(o) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (o *Value[T]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, o) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, o) // uses UnmarshalJSONFrom
}
diff --git a/types/opt/value_test.go b/types/opt/value_test.go
index dbd8b255f..890f9a579 100644
--- a/types/opt/value_test.go
+++ b/types/opt/value_test.go
@@ -13,6 +13,11 @@ import (
"tailscale.com/util/must"
)
+var (
+ _ jsonv2.MarshalerTo = (*Value[bool])(nil)
+ _ jsonv2.UnmarshalerFrom = (*Value[bool])(nil)
+)
+
type testStruct struct {
Int int `json:",omitempty,omitzero"`
Str string `json:",omitempty"`
diff --git a/types/prefs/item.go b/types/prefs/item.go
index 103204147..717a0c76c 100644
--- a/types/prefs/item.go
+++ b/types/prefs/item.go
@@ -152,15 +152,15 @@ func (iv ItemView[T, V]) Equal(iv2 ItemView[T, V]) bool {
return iv.ж.Equal(*iv2.ж)
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (iv ItemView[T, V]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return iv.ж.MarshalJSONV2(out, opts)
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (iv ItemView[T, V]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return iv.ж.MarshalJSONTo(out)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (iv *ItemView[T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (iv *ItemView[T, V]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
var x Item[T]
- if err := x.UnmarshalJSONV2(in, opts); err != nil {
+ if err := x.UnmarshalJSONFrom(in); err != nil {
return err
}
iv.ж = &x
@@ -169,10 +169,10 @@ func (iv *ItemView[T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Opti
// MarshalJSON implements [json.Marshaler].
func (iv ItemView[T, V]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(iv) // uses MarshalJSONV2
+ return jsonv2.Marshal(iv) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (iv *ItemView[T, V]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, iv) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, iv) // uses UnmarshalJSONFrom
}
diff --git a/types/prefs/list.go b/types/prefs/list.go
index 9830e79de..7db473887 100644
--- a/types/prefs/list.go
+++ b/types/prefs/list.go
@@ -157,15 +157,20 @@ func (lv ListView[T]) Equal(lv2 ListView[T]) bool {
return lv.ж.Equal(*lv2.ж)
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (lv ListView[T]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return lv.ж.MarshalJSONV2(out, opts)
+var (
+ _ jsonv2.MarshalerTo = (*ListView[bool])(nil)
+ _ jsonv2.UnmarshalerFrom = (*ListView[bool])(nil)
+)
+
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (lv ListView[T]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return lv.ж.MarshalJSONTo(out)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (lv *ListView[T]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (lv *ListView[T]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
var x List[T]
- if err := x.UnmarshalJSONV2(in, opts); err != nil {
+ if err := x.UnmarshalJSONFrom(in); err != nil {
return err
}
lv.ж = &x
@@ -174,10 +179,10 @@ func (lv *ListView[T]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options
// MarshalJSON implements [json.Marshaler].
func (lv ListView[T]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(lv) // uses MarshalJSONV2
+ return jsonv2.Marshal(lv) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (lv *ListView[T]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, lv) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, lv) // uses UnmarshalJSONFrom
}
diff --git a/types/prefs/map.go b/types/prefs/map.go
index 2bd32bfbd..4b64690ed 100644
--- a/types/prefs/map.go
+++ b/types/prefs/map.go
@@ -133,15 +133,15 @@ func (mv MapView[K, V]) Equal(mv2 MapView[K, V]) bool {
return mv.ж.Equal(*mv2.ж)
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (mv MapView[K, V]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return mv.ж.MarshalJSONV2(out, opts)
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (mv MapView[K, V]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return mv.ж.MarshalJSONTo(out)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (mv *MapView[K, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (mv *MapView[K, V]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
var x Map[K, V]
- if err := x.UnmarshalJSONV2(in, opts); err != nil {
+ if err := x.UnmarshalJSONFrom(in); err != nil {
return err
}
mv.ж = &x
@@ -150,10 +150,10 @@ func (mv *MapView[K, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Optio
// MarshalJSON implements [json.Marshaler].
func (mv MapView[K, V]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(mv) // uses MarshalJSONV2
+ return jsonv2.Marshal(mv) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (mv *MapView[K, V]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, mv) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, mv) // uses UnmarshalJSONFrom
}
diff --git a/types/prefs/prefs.go b/types/prefs/prefs.go
index 4f7902077..a6caf1283 100644
--- a/types/prefs/prefs.go
+++ b/types/prefs/prefs.go
@@ -158,22 +158,27 @@ func (p *preference[T]) SetReadOnly(readonly bool) {
p.s.Metadata.ReadOnly = readonly
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (p preference[T]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return jsonv2.MarshalEncode(out, &p.s, opts)
+var (
+ _ jsonv2.MarshalerTo = (*preference[struct{}])(nil)
+ _ jsonv2.UnmarshalerFrom = (*preference[struct{}])(nil)
+)
+
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (p preference[T]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return jsonv2.MarshalEncode(out, &p.s)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (p *preference[T]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
- return jsonv2.UnmarshalDecode(in, &p.s, opts)
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (p *preference[T]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
+ return jsonv2.UnmarshalDecode(in, &p.s)
}
// MarshalJSON implements [json.Marshaler].
func (p preference[T]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(p) // uses MarshalJSONV2
+ return jsonv2.Marshal(p) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (p *preference[T]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONFrom
}
diff --git a/types/prefs/prefs_example/prefs_types.go b/types/prefs/prefs_example/prefs_types.go
index 49f0d8c3c..c35f1f62f 100644
--- a/types/prefs/prefs_example/prefs_types.go
+++ b/types/prefs/prefs_example/prefs_types.go
@@ -48,10 +48,10 @@ import (
// the `omitzero` JSON tag option. This option is not supported by the
// [encoding/json] package as of 2024-08-21; see golang/go#45669.
// It is recommended that a prefs type implements both
-// [jsonv2.MarshalerV2]/[jsonv2.UnmarshalerV2] and [json.Marshaler]/[json.Unmarshaler]
+// [jsonv2.MarshalerTo]/[jsonv2.UnmarshalerFrom] and [json.Marshaler]/[json.Unmarshaler]
// to ensure consistent and more performant marshaling, regardless of the JSON package
// used at the call sites; the standard marshalers can be implemented via [jsonv2].
-// See [Prefs.MarshalJSONV2], [Prefs.UnmarshalJSONV2], [Prefs.MarshalJSON],
+// See [Prefs.MarshalJSONTo], [Prefs.UnmarshalJSONFrom], [Prefs.MarshalJSON],
// and [Prefs.UnmarshalJSON] for an example implementation.
type Prefs struct {
ControlURL prefs.Item[string] `json:",omitzero"`
@@ -128,34 +128,39 @@ type AppConnectorPrefs struct {
Advertise prefs.Item[bool] `json:",omitzero"`
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
+var (
+ _ jsonv2.MarshalerTo = (*Prefs)(nil)
+ _ jsonv2.UnmarshalerFrom = (*Prefs)(nil)
+)
+
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
// It is implemented as a performance improvement and to enable omission of
// unconfigured preferences from the JSON output. See the [Prefs] doc for details.
-func (p Prefs) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
+func (p Prefs) MarshalJSONTo(out *jsontext.Encoder) error {
// The prefs type shadows the Prefs's method set,
// causing [jsonv2] to use the default marshaler and avoiding
// infinite recursion.
type prefs Prefs
- return jsonv2.MarshalEncode(out, (*prefs)(&p), opts)
+ return jsonv2.MarshalEncode(out, (*prefs)(&p))
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (p *Prefs) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (p *Prefs) UnmarshalJSONFrom(in *jsontext.Decoder) error {
// The prefs type shadows the Prefs's method set,
// causing [jsonv2] to use the default unmarshaler and avoiding
// infinite recursion.
type prefs Prefs
- return jsonv2.UnmarshalDecode(in, (*prefs)(p), opts)
+ return jsonv2.UnmarshalDecode(in, (*prefs)(p))
}
// MarshalJSON implements [json.Marshaler].
func (p Prefs) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(p) // uses MarshalJSONV2
+ return jsonv2.Marshal(p) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (p *Prefs) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONFrom
}
type marshalAsTrueInJSON struct{}
diff --git a/types/prefs/prefs_test.go b/types/prefs/prefs_test.go
index ea4729366..d6af745bf 100644
--- a/types/prefs/prefs_test.go
+++ b/types/prefs/prefs_test.go
@@ -19,6 +19,20 @@ import (
//go:generate go run tailscale.com/cmd/viewer --tags=test --type=TestPrefs,TestBundle,TestValueStruct,TestGenericStruct,TestPrefsGroup
+var (
+ _ jsonv2.MarshalerTo = (*ItemView[*TestBundle, TestBundleView])(nil)
+ _ jsonv2.UnmarshalerFrom = (*ItemView[*TestBundle, TestBundleView])(nil)
+
+ _ jsonv2.MarshalerTo = (*MapView[string, string])(nil)
+ _ jsonv2.UnmarshalerFrom = (*MapView[string, string])(nil)
+
+ _ jsonv2.MarshalerTo = (*StructListView[*TestBundle, TestBundleView])(nil)
+ _ jsonv2.UnmarshalerFrom = (*StructListView[*TestBundle, TestBundleView])(nil)
+
+ _ jsonv2.MarshalerTo = (*StructMapView[string, *TestBundle, TestBundleView])(nil)
+ _ jsonv2.UnmarshalerFrom = (*StructMapView[string, *TestBundle, TestBundleView])(nil)
+)
+
type TestPrefs struct {
Int32Item Item[int32] `json:",omitzero"`
UInt64Item Item[uint64] `json:",omitzero"`
@@ -53,32 +67,37 @@ type TestPrefs struct {
Group TestPrefsGroup `json:",omitzero"`
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (p TestPrefs) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
+var (
+ _ jsonv2.MarshalerTo = (*TestPrefs)(nil)
+ _ jsonv2.UnmarshalerFrom = (*TestPrefs)(nil)
+)
+
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (p TestPrefs) MarshalJSONTo(out *jsontext.Encoder) error {
// The testPrefs type shadows the TestPrefs's method set,
// causing jsonv2 to use the default marshaler and avoiding
// infinite recursion.
type testPrefs TestPrefs
- return jsonv2.MarshalEncode(out, (*testPrefs)(&p), opts)
+ return jsonv2.MarshalEncode(out, (*testPrefs)(&p))
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (p *TestPrefs) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (p *TestPrefs) UnmarshalJSONFrom(in *jsontext.Decoder) error {
// The testPrefs type shadows the TestPrefs's method set,
// causing jsonv2 to use the default unmarshaler and avoiding
// infinite recursion.
type testPrefs TestPrefs
- return jsonv2.UnmarshalDecode(in, (*testPrefs)(p), opts)
+ return jsonv2.UnmarshalDecode(in, (*testPrefs)(p))
}
// MarshalJSON implements [json.Marshaler].
func (p TestPrefs) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(p) // uses MarshalJSONV2
+ return jsonv2.Marshal(p) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (p *TestPrefs) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, p) // uses UnmarshalJSONFrom
}
// TestBundle is an example structure type that,
diff --git a/types/prefs/struct_list.go b/types/prefs/struct_list.go
index 872cb2326..65f11011a 100644
--- a/types/prefs/struct_list.go
+++ b/types/prefs/struct_list.go
@@ -169,15 +169,15 @@ func (lv StructListView[T, V]) Equal(lv2 StructListView[T, V]) bool {
return lv.ж.Equal(*lv2.ж)
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (lv StructListView[T, V]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return lv.ж.MarshalJSONV2(out, opts)
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (lv StructListView[T, V]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return lv.ж.MarshalJSONTo(out)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (lv *StructListView[T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (lv *StructListView[T, V]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
var x StructList[T]
- if err := x.UnmarshalJSONV2(in, opts); err != nil {
+ if err := x.UnmarshalJSONFrom(in); err != nil {
return err
}
lv.ж = &x
@@ -186,10 +186,10 @@ func (lv *StructListView[T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv
// MarshalJSON implements [json.Marshaler].
func (lv StructListView[T, V]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(lv) // uses MarshalJSONV2
+ return jsonv2.Marshal(lv) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (lv *StructListView[T, V]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, lv) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, lv) // uses UnmarshalJSONFrom
}
diff --git a/types/prefs/struct_map.go b/types/prefs/struct_map.go
index 4d55da7a0..a081f7c74 100644
--- a/types/prefs/struct_map.go
+++ b/types/prefs/struct_map.go
@@ -149,15 +149,15 @@ func (mv StructMapView[K, T, V]) Equal(mv2 StructMapView[K, T, V]) bool {
return mv.ж.Equal(*mv2.ж)
}
-// MarshalJSONV2 implements [jsonv2.MarshalerV2].
-func (mv StructMapView[K, T, V]) MarshalJSONV2(out *jsontext.Encoder, opts jsonv2.Options) error {
- return mv.ж.MarshalJSONV2(out, opts)
+// MarshalJSONTo implements [jsonv2.MarshalerTo].
+func (mv StructMapView[K, T, V]) MarshalJSONTo(out *jsontext.Encoder) error {
+ return mv.ж.MarshalJSONTo(out)
}
-// UnmarshalJSONV2 implements [jsonv2.UnmarshalerV2].
-func (mv *StructMapView[K, T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jsonv2.Options) error {
+// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
+func (mv *StructMapView[K, T, V]) UnmarshalJSONFrom(in *jsontext.Decoder) error {
var x StructMap[K, T]
- if err := x.UnmarshalJSONV2(in, opts); err != nil {
+ if err := x.UnmarshalJSONFrom(in); err != nil {
return err
}
mv.ж = &x
@@ -166,10 +166,10 @@ func (mv *StructMapView[K, T, V]) UnmarshalJSONV2(in *jsontext.Decoder, opts jso
// MarshalJSON implements [json.Marshaler].
func (mv StructMapView[K, T, V]) MarshalJSON() ([]byte, error) {
- return jsonv2.Marshal(mv) // uses MarshalJSONV2
+ return jsonv2.Marshal(mv) // uses MarshalJSONTo
}
// UnmarshalJSON implements [json.Unmarshaler].
func (mv *StructMapView[K, T, V]) UnmarshalJSON(b []byte) error {
- return jsonv2.Unmarshal(b, mv) // uses UnmarshalJSONV2
+ return jsonv2.Unmarshal(b, mv) // uses UnmarshalJSONFrom
}