How can I represent a 2-dimensional array in Protocol Buffers?
I need to store int
and double
2d arrays as a field on a PB message, for example:
int[][] multi = new int[5][10];
I'm using C++, Java and C#.
Thanks in advance.
How can I represent a 2-dimensional array in Protocol Buffers?
I need to store int
and double
2d arrays as a field on a PB message, for example:
int[][] multi = new int[5][10];
I'm using C++, Java and C#.
Thanks in advance.
There is no direct support in the protocol for this. Your best bet is to have a repeated set of objects that have an array each - i.e.
message Foo {
repeated int items = 1;
}
...
repeated Foo foos = 1;
You could try structpb. like this:
.proto
import "google/protobuf/struct.proto";
message Foo {
repeated google.protobuf.Value array = 1;
}
.go
import (
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/encoding/protojson"
)
...
foo := &Foo{}
items := []interface{}{1,2,3,4,5}
l, err := structpb.NewList(items)
foo.array = append(foo.array, structpb.NewListValue(l))
protojson.Format(foo) // [[1,2,3,4,5]]
It does not work.
I look up the grpc guide, which just supports the key-value structure. I advise you:
message ListResp {
repeated Array list = 1;
}
message Array{
int64 id = 1; //
string name = 2; //
}