frostfs-api-go/v2/netmap/json.go
Alex Vanin b681b28e33 [#172] v2/netmap: Add JSON converter for node info
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
2020-10-20 13:45:14 +03:00

36 lines
584 B
Go

package netmap
import (
"github.com/golang/protobuf/jsonpb"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
)
func NodeInfoToJSON(n *NodeInfo) []byte {
if n == nil {
return nil
}
msg := NodeInfoToGRPCMessage(n)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func NodeInfoFromJSON(data []byte) *NodeInfo {
if len(data) == 0 {
return nil
}
msg := new(netmap.NodeInfo)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
return nil
}
return NodeInfoFromGRPCMessage(msg)
}