protobuf-net
3.2.45
Prefix Reserved
dotnet add package protobuf-net --version 3.2.45
NuGet\Install-Package protobuf-net -Version 3.2.45
<PackageReference Include="protobuf-net" Version="3.2.45" />
paket add protobuf-net --version 3.2.45
#r "nuget: protobuf-net, 3.2.45"
// Install protobuf-net as a Cake Addin #addin nuget:?package=protobuf-net&version=3.2.45 // Install protobuf-net as a Cake Tool #tool nuget:?package=protobuf-net&version=3.2.45
<img src="https://protogen.marcgravell.com/images/protobuf-net.svg" alt="protobuf-net logo" width="45" height="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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- protobuf-net.Core (>= 3.2.45)
-
.NETStandard 2.0
- protobuf-net.Core (>= 3.2.45)
- System.Reflection.Emit (>= 4.7.0)
- System.Reflection.Emit.Lightweight (>= 4.7.0)
-
.NETStandard 2.1
- protobuf-net.Core (>= 3.2.45)
-
net6.0
- protobuf-net.Core (>= 3.2.45)
NuGet packages (875)
Showing the top 5 NuGet packages that depend on protobuf-net:
Package | Downloads |
---|---|
App.Metrics.Formatters.Prometheus
App Metrics Formatting, formatting metrics data to Prometheus formats. |
|
protobuf-net.Grpc
Package Description |
|
EventStore.Client
The legacy TCP client API for Event Store. Get the open source or commercial versions of Event Store server from https://eventstore.com/ |
|
ProtoBufJsonConverter
Convert a protobuf message to a JSON string or object (and back) using the proto definition file. |
|
Unipluss.Sign.Client
Client library to communication with Signere.no REST API. The library contains relevant model classes and a client class with methods to access all resources provided in the Signere.no API. The client class itself is implemented from an interface to make it easily testable. All explicit communication with the API is handled internally and translated to POCO objects. The client methods require certain fields from Signere.no user credentials in order to work properly. To create a user account and obtain these credentials, please visit signere.no . Also, check out the REST API documentation at Https://api.signere.no for further details about the resources that can be accessed through this library. |
GitHub repositories (175)
Showing the top 5 popular GitHub repositories that depend on protobuf-net:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
aspnetboilerplate/aspnetboilerplate
ASP.NET Boilerplate - Web Application Framework
|
|
dotnet/orleans
Cloud Native application framework for .NET
|
|
QuantConnect/Lean
Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
|
|
JosefNemec/Playnite
Video game library manager with support for wide range of 3rd party libraries and game emulation support, providing one unified interface for your games.
|
Version | Downloads | Last updated |
---|---|---|
3.2.45 | 99,599 | 10/23/2024 |
3.2.30 | 7,665,286 | 9/27/2023 |
3.2.26 | 3,299,619 | 5/25/2023 |
3.2.16 | 1,837,564 | 3/17/2023 |
3.2.12 | 415,042 | 3/4/2023 |
3.2.8 | 74,544 | 2/27/2023 |
3.2.0 | 138,122 | 2/20/2023 |
3.1.33 | 2,422,086 | 1/30/2023 |
3.1.26 | 1,435,706 | 12/16/2022 |
3.1.25 | 1,558,856 | 11/9/2022 |
3.1.22 | 1,272,389 | 9/21/2022 |
3.1.17 | 2,642,209 | 6/26/2022 |
3.1.4 | 1,989,145 | 5/10/2022 |
3.1.0 | 598,343 | 4/22/2022 |
3.0.131 | 59,726 | 4/22/2022 |
3.0.101 | 11,384,002 | 3/19/2021 |
3.0.73 | 3,799,028 | 12/2/2020 |
3.0.72 | 7,758 | 12/2/2020 |
3.0.62 | 282,050 | 11/13/2020 |
3.0.52 | 1,484,921 | 10/3/2020 |
3.0.29 | 2,662,388 | 7/8/2020 |
3.0.27 | 56,651 | 7/6/2020 |
3.0.24 | 16,724 | 7/6/2020 |
3.0.21 | 7,732 | 7/5/2020 |
3.0.18 | 5,872 | 7/5/2020 |
3.0.13 | 471,819 | 7/2/2020 |
3.0.2 | 195,205 | 6/24/2020 |
3.0.0 | 170,509 | 6/9/2020 |
2.4.8 | 3,490,670 | 1/9/2023 |
2.4.7 | 277,103 | 4/22/2022 |
2.4.6 | 12,045,217 | 1/28/2020 |
2.4.4 | 3,870,927 | 10/29/2019 |
2.4.1 | 646,108 | 10/9/2019 |
2.4.0 | 32,627,500 | 8/30/2018 |
2.3.17 | 682,320 | 8/3/2018 |
2.3.16 | 18,815 | 8/2/2018 |
2.3.15 | 134,384 | 7/30/2018 |
2.3.14 | 56,169 | 7/24/2018 |
2.3.14-alpha1 | 10,342 | 6/27/2018 |
2.3.13 | 929,411 | 6/19/2018 |
2.3.7 | 3,290,423 | 2/15/2018 |
2.3.6 | 526,724 | 2/1/2018 |
2.3.5 | 184,861 | 1/25/2018 |
2.3.4 | 3,270,485 | 1/19/2018 |
2.3.3 | 883,826 | 12/4/2017 |
2.3.2 | 4,146,750 | 8/4/2017 |
2.3.1 | 76,490 | 7/30/2017 |
2.3.0 | 201,026 | 7/13/2017 |
2.3.0-gamma | 2,701 | 7/12/2017 |
2.2.1 | 745,217 | 5/27/2017 |
2.1.0 | 6,793,123 | 7/8/2016 |
2.1.0-alpha-5 | 3,192 | 7/7/2016 |
2.1.0-alpha-4 | 2,965 | 7/6/2016 |
2.1.0-alpha-3 | 2,787 | 7/6/2016 |
2.1.0-alpha-1 | 49,412 | 11/22/2015 |
2.0.0.668 | 9,530,482 | 9/30/2013 |
2.0.0.666 | 26,740 | 9/9/2013 |
2.0.0.664 | 6,025 | 9/3/2013 |
2.0.0.663 | 3,671 | 9/2/2013 |
2.0.0.640 | 252,288 | 5/29/2013 |
2.0.0.638 | 6,339 | 5/28/2013 |
2.0.0.621 | 118,207 | 1/16/2013 |
2.0.0.619 | 4,729 | 1/11/2013 |
2.0.0.614 | 11,154 | 12/31/2012 |
2.0.0.612 | 12,325 | 12/10/2012 |
2.0.0.611 | 6,938 | 12/5/2012 |
2.0.0.602 | 39,786 | 11/13/2012 |
2.0.0.601 | 18,925 | 11/6/2012 |
2.0.0.480 | 1,303,264 | 12/11/2011 |
1.0.0.280 | 308,286 | 4/14/2011 |