seaweedfs/weed/pb/proto_read_write_test.go

44 lines
778 B
Go
Raw Normal View History

2019-11-27 11:09:42 +00:00
package pb
import (
"fmt"
"testing"
"github.com/golang/protobuf/jsonpb"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
2019-11-27 11:09:42 +00:00
)
func TestJsonpMarshalUnmarshal(t *testing.T) {
2019-12-02 23:52:33 +00:00
tv := &volume_server_pb.RemoteFile{
2019-11-27 11:09:42 +00:00
BackendType: "aws",
2019-12-03 04:49:58 +00:00
BackendId: "",
FileSize: 12,
2019-11-27 11:09:42 +00:00
}
m := jsonpb.Marshaler{
EmitDefaults: true,
2019-11-27 20:34:57 +00:00
Indent: " ",
2019-11-27 11:09:42 +00:00
}
if text, err := m.MarshalToString(tv); err != nil {
fmt.Printf("marshal eror: %v\n", err)
} else {
fmt.Printf("marshalled: %s\n", text)
}
rawJson := `{
"backendType":"aws",
2019-12-02 23:52:33 +00:00
"backendId":"temp",
2022-05-01 16:02:01 +00:00
"fileSize":12
2019-11-27 11:09:42 +00:00
}`
2019-12-02 23:52:33 +00:00
tv1 := &volume_server_pb.RemoteFile{}
2019-11-27 11:09:42 +00:00
if err := jsonpb.UnmarshalString(rawJson, tv1); err != nil {
2019-12-02 23:06:12 +00:00
fmt.Printf("unmarshal error: %v\n", err)
2019-11-27 11:09:42 +00:00
}
fmt.Printf("unmarshalled: %+v\n", tv1)
}