protobuf-net 3.2.45
protobuf-net
protobuf-net is a contract based serializer for .NET code, that happens to write data in the "protocol buffers" serialization format engineered by Google. The API, however, is very different to Google's, and follows typical .NET patterns (it is broadly comparable, in usage, to XmlSerializer
, DataContractSerializer
, etc). It should work for most .NET languages that write standard types and can use attributes.
Release Notes
Change history and pending changes are here.
Supported Runtimes
- .NET 6.0+ (.NET 5 etc will use .NET Standard 2.1)
- .NET Standard 2.0, 2.1
- .NET Framework 4.6.2+
Build tools
Build tools to help you use protobuf-net correctly are available via protobuf-net.BuildTools
Runtime Installation
All stable and some pre-release packages are available on NuGet. CI Builds are available via MyGet (feed URL: https://www.myget.org/F/protobuf-net/api/v3/index.json
).
You can use the following command in the Package Manager Console:
Install-Package protobuf-net
Package | NuGet Stable | NuGet Pre-release | Downloads | MyGet |
---|---|---|---|---|
protobuf-net |
Basic usage
1 First Decorate your classes
[ProtoContract]
class Person {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Name {get;set;}
[ProtoMember(3)]
public Address Address {get;set;}
}
[ProtoContract]
class Address {
[ProtoMember(1)]
public string Line1 {get;set;}
[ProtoMember(2)]
public string Line2 {get;set;}
}
Note that unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized (i.e. that it is a data contract).
2 Serialize your data
This writes a 32 byte file to "person.bin" :
var person = new Person {
Id = 12345, Name = "Fred",
Address = new Address {
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
using (var file = File.Create("person.bin")) {
Serializer.Serialize(file, person);
}
3 Deserialize your data
This reads the data back from "person.bin" :
Person newPerson;
using (var file = File.OpenRead("person.bin")) {
newPerson = Serializer.Deserialize<Person>(file);
}
Notes
Notes for Identifiers
- they must be positive integers (for best portability, they should be
<= 536870911
and not in the range19000-19999
) - they must be unique within a single type but the same numbers can be re-used in sub-types if inheritance is enabled
- the identifiers must not conflict with any inheritance identifiers (discussed later)
- lower numbers take less space - don't start at 100,000,000
- the identifier is important; you can change the member-name, or shift it between a property and a field, but changing the identifier changes the data
Advanced subjects
Inheritance
Inheritance must be explicitly declared, in a similar way that it must for XmlSerializer and DataContractSerializer. This is done via [ProtoInclude(...)] on each type with known sub-types:
[ProtoContract]
[ProtoInclude(7, typeof(SomeDerivedType))]
class SomeBaseType {...}
[ProtoContract]
class SomeDerivedType {...}
There is no special significance in the 7 above; it is an integer key, just like every [ProtoMember(...)]. It must be unique in terms of SomeBaseType (no other [ProtoInclude(...)] or [ProtoMember(...)] in SomeBaseType can use 7), but does not need to be unique globally.
.proto file
As an alternative to writing your classes and decorating them, You can generate your types from a .proto schema using protogen
;
the protogen
tool is available as a zip from that location, or as a "global tool" (multi-platform).
Alternative to attributes
In v2+, everything that can be done with attributes can also be configured at runtime via RuntimeTypeModel
. The Serializer.* methods are basically just shortcuts to RuntimeTypeModel.Default., so to manipulate the behaviour of Serializer., you must configure RuntimeTypeModel.Default.
Support
I try to be responsive to Stack Overflow questions in the protobuf-net
tag, issues logged on GitHub, email, etc. I don't currently offer a paid support channel. If I've helped you, feel free to buy me a coffee or see the "Sponsor" link at the top of the GitHub page.
Showing the top 20 packages that depend on protobuf-net.
Packages | Downloads |
---|---|
prometheus-net
.NET client for prometheus.io
|
8 |
prometheus-net
.NET client for prometheus.io
|
9 |
prometheus-net
.NET client for prometheus.io
|
10 |
prometheus-net
.NET client for prometheus.io
|
11 |
prometheus-net
.NET metrics exporter for the Prometheus monitoring and alerting system
|
8 |
prometheus-net
.NET metrics exporter for the Prometheus monitoring and alerting system
|
13 |
TimeZoneNames
Provides localized time zone names.
|
10 |
TimeZoneNames
Provides localized time zone names.
|
11 |
TimeZoneNames
Provides localized time zone names.
|
12 |
TimeZoneNames
Provides localized time zone names.
|
13 |
.NET Framework 4.6.2
- protobuf-net.Core (>= 3.2.45)
.NET 6.0
- protobuf-net.Core (>= 3.2.45)
.NET Standard 2.0
- protobuf-net.Core (>= 3.2.45)
- System.Reflection.Emit (>= 4.7.0)
- System.Reflection.Emit.Lightweight (>= 4.7.0)
.NET Standard 2.1
- protobuf-net.Core (>= 3.2.45)
Version | Downloads | Last updated |
---|---|---|
3.2.45 | 5 | 11/01/2024 |
3.2.30 | 19 | 05/23/2024 |
3.2.26 | 13 | 02/22/2024 |
3.2.16 | 8 | 05/23/2024 |
3.2.12 | 10 | 05/23/2024 |
3.2.8 | 8 | 05/23/2024 |
3.2.0 | 8 | 05/23/2024 |
3.1.33 | 17 | 05/23/2024 |
3.1.26 | 10 | 05/23/2024 |
3.1.25 | 10 | 05/23/2024 |
3.1.22 | 7 | 05/23/2024 |
3.1.17 | 12 | 05/23/2024 |
3.1.4 | 10 | 06/07/2022 |
3.1.0 | 9 | 06/07/2022 |
3.0.131 | 9 | 06/07/2022 |
3.0.101 | 9 | 04/30/2022 |
3.0.73 | 11 | 05/23/2024 |
3.0.72 | 6 | 11/13/2024 |
3.0.62 | 10 | 05/23/2024 |
3.0.52 | 9 | 05/23/2024 |
3.0.29 | 12 | 05/23/2024 |
3.0.27 | 9 | 05/23/2024 |
3.0.24 | 10 | 05/23/2024 |
3.0.21 | 6 | 05/23/2024 |
3.0.18 | 10 | 05/23/2024 |
3.0.13 | 6 | 05/23/2024 |
3.0.2 | 7 | 05/23/2024 |
3.0.0 | 6 | 05/23/2024 |
2.4.8 | 7 | 05/23/2024 |
2.4.7 | 10 | 06/07/2022 |
2.4.6 | 7 | 05/23/2024 |
2.4.1 | 8 | 06/07/2022 |
2.4.0 | 3 | 12/06/2024 |
2.3.17 | 15 | 05/23/2024 |
2.3.15 | 11 | 05/23/2024 |
2.3.14 | 8 | 05/23/2024 |
2.3.14-alpha1 | 4 | 11/27/2024 |
2.3.7 | 8 | 05/23/2024 |
2.3.4 | 7 | 05/23/2024 |
2.3.3 | 12 | 05/23/2024 |
2.3.2 | 10 | 06/07/2022 |
2.3.1 | 14 | 06/07/2022 |
2.3.0 | 11 | 05/23/2024 |
2.3.0-gamma | 3 | 12/11/2024 |
2.1.0 | 11 | 05/23/2024 |
2.1.0-alpha-5 | 5 | 11/14/2024 |
2.1.0-alpha-4 | 4 | 11/15/2024 |
2.1.0-alpha-3 | 5 | 11/14/2024 |
2.1.0-alpha-1 | 6 | 11/28/2024 |
2.0.0.668 | 4 | 11/28/2024 |
2.0.0.666 | 6 | 11/27/2024 |
2.0.0.664 | 4 | 12/06/2024 |
2.0.0.663 | 3 | 12/10/2024 |
2.0.0.640 | 5 | 11/12/2024 |
2.0.0.638 | 4 | 11/14/2024 |
2.0.0.621 | 11 | 06/07/2022 |
2.0.0.619 | 4 | 11/15/2024 |
2.0.0.614 | 4 | 11/15/2024 |
2.0.0.612 | 4 | 11/14/2024 |
2.0.0.611 | 4 | 12/02/2024 |
2.0.0.602 | 4 | 12/02/2024 |
2.0.0.601 | 4 | 11/14/2024 |
2.0.0.480 | 3 | 11/14/2024 |
1.0.0.280 | 4 | 11/14/2024 |