2019-11-27 19:09:42 +08:00
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
2022-08-18 03:42:03 +08:00
|
|
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
2019-11-27 19:09:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonpMarshalUnmarshal(t *testing.T) {
|
|
|
|
|
2019-12-03 07:52:33 +08:00
|
|
|
tv := &volume_server_pb.RemoteFile{
|
2019-11-27 19:09:42 +08:00
|
|
|
BackendType: "aws",
|
2019-12-03 12:49:58 +08:00
|
|
|
BackendId: "",
|
|
|
|
FileSize: 12,
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:15:46 +08:00
|
|
|
m := jsonpb.MarshalOptions{
|
|
|
|
EmitUnpopulated: true,
|
|
|
|
Indent: " ",
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:15:46 +08:00
|
|
|
if text, err := m.Marshal(tv); err != nil {
|
2019-11-27 19:09:42 +08:00
|
|
|
fmt.Printf("marshal eror: %v\n", err)
|
|
|
|
} else {
|
2022-08-18 15:15:46 +08:00
|
|
|
fmt.Printf("marshalled: %s\n", string(text))
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rawJson := `{
|
|
|
|
"backendType":"aws",
|
2019-12-03 07:52:33 +08:00
|
|
|
"backendId":"temp",
|
2022-05-02 00:02:01 +08:00
|
|
|
"fileSize":12
|
2019-11-27 19:09:42 +08:00
|
|
|
}`
|
|
|
|
|
2019-12-03 07:52:33 +08:00
|
|
|
tv1 := &volume_server_pb.RemoteFile{}
|
2022-08-18 15:15:46 +08:00
|
|
|
if err := jsonpb.Unmarshal([]byte(rawJson), tv1); err != nil {
|
2019-12-03 07:06:12 +08:00
|
|
|
fmt.Printf("unmarshal error: %v\n", err)
|
2019-11-27 19:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("unmarshalled: %+v\n", tv1)
|
|
|
|
|
|
|
|
}
|