7

I am looking for a xpath like query language for protobuf messages. For example, for the Person message shown below [ borrowed from the Developer guide ]

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

I would like to have methods like

XPBQuery.get(person, "$.id") ==> returns the id
XPBQuery.get(person, "$.name") ==> returns the name
XPBQuery.get(person, "$.phone.number[0]") ==> returns the first phone number

One way is to convert the proto to Json and use a JsonPath/JsPath API's. But it may be expensive to convert to Json everytime especially for large Proto objects.

Any help is much appreciated.

Thanks, Irfan

Irfan
  • 71
  • 1
  • 4
  • I don't know if this exists already, but if not you can implement this in terms of the Protobuf descriptor and reflection interfaces (not to be confused with Java reflection -- Protobuf reflection is provided by the Protobuf library). See `com.google.protobuf.Message` in Java or `google::protobuf::Reflection` in C++. – Kenton Varda Feb 15 '15 at 06:16
  • @Irfan - Wanted to check if you figured out a way to achieve this? I want to take out JSON.NET (that supports JSONPath) and use ProtoBuf, but lack of querying capability stops me. – Lalman Jun 05 '15 at 12:21
  • If you convert your message to java bean then you may use [JXPath](https://commons.apache.org/proper/commons-jxpath/users-guide.html) – Konstantin Pavlov Jun 28 '15 at 10:32

2 Answers2

1

Support for that is coming in protobuf v3: https://github.com/google/protobuf/blob/4644f99d1af4250dec95339be6a13e149787ab33/src/google/protobuf/field_mask.proto

Igor Gatis
  • 4,648
  • 10
  • 43
  • 66
0

While looking for a solution to a similar problem I discovered:

(I did not use those libraries as my target language is C++, but hope this might help someone else)

Good luck!

vlp
  • 7,811
  • 2
  • 23
  • 51