From 3e12d0eb69831d62ad31ed7263b50fd28c52298f Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 14 Mar 2022 16:52:45 +0300 Subject: [PATCH] [#172] object: Allow to marshal `SplitInfo` to JSON Signed-off-by: Evgenii Stratonikov --- object/splitinfo.go | 10 ++++++++++ object/splitinfo_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/object/splitinfo.go b/object/splitinfo.go index 2804011..4a2fcfa 100644 --- a/object/splitinfo.go +++ b/object/splitinfo.go @@ -65,3 +65,13 @@ func (s *SplitInfo) Marshal() ([]byte, error) { func (s *SplitInfo) Unmarshal(data []byte) error { return (*object.SplitInfo)(s).Unmarshal(data) } + +// MarshalJSON implements json.Marshaler. +func (s *SplitInfo) MarshalJSON() ([]byte, error) { + return (*object.SplitInfo)(s).MarshalJSON() +} + +// UnmarshalJSON implements json.Unmarshaler. +func (s *SplitInfo) UnmarshalJSON(data []byte) error { + return (*object.SplitInfo)(s).UnmarshalJSON(data) +} diff --git a/object/splitinfo_test.go b/object/splitinfo_test.go index 5f99641..1c42a4d 100644 --- a/object/splitinfo_test.go +++ b/object/splitinfo_test.go @@ -2,6 +2,7 @@ package object_test import ( "crypto/rand" + "encoding/json" "testing" objv2 "github.com/nspcc-dev/neofs-api-go/v2/object" @@ -87,3 +88,17 @@ func TestNewSplitInfo(t *testing.T) { require.Nil(t, siV2.GetLink()) }) } + +func TestSplitInfoMarshalJSON(t *testing.T) { + s := object.NewSplitInfo() + s.SetSplitID(object.NewSplitID()) + s.SetLastPart(generateID()) + s.SetLink(generateID()) + + data, err := s.MarshalJSON() + require.NoError(t, err) + + actual := object.NewSplitInfo() + require.NoError(t, json.Unmarshal(data, actual)) + require.Equal(t, s, actual) +}