|
| 1 | +# Wodsoft Protobuf Wrapper |
| 2 | + |
| 3 | +## Contents |
| 4 | + |
| 5 | +- [About](#About) |
| 6 | +- [Requirements] |
| 7 | +- [Installation] |
| 8 | +- [Usage] |
| 9 | + - [Serialize] |
| 10 | + - [Deserialize] |
| 11 | + - [Field order] |
| 12 | + - [Get Protobuf Wrapper] |
| 13 | +- [Advanced] |
| 14 | + - [Supported property types and relationships] |
| 15 | + - [How it works] |
| 16 | + - [Performance] |
| 17 | +- [License] |
| 18 | + |
| 19 | +## About |
| 20 | + |
| 21 | +This library is a extension that help your use Google Protobuf without `.proto` files. |
| 22 | + |
| 23 | +In general `.proto` file will generate model that inherit `IMessage`. |
| 24 | +Protobuf using it for serialization. |
| 25 | + |
| 26 | +Sometimes we already have some models in .NET projects and doesn't need to share them for other languages. |
| 27 | +And we also need to serialize or deserialize them with Protobuf. |
| 28 | + |
| 29 | +In this case, this library will help you to use Protobuf for serialization. |
| 30 | + |
| 31 | +## Requirements |
| 32 | + |
| 33 | +Wodsoft.Protobuf.Extensions requires NETStandard2.0 or above. |
| 34 | + |
| 35 | +The library works on platform that allow dynamic complie. For example, **IOS is not allow to work.** |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +Library is available in four NuGet packages. |
| 40 | + |
| 41 | +```bash |
| 42 | +dotnet add package Wodsoft.Protobuf.Wrapper |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Serialize |
| 48 | + |
| 49 | +You can use method `Serialize` of `Wodsoft.Protobuf.Message`. |
| 50 | +You need a `System.IO.Stream` to store bytes. |
| 51 | + |
| 52 | +```csharp |
| 53 | +YourModel model = new (); |
| 54 | +MemoryStream stream = new MemoryStream(); |
| 55 | +Message.Serialize(stream, model); |
| 56 | +``` |
| 57 | + |
| 58 | +Also, there is a overloaded method. |
| 59 | +You can pass a `Google.Protobuf.CodedInputStream` from your context. |
| 60 | + |
| 61 | +```csharp |
| 62 | +YourModel model = new (); |
| 63 | +CodedInputStream input = ...; |
| 64 | +Message.Serialize(input, model); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +### Deserialize |
| 69 | + |
| 70 | +You can use method `Deserialize` of `Wodsoft.Protobuf.Message`. |
| 71 | +You need a `System.IO.Stream` where contains serialized bytes. |
| 72 | +Then it will return your model of generic argument `T`. |
| 73 | + |
| 74 | +```csharp |
| 75 | +Stream stream = ...; |
| 76 | +YourType model = Message.Deserialize<YourType>(stream); |
| 77 | +``` |
| 78 | + |
| 79 | +Also, there is a overloaded method too. |
| 80 | +You can pass a `Google.Protobuf.CodedOutputStream` from your context. |
| 81 | + |
| 82 | +```csharp |
| 83 | +CodedOutputStream output = ...; |
| 84 | +YourType model = Message.Deserialize<YourType>(output); |
| 85 | +``` |
| 86 | + |
| 87 | +### Field order |
| 88 | + |
| 89 | +Use `System.Runtime.Serialization.DataMemberAttribute` for your properties and set the `Order` property to attribute. |
| 90 | + |
| 91 | +**Important:** If there is a `DataMemberAttribute` on any property, it will only serialize or deserialize with properties which has `DataMemberAttribute`. |
| 92 | + |
| 93 | +### Get Protobuf Wrapper |
| 94 | + |
| 95 | +We can set a model value to `Message<>` variable directly. |
| 96 | + |
| 97 | +```csharp |
| 98 | +SimplyModel model; |
| 99 | +Message<SimplyModel> message = model; |
| 100 | +``` |
| 101 | + |
| 102 | +Then `message` can be used in Protobuf serialization directly. |
| 103 | + |
| 104 | + |
| 105 | +## Advanced |
| 106 | + |
| 107 | +### Supported property types and relationships |
| 108 | + |
| 109 | +| C# Types | Protobuf Types | Message Structure | |
| 110 | +| - | - | - | |
| 111 | +| bool(?) | bool | Varint | |
| 112 | +| sbyte(?) | int32 | Varint | |
| 113 | +| byte(?) | int32 | Varint | |
| 114 | +| short(?) | int32 | Varint | |
| 115 | +| ushort(?) | int32 | Varint | |
| 116 | +| int(?) | int32 | Varint | |
| 117 | +| long(?) | int64 | Varint | |
| 118 | +| uint(?) | uint32 | Varint | |
| 119 | +| ulong(?) | uint64 | Varint | |
| 120 | +| float(?) | float | Varint | |
| 121 | +| double(?) | double | Varint | |
| 122 | +| string | string | Length-delimited | |
| 123 | +| byte[] | ByteString | Length-delimited | |
| 124 | +| Guid(?) | ByteString | Length-delimited | |
| 125 | +| DateTime(?) | google.protobuf.Timestamp | Length-delimited | |
| 126 | +| DateTimeOffset(?) | google.protobuf.Timestamp | Length-delimited | |
| 127 | +| TimeSpan(?) | google.protobuf.Duration | Length-delimited | |
| 128 | +| IMessage | | Length-delimited | |
| 129 | +| T[] | RepeatedField\<T\> | Length-delimited | |
| 130 | +| ICollection\<T\> | RepeatedField\<T\> | Length-delimited | |
| 131 | +| Collection\<T\> | RepeatedField\<T\> | Length-delimited | |
| 132 | +| IList\<T\> | RepeatedField\<T\> | Length-delimited | |
| 133 | +| List\<T\> | RepeatedField\<T\> | Length-delimited | |
| 134 | +| IDictionary\<TKey, TValue\> | MapField\<TKey, TValue\> | Length-delimited | |
| 135 | +| Dictionary\<TKey, TValue\> | MapField\<TKey, TValue\> | Length-delimited | |
| 136 | + |
| 137 | +- **(?)** means work with `Nullable<>` types. |
| 138 | +- It's fine to use Protobuf object that inherit `Google.Protobuf.IMessage` as property type. |
| 139 | +- All `RepeatedField` and `MapField` object **CAN NOT CONTAINS** `null` values. |
| 140 | +- We support use `byte`, `sbyte`, `short` and `ushort` as property type. |
| 141 | +It will work as type `int` with serialization. |
| 142 | +Deserialize from other library serialized message, `int` field maybe lost its data. |
| 143 | + |
| 144 | +### How it works |
| 145 | + |
| 146 | +Mainly, Protobuf do serialization through `Google.Protobuf.IMessage` and `Google.Protobuf.IBufferMessage` interfaces. |
| 147 | + |
| 148 | +So we define a abstract `Wodsoft.Protobuf.Message` class. |
| 149 | +And define protected abstract `Read`, `Write`, `CalculateSize` methods. |
| 150 | +Explicit implement there interfaces and call the methods. |
| 151 | + |
| 152 | +Then define a abstract `Wodsoft.Protobuf.Message<T>` generic class. |
| 153 | +There is a property to reach origin model. And we can make some implicit operator here. |
| 154 | +```csharp |
| 155 | +public T Source { get; } |
| 156 | +``` |
| 157 | + |
| 158 | +Finally, we create dynamic class inherit `Message<T>` for those models when they need to do serialization in runtime. |
| 159 | +Emit codes for `Read`, `Write`, `CalculateSize`. |
| 160 | + |
| 161 | +### Performance |
| 162 | + |
| 163 | +It is nothing at performance cost in general types. |
| 164 | +But there is some performance cost when use some primitives types that Protobuf doesn't support. |
| 165 | + |
| 166 | +- **RECOMMEND DO NOT USE** `byte`, `sbyte`, `short` and `ushort` as property type. |
| 167 | +Specially use with generic types like `List` or `Dictionary` etc. |
| 168 | +It will cost more **performance** for convert them from `int` to purpose type. |
| 169 | +- **RECOMMEND USE** `RepeatedField<>`, `IList<>` or `ICollection<>` as collection property type. |
| 170 | +Use `RepeatedField<>` will be the **fastest performance**. |
| 171 | +- Use `IList<>` or `ICollection<>` should convert it to `RepeatedField<>` when serialize model. |
| 172 | +- Use `List<>` or `Collection<>` should convert it to `RepeatedField<>` when serialize model. |
| 173 | +And convert it back when deserialize model. |
| 174 | +- **RECOMMEND USE** `MapField<,>` or `IDictionary<,>` as dictionary property type. |
| 175 | +Use `MapField<,>` will be the **fastest performance**. |
| 176 | + |
| 177 | + |
| 178 | +## License |
| 179 | + |
| 180 | +This library is under the MIT License. |
0 commit comments