0

Here are three examples of protobuf syntax.

  1. mscoco_label_map.pbtxt

    item {
      name: "/m/01g317"
      id: 1
      display_name: "person"
    }
    item {
      name: "/m/0199g"
      id: 2
      display_name: "bicycle"
    }
    ...
    
  2. en.wikipedia.org

    anotherfield {
      foo: 123
      bar: 456
    }
    anotherfield {
      foo: 222
      bar: 333
    }
    
  3. Official documentation

    syntax = "proto3";
    
    message SearchRequest {
      string query = 1;
      int32 page_number = 2;
      int32 result_per_page = 3;
    }
    

Official (#3) example is obviously differs from #1 and #2. Did I miss a paragraph in official documentation which states that colon can be used instead of equal sign?

Official documentation describes a JSON Mapping, but there is not a single example that looks like #1 and #2. Also #1 and #2 is not a valid JSON either (missing quotes around keys, missing commas).

Q: where are #1 and #2 syntax came from?

Link to better (than official docs) syntax description is appreciated.

Dmitriy Work
  • 773
  • 10
  • 17
  • 2
    Your first two examples appear to be depicting *payloads*, where-as the third example is a *schema*; it is not surprising that these are entirely different, with the latter being the .proto syntax, and the former appearing to be ... well, I *would* say that they are depicting the opinionated JSON variant of protobuf, but honestly: that's not valid JSON, so it would surprise me hugely if that is actual data. Are you sure it isn't simply trying to depict the semantic structure, not the actual format? – Marc Gravell Jun 15 '21 at 19:09

1 Answers1

1

Thanks to Marc's Gravell response I was able to find out the answer.

#3 is a schema, proto syntax, think XSD (XML Schema Definition).

#1, #2 is a text dump of an actual data (payload), textproto syntax, *.pbtxt file extension, think XML or JSON.

Related question: What does the protobuf text format look like?

Related links:

  1. https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.text_format

    C++ API for printing and parsing protocol messages in a human-readable, text-based format.

  2. https://googleapis.dev/python/protobuf/latest/google/protobuf/text_format.html

    Python API

  3. https://developers.google.com/protocol-buffers/docs/reference/proto3-spec

    Protocol Buffers Version 3 Language Specification

  4. https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/text_format.cc#L288

    Source code (C++) of text format ASCII representation parser.

  5. https://medium.com/@nathantnorth/protocol-buffers-text-format-14e0584f70a5

    Protocol Buffers: Text Format (2019-10-11, Nathan North)

    There are a couple different ways to structure the data (instead of using brackets, you could use <>) but unfortunately nothing in this realm is terribly well documented so it’s more of a game of try it and see if it works.

  6. https://gist.github.com/henridf/704c1c812f04a502c1c26f77a739090b

    Encoding a protobuf with protoc --encode (syntax example).

Clear syntax documentation of protobuf text format is still missing.

Dmitriy Work
  • 773
  • 10
  • 17