From e6035a3249010727c275c785d9a7ebd2977d311d Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 19 Oct 2020 22:09:13 +0300 Subject: [PATCH] [#172] v2: Use protojson package Signed-off-by: Alex Vanin --- v2/acl/json.go | 25 +++++++++++-------------- v2/container/json.go | 11 +++++------ v2/netmap/json.go | 11 +++++------ 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/v2/acl/json.go b/v2/acl/json.go index 2707793..d2237f8 100644 --- a/v2/acl/json.go +++ b/v2/acl/json.go @@ -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 } diff --git a/v2/container/json.go b/v2/container/json.go index edd7d48..c932fba 100644 --- a/v2/container/json.go +++ b/v2/container/json.go @@ -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 } diff --git a/v2/netmap/json.go b/v2/netmap/json.go index 6b840ca..bb1ed99 100644 --- a/v2/netmap/json.go +++ b/v2/netmap/json.go @@ -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 }