From 9eda317efe1a145695bafdd307baf40e9ed56275 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 17 Feb 2021 20:32:23 +0300 Subject: [PATCH] [#259] netmap: Implement JSON encoding for NetworkInfo RPC messages Signed-off-by: Leonard Lyubich --- v2/netmap/json.go | 20 ++++++++++++++++++++ v2/netmap/json_test.go | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/v2/netmap/json.go b/v2/netmap/json.go index 72105017..3445f05f 100644 --- a/v2/netmap/json.go +++ b/v2/netmap/json.go @@ -124,3 +124,23 @@ func (ni *NodeInfo) UnmarshalJSON(data []byte) error { return nil } + +func (i *NetworkInfo) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + NetworkInfoToGRPCMessage(i), + ) +} + +func (i *NetworkInfo) UnmarshalJSON(data []byte) error { + msg := new(netmap.NetworkInfo) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *i = *NetworkInfoFromGRPCMessage(msg) + + return nil +} diff --git a/v2/netmap/json_test.go b/v2/netmap/json_test.go index 561dbf12..3f01b034 100644 --- a/v2/netmap/json_test.go +++ b/v2/netmap/json_test.go @@ -66,3 +66,15 @@ func TestNodeInfoJSON(t *testing.T) { require.Equal(t, i, i2) } + +func TestNetworkInfoJSON(t *testing.T) { + i := generateNetworkInfo() + + data, err := i.MarshalJSON() + require.NoError(t, err) + + i2 := new(netmap.NetworkInfo) + require.NoError(t, i2.UnmarshalJSON(data)) + + require.Equal(t, i, i2) +}