{"id":4565,"date":"2014-03-30T13:32:36","date_gmt":"2014-03-30T13:32:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/finding-protocol-buffer-message-type-from-serialized-data-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:32:36","modified_gmt":"2014-03-30T13:32:36","slug":"finding-protocol-buffer-message-type-from-serialized-data-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/finding-protocol-buffer-message-type-from-serialized-data-collection-of-common-programming-errors\/","title":{"rendered":"Finding protocol buffer message type from serialized data-Collection of common programming errors"},"content":{"rendered":"<p>protobuf does not include any type information on the wire (unless you do that yourself external to protobuf). As such you <strong>cannot<\/strong> strictly validate that &#8211; which is actually a <em>good<\/em> thing, because it means that types are interchangeable and compatible. As long as <code>class abc<\/code> <em>has a compatible contract<\/em> to the other type, it will work. By &#8220;compatible&#8221; here, I mean: for any field-numbers that are common to both, they have compatible wire-types. If <code>abc<\/code> declares field 4 to be a string, and the other class declares field 4 to be a double-precision number, then it will fail at deserialize.<\/p>\n<p>One other &#8220;signal&#8221; you could use is the omission of <code>required<\/code> fields: if <code>abc<\/code> always includes field 3, but you get data that <em>omits<\/em> field 3, then it probably isn&#8217;t an <code>abc<\/code>. Note that protobuf is designed to be version tolerant, though: you can&#8217;t assume that <em>extra<\/em> fields mean it isn&#8217;t an <code>abc<\/code>, as it could be that the data is using a later version of the contract, or is using extension fields. Likewise, missing <em>optional<\/em> fields could be missing because <em>either<\/em> they simply chose not to provide a value, <em>or<\/em> that field is not declared on the version of the contract they are using.<\/p>\n<p>Re testing for a successful parse: that will be implementation specific. I would <em>imagine<\/em> that the c++ implementation will have either a return-value to check, or a flag field to check. I don&#8217;t use that api myself so I cannot say. On some other platforms I would expect an exception to be thrown (java, .net, etc) if there was a critical issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>protobuf does not include any type information on the wire (unless you do that yourself external to protobuf). As such you cannot strictly validate that &#8211; which is actually a good thing, because it means that types are interchangeable and compatible. As long as class abc has a compatible contract to the other type, it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4565","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4565"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4565\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}