{"id":1247,"date":"2017-05-30T17:04:34","date_gmt":"2017-05-30T17:04:34","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1247"},"modified":"2024-09-04T15:39:21","modified_gmt":"2024-09-04T15:39:21","slug":"what-are-protocol-buffers","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-are-protocol-buffers\/","title":{"rendered":"What are Protocol Buffers?"},"content":{"rendered":"<h2>What are Protocol Buffers?<\/h2>\n<p>Protocol buffers a.k.a Protobufs are Google&#8217;s language-neutral, platform-neutral, extensible mechanism for serializing structured data. When you compare Protobuf with other serialization formats like XML and JSON, Protobufs are smaller, faster, and simpler. You can use Protobufs with many programming languages like Java,C#, C++, Python etc.<\/p>\n<p>You need to define your messages in .proto file with the protocol buffers language syntax. ( proto3 or proto2 syntax)<\/p>\n<p>For Example, Explicit &#8216;optional&#8217; labels are disallowed in the Proto3 syntax. To define &#8216;optional&#8217; fields in Proto3, simply remove the &#8216;optional&#8217; label, as fields are &#8216;optional&#8217; by default. Required fields are not allowed in proto3.<\/p>\n<p>More information can be found at Official Protocol Buffer github repository :<\/p>\n<p><a href=\"https:\/\/github.com\/google\/protobuf\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/google\/protobuf<\/a><\/p>\n<h3><strong>Working with protocol buffers.<\/strong><\/h3>\n<p>Basic steps involved in working with PBs are as follows:<\/p>\n<p>Step: Define message formats in a .proto file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">syntax = \"proto3\";\r\n\r\npackage com.testingdocs.messages;\r\n\r\noption java_package = \"com.testingdocs.proto\";\r\noption java_outer_classname = \"PersonProto\";\r\n\r\nmessage Person {\r\n\r\nrequired string name = 1;\r\n required int32 id = 2;\r\n optional string email = 3;\r\n \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Step: Download the compiler from here:<\/p>\n<p><a href=\"https:\/\/github.com\/google\/protobuf\/releases\/tag\/v3.3.0\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/google\/protobuf\/releases\/tag\/v3.3.0<\/a><\/p>\n<p>Use the protocol buffer compiler ( protoc ).<\/p>\n<p>Example sample command : dots in the command denote preset directory for -I\u00a0 and &#8211;java_out parameters. You can specify the source directories.<\/p>\n<p>\/&gt; protoc.exe -I=. &#8211;java_out=. person.proto<\/p>\n<p>Step: Use the Java PB API to read\/write messages.<\/p>\n<h3><strong>Protocol Buffer Maven dependency for Java:<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.google.protobuf\/protobuf-java --&gt;\r\n&lt;dependency&gt;\r\n &lt;groupId&gt;com.google.protobuf&lt;\/groupId&gt;\r\n &lt;artifactId&gt;protobuf-java&lt;\/artifactId&gt;\r\n &lt;version&gt;3.3.1&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-591\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Google-Protocol-Buffer-Message.jpeg\" alt=\"Google Protocol Buffer Message\" width=\"1363\" height=\"741\" title=\"\"><\/pre>\n<h3><\/h3>\n<h3><strong>Sample Java Program:<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.io.FileOutputStream;\r\n\r\nimport com.testingdocs.proto.PersonProto.Person;\r\n\r\npublic class ProtoBufferExample {\r\n \r\n public static void main(String[] args) throws Exception {\r\n FileOutputStream output;\r\n Person surendra = Person.newBuilder()\r\n .setId(007)\r\n .setName(\"S Kumar\")\r\n .setEmail(\"email@testingdocs.com\")\r\n .build();\r\n output = new FileOutputStream(\"ProgramOutput.txt\");\r\n surendra.writeTo(output);\r\n output.close();\r\n }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are Protocol Buffers? Protocol buffers a.k.a Protobufs are Google&#8217;s language-neutral, platform-neutral, extensible mechanism for serializing structured data. When you compare Protobuf with other serialization formats like XML and JSON, Protobufs are smaller, faster, and simpler. You can use Protobufs with many programming languages like Java,C#, C++, Python etc. You need to define your messages [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1247"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":24186,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1247\/revisions\/24186"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}