[#168] accounting: Implement binary/JSON encoders/decoders on Decimal

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 13:38:49 +03:00 committed by Alex Vanin
parent 52fae76533
commit 9bebc1247d
6 changed files with 139 additions and 25 deletions

26
v2/accounting/json.go Normal file
View file

@ -0,0 +1,26 @@
package accounting
import (
accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func (d *Decimal) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
DecimalToGRPCMessage(d),
)
}
func (d *Decimal) UnmarshalJSON(data []byte) error {
msg := new(accounting.Decimal)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*d = *DecimalFromGRPCMessage(msg)
return nil
}