Protocol Buffers v3.0.0发布,Google公司开发的一种数据描述语言

jopen 6年前
   <p style="text-align: center;"><img alt="" src="" /></p>    <p>Protocol Buffers (ProtocolBuffer/protobuf)是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。现阶段支持C++、JAVA、Python等三种编程语言。</p>    <p><strong>为什么不只用XML?</strong>同XML相比,Protocol buffers在序列化结构化数据方面有许多优点(google官方提出):</p>    <ul>     <li>更简单</li>     <li>数据描述文件只需原来的1/10至1/3</li>     <li>解析速度是原来的20倍至100倍</li>     <li>减少了二义性</li>     <li>生成了更容易在编程中使用的数据访问类</li>    </ul>    <h2>更新日志</h2>    <h3>Proto3</h3>    <ul>     <li> <p>Introduced Protocol Buffers language version 3 (aka proto3).</p> <p>When protocol buffers was initially open sourced it implemented Protocol<br /> Buffers language version 2 (aka proto2), which is why the version number<br /> started from v2.0.0. From v3.0.0, a new language version (proto3) is<br /> introduced while the old version (proto2) will continue to be supported.</p> <p>The main intent of introducing proto3 is to clean up protobuf before pushing<br /> the language as the foundation of Google's new API platform. In proto3, the<br /> language is simplified, both for ease of use and to make it available in a<br /> wider range of programming languages. At the same time a few features are<br /> added to better support common idioms found in APIs.</p> <p>The following are the main new features in language version 3:</p>      <ol>       <li>Removal of field presence logic for primitive value fields, removal of required fields, and removal of default values. This makes proto3 significantly easier to implement with open struct representations, as in languages like Android Java, Objective C, or Go.</li>       <li>Removal of unknown fields.</li>       <li>Removal of extensions, which are instead replaced by a new standard type called Any.</li>       <li>Fix semantics for unknown enum values.</li>       <li>Addition of maps (back-ported to proto2)</li>       <li>Addition of a small set of standard types for representation of time, dynamic data, etc (back-ported to proto2)</li>       <li>A well-defined encoding in JSON as an alternative to binary proto encoding.</li>      </ol> <p>A new notion "syntax" is introduced to specify whether a .proto file<br /> uses proto2 or proto3:</p> <pre>  <code>// foo.proto  syntax = "proto3";  message Bar {...}  </code></pre> <p>If omitted, the protocol buffer compiler generates a warning and "proto2" is<br /> used as the default. This warning will be turned into an error in a future<br /> release.</p> <p>We recommend that new Protocol Buffers users use proto3. However, we do not<br /> generally recommend that existing users migrate from proto2 from proto3 due<br /> to API incompatibility, and we will continue to support proto2 for a long<br /> time.</p> <p>Other significant changes in proto3.</p> </li>     <li>Explicit "optional" keyword are disallowed in proto3 syntax, as fields are optional by default; required fields are no longer supported.</li>     <li>Removed non-zero default values and field presence logic for non-message fields. e.g. has_xxx() methods are removed; primitive fields set to default values (0 for numeric fields, empty for string/bytes fields) will be skipped during serialization.</li>     <li>Group fields are no longer supported in proto3 syntax.</li>     <li>Changed repeated primitive fields to use packed serialization by default in proto3 (implemented for C++, Java, Python in this release). The user can still disable packed serialization by setting packed to false for now.</li>     <li>Added well-known type protos (any.proto, empty.proto, timestamp.proto, duration.proto, etc.). Users can import and use these protos just like regular proto files. Additional runtime support are available for each language.</li>     <li> <p>Proto3 JSON is supported in several languages (fully supported in C++, Java,<br /> Python and C# partially supported in Ruby). The JSON spec is defined in the<br /> proto3 language guide:</p> <p><a href="/misc/goto?guid=4958992704454874100"></a></p> <p>We will publish a more detailed spec to define the exact behavior of<br /> proto3-conformant JSON serializers and parsers. Until then, do not rely<br /> on specific behaviors of the implementation if it’s not documented in<br /> the above spec.</p> </li>     <li>Proto3 enforces strict UTF-8 checking. Parsing will fail if a string field contains non UTF-8 data.</li>    </ul>    <h3>General</h3>    <ul>     <li>Introduced new language implementations (C#, JavaScript, Ruby, Objective-C) to proto3.</li>     <li> <p>Added support for map fields (implemented in both proto2 and proto3).<br /> Map fields can be declared using the following syntax:</p> <pre>  <code>message Foo {    map<string, string> values = 1;  }  </code></pre> <p>The data of a map field is stored in memory as an unordered map and<br /> can be accessed through generated accessors.</p> </li>     <li> <p>Added a "reserved" keyword in both proto2 and proto3 syntax. Users can use<br /> this keyword to declare reserved field numbers and names to prevent them<br /> from being reused by other fields in the same message.</p> <p>To reserve field numbers, add a reserved declaration in your message:</p> <pre>  <code>message TestMessage {    reserved 2, 15, 9 to 11, 3;  }  </code></pre> <p>This reserves field numbers 2, 3, 9, 10, 11 and 15. If a user uses any of<br /> these as field numbers, the protocol buffer compiler will report an error.</p> <p>Field names can also be reserved:</p> <pre>  <code>message TestMessage {    reserved "foo", "bar";  }  </code></pre> </li>     <li>Added a deterministic serialization API (currently available in C++). The deterministic serialization guarantees that given a binary, equal messages will be serialized to the same bytes. This allows applications like MapReduce to group equal messages based on the serialized bytes. The deterministic serialization is, however, NOT canonical across languages; it is also unstable across different builds with schema changes due to unknown fields. Users who need canonical serialization, e.g. persistent storage in a canonical form, fingerprinting, etc, should define their own canonicalization specification and implement the serializer using reflection APIs rather than relying on this API.</li>     <li>Added a new field option "json_name". By default proto field names are converted to "lowerCamelCase" in proto3 JSON format. This option can be used to override this behavior and specify a different JSON name for the field.</li>     <li>Added conformance tests to ensure implementations are following proto3 JSON specification.</li>    </ul>    <h3>C++</h3>    <ul>     <li> <p>Added arena allocation support (for both proto2 and proto3).</p> <p>Profiling shows memory allocation and deallocation constitutes a significant<br /> fraction of CPU-time spent in protobuf code and arena allocation is a<br /> technique introduced to reduce this cost. With arena allocation, new<br /> objects are allocated from a large piece of preallocated memory and<br /> deallocation of these objects is almost free. Early adoption shows 20% to<br /> 50% improvement in some Google binaries.</p> <p>To enable arena support, add the following option to your .proto file:</p> <pre>  <code>option cc_enable_arenas = true;  </code></pre> <p>The protocol buffer compiler will generate additional code to make the generated<br /> message classes work with arenas. This does not change the existing API<br /> of protobuf messages and does not affect wire format. Your existing code<br /> should continue to work after adding this option. In the future we will<br /> make this option enabled by default.</p> <p>To actually take advantage of arena allocation, you need to use the arena<br /> APIs when creating messages. A quick example of using the arena API:</p> <pre>  <code>{    google::protobuf::Arena arena;    // Allocate a protobuf message in the arena.    MyMessage* message = Arena::CreateMessage<MyMessage>(&arena);    // All submessages will be allocated in the same arena.    if (!message->ParseFromString(data)) {      // Deal with malformed input data.    }    // Must not delete the message here. It will be deleted automatically    // when the arena is destroyed.  }  </code></pre> <p>Currently arena allocation does not work with map fields. Enabling arenas in a .proto<br /> file containing map fields will result in compile errors in the generated<br /> code. This will be addressed in a future release.</p> </li>     <li> <p>Added runtime support for the Any type. To use Any in your proto file, first<br /> import the definition of Any:</p> <pre>  <code>// foo.proto  import "google/protobuf/any.proto";  message Foo {    google.protobuf.Any any_field = 1;  }  message Bar {    int32 value = 1;  }  </code></pre> <p>Then in C++ you can access the Any field using PackFrom()/UnpackTo()<br /> methods:</p> <pre>  <code>Foo foo;  Bar bar = ...;  foo.mutable_any_field()->PackFrom(bar);  ...  if (foo.any_field().IsType<Bar>()) {    foo.any_field().UnpackTo(&bar);    ...  }  </code></pre> </li>     <li> <p>In text format, the entries of a map field will be sorted by key.</p> </li>     <li> <p>Introduced new utility functions/classes in the google/protobuf/util<br /> directory:</p>      <ul>       <li>MessageDifferencer: compare two proto messages and report their differences.</li>       <li>JsonUtil: support converting protobuf binary format to/from JSON.</li>       <li>TimeUtil: utility functions to work with well-known types Timestamp and Duration.</li>       <li>FieldMaskUtil: utility functions to work with FieldMask.</li>      </ul> </li>     <li> <p>Introduced a deterministic serialization API in<br /> CodedOutputStream::SetSerializationDeterministic(bool). See the notes about<br /> deterministic serialization in the General section.</p> </li>    </ul>    <h3>Java</h3>    <ul>     <li> <p>Introduced a new util package that will be distributed as a separate<br /> artifact in maven. It contains:</p>      <ul>       <li>JsonFormat: convert proto messages to/from JSON.</li>       <li>Timestamps/Durations: utility functions to work with Timestamp and Duration.</li>       <li>FieldMaskUtil: utility functions to work with FieldMask.</li>      </ul> </li>     <li> <p>Introduced an ExperimentalApi annotation. Annotated APIs are experimental<br /> and are subject to change in a backward incompatible way in future releases.</p> </li>     <li>Introduced zero-copy serialization as an ExperimentalApi      <ul>       <li>Introduction of the <code>ByteOutput</code> interface. This is similar to <code>OutputStream</code> but provides semantics for lazy writing (i.e. no immediate copy required) of fields that are considered to be immutable.</li>       <li><code>ByteString</code> now supports writing to a <code>ByteOutput</code>, which will directly expose the internals of the <code>ByteString</code> (i.e. <code>byte[]</code> or <code>ByteBuffer</code>) to the <code>ByteOutput</code> without copying.</li>       <li><code>CodedOutputStream</code> now supports writing to a <code>ByteOutput</code>. <code>ByteString</code> instances that are too large to fit in the internal buffer will be (lazily) written to the <code>ByteOutput</code> directly.</li>       <li>This allows applications using large <code>ByteString</code> fields to avoid duplication of these fields entirely. Such an application can supply a <code>ByteOutput</code> that chains together the chunks received from <code>CodedOutputStream</code> before forwarding them onto the IO system.</li>      </ul> </li>     <li> <p>Other related changes to <code>CodedOutputStream</code></p>      <ul>       <li>Additional use of <code>sun.misc.Unsafe</code> where possible to perform fast access to <code>byte[]</code> and<code>ByteBuffer</code> values and avoiding unnecessary range checking.</li>       <li><code>ByteBuffer</code>-backed <code>CodedOutputStream</code> now writes directly to the <code>ByteBuffer</code> rather than to an intermediate array.</li>      </ul> </li>     <li> <p>Performance optimizations for String fields serialization.</p> </li>     <li>The static PARSER in each generated message is deprecated, and it will be removed in a future release. A static parser() getter is generated for each message type instead.</li>     <li>File option "java_generate_equals_and_hash" is now deprecated. equals() and hashCode() methods are generated by default.</li>    </ul>    <h3>Python</h3>    <ul>     <li> <p>Python has received several updates, most notably support for proto3<br /> semantics in any .proto file that declares syntax="proto3".<br /> Messages declared in proto3 files no longer represent field presence<br /> for scalar fields (number, enums, booleans, or strings). You can<br /> no longer call HasField() for such fields, and they are serialized<br /> based on whether they have a non-zero/empty/false value.</p> </li>     <li> <p>One other notable change is in the C++-accelerated implementation.<br /> Descriptor objects (which describe the protobuf schema and allow<br /> reflection over it) are no longer duplicated between the Python<br /> and C++ layers. The Python descriptors are now simple wrappers<br /> around the C++ descriptors. This change should significantly<br /> reduce the memory usage of programs that use a lot of message<br /> types.</p> </li>     <li> <p>Added map support.</p>      <ul>       <li>maps now have a dict-like interface (msg.map_field[key] = value)</li>       <li>existing code that modifies maps via the repeated field interface will need to be updated.</li>      </ul> </li>     <li> <p>Added proto3 JSON format utility. It includes support for all field types and a few well-known types.</p> </li>     <li>Added runtime support for Any, Timestamp, Duration and FieldMask.</li>     <li>"[ ]" is now accepted for repeated scalar fields in text format parser.</li>     <li>Removed legacy Python 2.5 support.</li>     <li>Moved to a single Python 2.x/3.x-compatible codebase</li>    </ul>    <h3>Ruby</h3>    <ul>     <li> <p>We have added proto3 support for Ruby via a native C/JRuby extension.</p> <p>For the moment we only support proto3. Proto2 support is planned, but not<br /> yet implemented. Proto3 JSON is supported, but the special JSON mappings<br /> for the well-known types are not yet implemented.</p> <p>The Ruby extension itself is included in the ruby/ directory, and details on<br /> building and installing the extension are in ruby/ The extension<br /> is also be published as a Ruby gem. Code generator support is included as<br /> part of <code>protoc</code> with the <code>--ruby_out</code> flag.</p> <p>The Ruby extension implements a user-friendly DSL to define message types<br /> (also generated by the code generator from <code>.proto</code> files). Once a message<br /> type is defined, the user may create instances of the message that behave in<br /> ways idiomatic to Ruby. For example:</p>      <ul>       <li>Message fields are present as ordinary Ruby properties (getter method <code>foo</code> and setter method<code>foo=</code>).</li>       <li>Repeated field elements are stored in a container that acts like a native Ruby array, and map elements are stored in a container that acts like a native Ruby hashmap.</li>       <li>The usual well-known methods, such as <code>#to_s</code>, <code>#dup</code>, and the like, are present.</li>      </ul> <p>Unlike several existing third-party Ruby extensions for protobuf, this<br /> extension is built on a "strongly-typed" philosophy: message fields and<br /> array/map containers will throw exceptions eagerly when values of the<br /> incorrect type are inserted.</p> <p>See ruby/ for details.</p> </li>    </ul>    <h3>Objective-C</h3>    <ul>     <li> <p>Objective-C includes a code generator and a native objective-c runtime<br /> library. By adding “--objc_out” to protoc, the code generator will generate<br /> a header(<em>.pbobjc.h) and an implementation file(</em>.pbobjc.m) for each proto<br /> file.</p> <p>In this first release, the generated interface provides: enums, messages,<br /> field support(single, repeated, map, oneof), proto2 and proto3 syntax<br /> support, parsing and serialization. It’s compatible with ARC and non-ARC<br /> usage. In addition, users can access it via the swift bridging header.</p> </li>    </ul>    <h3>C#</h3>    <ul>     <li>C# support is derived from the project at <a href="/misc/goto?guid=4958992704565971366"></a>, which is now in maintenance mode.</li>     <li>The primary differences between the previous project and the proto3 version are that message types are now mutable, and the codegen is integrated in protoc</li>     <li>There are two NuGet packages: Google.Protobuf (the support library) and Google.Protobuf.Tools (containing protoc)</li>     <li>Target platforms now .NET 4.5, selected portable subsets and .NET Core.</li>     <li>Null values are used to represent "no value" for message type fields, and for wrapper types such as Int32Value which map to C# nullable value types.</li>     <li>Proto3 semantics supported; proto2 files are prohibited for C# codegen.</li>     <li>Enum values are PascalCased, and if there's a prefix which matches the name of the enum, that is removed (so an enum <code>COLOR</code> with a value <code>COLOR_LIGHT_GRAY</code> would generate a value of just<code>LightGray</code>).</li>    </ul>    <h3>JavaScript</h3>    <ul>     <li>Added proto2/proto3 support for JavaScript. The runtime is written in pure JavaScript and works in browsers and in Node.js. To generate JavaScript code for your proto, invoke protoc with "--js_out". See js/ for more build instructions.</li>     <li>JavaScript has support for binary protobuf format, but not proto3 JSON. There is also no support for reflection, since the code size impacts from this are often not the right choice for the browser.</li>     <li>There is support for both CommonJS imports and Closure <code>goog.require()</code>.</li>    </ul>    <h3>Lite</h3>    <ul>     <li> <p>Supported Proto3 lite-runtime in Java for mobile platforms.<br /> A new "lite" generator parameter was introduced in the protoc for C++ for<br /> Proto3 syntax messages. Example usage:</p> <pre>  <code>./protoc --cpp_out=lite:$OUTPUT_PATH foo.proto  </code></pre> <p>The protoc will treat the current input and all the transitive dependencies<br /> as LITE. The same generator parameter must be used to generate the<br /> dependencies.</p> <p>In Proto3 syntax files, "optimized_for=LITE_RUNTIME" is no longer supported.</p> <p>For Java, --javalite_out code generator is supported as a separate compiler<br /> plugin in a separate branch.</p> </li>     <li> <p>Performance optimizations for Java Lite runtime on Android:<br /> - Reduced allocations<br /> - Reduced method overhead after ProGuarding<br /> - Reduced code size after ProGuarding</p> </li>     <li>Java Lite protos now implement deep equals/hashCode/toString</li>    </ul>    <h3>Compatibility Notice</h3>    <ul>     <li>v3.0.0 is the first API stable release of the v3.x series. We do not expect any future API breaking changes.</li>     <li>For C++, Java Lite and Objective-C, source level compatibility is guaranteed. Upgrading from v3.0.0 to newer minor version releases will be source compatible. For example, if your code compiles against protobuf v3.0.0, it will continue to compile after you upgrade protobuf library to v3.1.0.</li>     <li>For other languages, both source level compatibility and binary level compatibility are guaranteed. For example, if you have a Java binary built against protobuf v3.0.0. After switching the protobuf runtime binary to v3.1.0, your built binary should continue to work.</li>     <li>Compatibility is only guaranteed for documented API and documented behaviors. If you are using undocumented API (e.g., use anything in the C++ internal namespace), it can be broken by minor version releases in an undetermined manner.</li>    </ul>    <h3>Changes since v3.0.0-beta-4</h3>    <p><strong>Ruby</strong></p>    <ul>     <li>When you assign a string field <code>a.string_field = “X”</code>, we now call #encode(UTF-8) on the string and freeze the copy. This saves you from needing to ensure the string is already encoded as UTF-8. It also prevents you from mutating the string after it has been assigned (this is how we ensure it stays valid UTF-8).</li>     <li>The generated file for <code>foo.proto</code> is now <code>foo_pb.rb</code> instead of just <code>foo.rb</code>. This makes it easier to see which imports/requires are from protobuf generated code, and also prevents conflicts with any<code>foo.rb</code> file you might have written directly in Ruby. It is a backward-incompatible change: you will need to update all of your <code>require</code> statements.</li>     <li>For package names like <code>foo_bar</code>, we now translate this to the Ruby module <code>FooBar</code>. This is more idiomatic Ruby than what we used to do (<code>Foo_bar</code>).</li>    </ul>    <p><strong>JavaScript</strong></p>    <ul>     <li>Scalar fields like numbers and boolean now return defaults instead of <code>undefined</code> or <code>null</code> when they are unset. You can test for presence explicitly by calling <code>hasFoo()</code>, which we now generate for scalar fields in proto2.</li>    </ul>    <p><strong>Java Lite</strong></p>    <ul>     <li>Java Lite is now implemented as a separate plugin, maintained in the <code>javalite</code> branch. Both lite runtime and protoc artifacts will be available in Maven.</li>    </ul>    <p><strong>C#</strong></p>    <ul>     <li>Target platforms now .NET 4.5, selected portable subsets and .NET Core.</li>     <li>legacy_enum_values option is no longer supported.</li>    </ul>    <h2>下载</h2>    <ul>     <li><a href="/misc/goto?guid=4958992704665621669" rel="nofollow">3.89 MB<strong>protobuf-cpp-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992704759535333" rel="nofollow">4.81 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992704856770450" rel="nofollow">4.16 MB<strong>protobuf-csharp-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992704959471001" rel="nofollow">5.22 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992705069925200" rel="nofollow">4.31 MB<strong>protobuf-java-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992705163084650" rel="nofollow">5.43 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992705259101834" rel="nofollow">3.97 MB<strong>protobuf-js-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992705360736876" rel="nofollow">4.92 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992705453364870" rel="nofollow">489 KB<strong>protobuf-lite-3.0.1-sources.jar</strong></a></li>     <li><a href="/misc/goto?guid=4958992705549667481" rel="nofollow">4.28 MB<strong>protobuf-objectivec-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992705665651695" rel="nofollow">5.35 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992705753716601" rel="nofollow">4.14 MB<strong>protobuf-python-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992705856756640" rel="nofollow">5.15 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992705954158310" rel="nofollow">4.13 MB<strong>protobuf-ruby-3.0.0.tar.gz</strong></a></li>     <li><a href="/misc/goto?guid=4958992706044733839" rel="nofollow">5.09 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706152567186" rel="nofollow">1.2 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706240386496" rel="nofollow">1.24 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706338101993" rel="nofollow">1.36 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706444683863" rel="nofollow">1.31 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706541384554" rel="nofollow">1.11 MB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706635319039" rel="nofollow">804 KB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706736585290" rel="nofollow">823 KB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706840000335" rel="nofollow">822 KB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992706947995392" rel="nofollow">797 KB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992707040973262" rel="nofollow">748 KB<strong></strong></a></li>     <li><a href="/misc/goto?guid=4958992707142535712" rel="nofollow"><strong>Source code</strong> (zip)</a></li>     <li><a href="/misc/goto?guid=4958992707229352534" rel="nofollow"><strong>Source code</strong> (tar.gz)</a></li>    </ul>    <h2> </h2>