[#172] v2: Use protojson package

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-19 22:09:13 +03:00 committed by Alex Vanin
parent fd8e691f61
commit e6035a3249
3 changed files with 21 additions and 26 deletions

View file

@ -1,24 +1,23 @@
package acl
import (
"github.com/golang/protobuf/jsonpb"
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func RecordToJSON(r *Record) []byte {
func RecordToJSON(r *Record) (data []byte) {
if r == nil {
return nil
}
msg := RecordToGRPCMessage(r)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
data, err := protojson.Marshal(msg)
if err != nil {
return nil
}
return []byte(s)
return
}
func RecordFromJSON(data []byte) *Record {
@ -28,7 +27,7 @@ func RecordFromJSON(data []byte) *Record {
msg := new(acl.EACLRecord)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
if err := protojson.Unmarshal(data, msg); err != nil {
return nil
}
@ -41,14 +40,13 @@ func TableToJSON(t *Table) (data []byte) {
}
msg := TableToGRPCMessage(t)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
data, err := protojson.Marshal(msg)
if err != nil {
return nil
}
return []byte(s)
return
}
func TableFromJSON(data []byte) *Table {
@ -58,7 +56,7 @@ func TableFromJSON(data []byte) *Table {
msg := new(acl.EACLTable)
if jsonpb.UnmarshalString(string(data), msg) != nil {
if err := protojson.Unmarshal(data, msg); err != nil {
return nil
}
@ -71,14 +69,13 @@ func BearerTokenToJSON(t *BearerToken) (data []byte) {
}
msg := BearerTokenToGRPCMessage(t)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
data, err := protojson.Marshal(msg)
if err != nil {
return nil
}
return []byte(s)
return
}
func BearerTokenFromJSON(data []byte) *BearerToken {
@ -88,7 +85,7 @@ func BearerTokenFromJSON(data []byte) *BearerToken {
msg := new(acl.BearerToken)
if jsonpb.UnmarshalString(string(data), msg) != nil {
if err := protojson.Unmarshal(data, msg); err != nil {
return nil
}

View file

@ -1,24 +1,23 @@
package container
import (
"github.com/golang/protobuf/jsonpb"
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func ContainerToJSON(c *Container) []byte {
func ContainerToJSON(c *Container) (data []byte) {
if c == nil {
return nil
}
msg := ContainerToGRPCMessage(c)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
data, err := protojson.Marshal(msg)
if err != nil {
return nil
}
return []byte(s)
return
}
func ContainerFromJSON(data []byte) *Container {
@ -28,7 +27,7 @@ func ContainerFromJSON(data []byte) *Container {
msg := new(container.Container)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
if err := protojson.Unmarshal(data, msg); err != nil {
return nil
}

View file

@ -1,24 +1,23 @@
package netmap
import (
"github.com/golang/protobuf/jsonpb"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func NodeInfoToJSON(n *NodeInfo) []byte {
func NodeInfoToJSON(n *NodeInfo) (data []byte) {
if n == nil {
return nil
}
msg := NodeInfoToGRPCMessage(n)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
data, err := protojson.Marshal(msg)
if err != nil {
return nil
}
return []byte(s)
return
}
func NodeInfoFromJSON(data []byte) *NodeInfo {
@ -28,7 +27,7 @@ func NodeInfoFromJSON(data []byte) *NodeInfo {
msg := new(netmap.NodeInfo)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
if err := protojson.Unmarshal(data, msg); err != nil {
return nil
}