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

86 lines
1.6 KiB
Go

package object
import (
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func (h *ShortHeader) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
ShortHeaderToGRPCMessage(h),
)
}
func (h *ShortHeader) UnmarshalJSON(data []byte) error {
msg := new(object.ShortHeader)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*h = *ShortHeaderFromGRPCMessage(msg)
return nil
}
func (a *Attribute) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
AttributeToGRPCMessage(a),
)
}
func (a *Attribute) UnmarshalJSON(data []byte) error {
msg := new(object.Header_Attribute)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*a = *AttributeFromGRPCMessage(msg)
return nil
}
func (h *SplitHeader) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
SplitHeaderToGRPCMessage(h),
)
}
func (h *SplitHeader) UnmarshalJSON(data []byte) error {
msg := new(object.Header_Split)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*h = *SplitHeaderFromGRPCMessage(msg)
return nil
}
func (h *Header) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
HeaderToGRPCMessage(h),
)
}
func (h *Header) UnmarshalJSON(data []byte) error {
msg := new(object.Header)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*h = *HeaderFromGRPCMessage(msg)
return nil
}