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

36 lines
603 B
Go

package container
import (
"github.com/golang/protobuf/jsonpb"
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
)
func ContainerToJSON(c *Container) []byte {
if c == nil {
return nil
}
msg := ContainerToGRPCMessage(c)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func ContainerFromJSON(data []byte) *Container {
if len(data) == 0 {
return nil
}
msg := new(container.Container)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
return nil
}
return ContainerFromGRPCMessage(msg)
}