Skip to content

Commit

Permalink
[#172] v2: Use protojson package
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
  • Loading branch information
alexvanin committed Oct 20, 2020
1 parent fd8e691 commit e6035a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
25 changes: 11 additions & 14 deletions v2/acl/json.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions v2/container/json.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions v2/netmap/json.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit e6035a3

Please sign in to comment.