diff --git a/v2/object/json.go b/v2/object/json.go index 7a3c2463..fe0a8484 100644 --- a/v2/object/json.go +++ b/v2/object/json.go @@ -44,3 +44,23 @@ func (a *Attribute) UnmarshalJSON(data []byte) error { return nil } + +func (h *SplitHeader) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + SplitHeaderToGRPCMessage(h), + ) +} + +func (h *SplitHeader) UnmarshalJSON(data []byte) error { + msg := new(object.Header_Split) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *h = *SplitHeaderFromGRPCMessage(msg) + + return nil +} diff --git a/v2/object/json_test.go b/v2/object/json_test.go index aa82f633..0c63b1bd 100644 --- a/v2/object/json_test.go +++ b/v2/object/json_test.go @@ -30,3 +30,15 @@ func TestAttributeJSON(t *testing.T) { require.Equal(t, a, a2) } + +func TestSplitHeaderJSON(t *testing.T) { + h := generateSplit("sig") + + data, err := h.MarshalJSON() + require.NoError(t, err) + + h2 := new(object.SplitHeader) + require.NoError(t, h2.UnmarshalJSON(data)) + + require.Equal(t, h, h2) +} diff --git a/v2/object/marshal.go b/v2/object/marshal.go index 657ddf15..8b15beb8 100644 --- a/v2/object/marshal.go +++ b/v2/object/marshal.go @@ -295,6 +295,17 @@ func (h *SplitHeader) StableSize() (size int) { return size } +func (h *SplitHeader) Unmarshal(data []byte) error { + m := new(object.Header_Split) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *h = *SplitHeaderFromGRPCMessage(m) + + return nil +} + func (h *Header) StableMarshal(buf []byte) ([]byte, error) { if h == nil { return []byte{}, nil diff --git a/v2/object/marshal_test.go b/v2/object/marshal_test.go index f73766e3..aac122ae 100644 --- a/v2/object/marshal_test.go +++ b/v2/object/marshal_test.go @@ -46,16 +46,13 @@ func TestSplitHeader_StableMarshal(t *testing.T) { hdr := generateHeader(123) from.SetParentHeader(hdr) - transport := new(grpc.Header_Split) - t.Run("non empty", func(t *testing.T) { wire, err := from.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + to := new(object.SplitHeader) + require.NoError(t, to.Unmarshal(wire)) - to := object.SplitHeaderFromGRPCMessage(transport) require.Equal(t, from, to) }) }