proto_read_write_test.go 778 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package pb
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  6. "github.com/golang/protobuf/jsonpb"
  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.Marshaler{
  15. EmitDefaults: true,
  16. Indent: " ",
  17. }
  18. if text, err := m.MarshalToString(tv); err != nil {
  19. fmt.Printf("marshal eror: %v\n", err)
  20. } else {
  21. fmt.Printf("marshalled: %s\n", text)
  22. }
  23. rawJson := `{
  24. "backendType":"aws",
  25. "backendId":"temp",
  26. "FileSize":12
  27. }`
  28. tv1 := &volume_server_pb.RemoteFile{}
  29. if err := jsonpb.UnmarshalString(rawJson, tv1); err != nil {
  30. fmt.Printf("unmarshal error: %v\n", err)
  31. }
  32. fmt.Printf("unmarshalled: %+v\n", tv1)
  33. }