proto_read_write_test.go 810 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package pb
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  6. jsonpb "google.golang.org/protobuf/encoding/protojson"
  7. )
  8. func TestJsonpMarshalUnmarshal(t *testing.T) {
  9. tv := &volume_server_pb.RemoteFile{
  10. BackendType: "aws",
  11. BackendId: "",
  12. FileSize: 12,
  13. }
  14. m := jsonpb.MarshalOptions{
  15. EmitUnpopulated: true,
  16. Indent: " ",
  17. }
  18. if text, err := m.Marshal(tv); err != nil {
  19. fmt.Printf("marshal eror: %v\n", err)
  20. } else {
  21. fmt.Printf("marshalled: %s\n", string(text))
  22. }
  23. rawJson := `{
  24. "backendType":"aws",
  25. "backendId":"temp",
  26. "fileSize":12
  27. }`
  28. tv1 := &volume_server_pb.RemoteFile{}
  29. if err := jsonpb.Unmarshal([]byte(rawJson), tv1); err != nil {
  30. fmt.Printf("unmarshal error: %v\n", err)
  31. }
  32. fmt.Printf("unmarshalled: %+v\n", tv1)
  33. }