frostfs-api-go/v2/refs/json.go
Leonard Lyubich 4b55b06780 [#168] refs: Implement binary/JSON encoders/decoders on Signature
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-11-13 18:07:26 +03:00

126 lines
2.3 KiB
Go

package refs
import (
refs "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func (a *Address) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
AddressToGRPCMessage(a),
)
}
func (a *Address) UnmarshalJSON(data []byte) error {
msg := new(refs.Address)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*a = *AddressFromGRPCMessage(msg)
return nil
}
func (o *ObjectID) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
ObjectIDToGRPCMessage(o),
)
}
func (o *ObjectID) UnmarshalJSON(data []byte) error {
msg := new(refs.ObjectID)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*o = *ObjectIDFromGRPCMessage(msg)
return nil
}
func (c *ContainerID) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
ContainerIDToGRPCMessage(c),
)
}
func (c *ContainerID) UnmarshalJSON(data []byte) error {
msg := new(refs.ContainerID)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*c = *ContainerIDFromGRPCMessage(msg)
return nil
}
func (o *OwnerID) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
OwnerIDToGRPCMessage(o),
)
}
func (o *OwnerID) UnmarshalJSON(data []byte) error {
msg := new(refs.OwnerID)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*o = *OwnerIDFromGRPCMessage(msg)
return nil
}
func (v *Version) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
VersionToGRPCMessage(v),
)
}
func (v *Version) UnmarshalJSON(data []byte) error {
msg := new(refs.Version)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*v = *VersionFromGRPCMessage(msg)
return nil
}
func (s *Signature) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
SignatureToGRPCMessage(s),
)
}
func (s *Signature) UnmarshalJSON(data []byte) error {
msg := new(refs.Signature)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*s = *SignatureFromGRPCMessage(msg)
return nil
}