2020-10-19 18:23:11 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2020-10-28 12:10:54 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-10-19 18:23:11 +00:00
|
|
|
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
|
2020-10-19 19:09:13 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2020-10-19 18:23:11 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
var (
|
|
|
|
errEmptyInput = errors.New("empty input")
|
|
|
|
)
|
|
|
|
|
|
|
|
func ContainerToJSON(c *Container) ([]byte, error) {
|
2020-10-19 18:23:11 +00:00
|
|
|
if c == nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 18:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := ContainerToGRPCMessage(c)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
2020-10-19 18:23:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func ContainerFromJSON(data []byte) (*Container, error) {
|
2020-10-19 18:23:11 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 18:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := new(container.Container)
|
|
|
|
|
2020-10-19 19:09:13 +00:00
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, err
|
2020-10-19 18:23:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return ContainerFromGRPCMessage(msg), nil
|
2020-10-19 18:23:11 +00:00
|
|
|
}
|