Skip to content

Commit e99f0a0

Browse files
committed
Add quickstart docs
1 parent 70fff27 commit e99f0a0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@ By default classes generated from *.proto files don't work particularly well wit
99

1010
You may try to write a custom JsonConverter that will utilize JsonFormatter and JsonParser from Google.Protobuf package to do all the heavy lifting. Unfortunately, this solution falls short in terms of performance. This library provides high-performance and low-allocating types that serialize Google.Protobuf objects to JavaScript Object Notation (JSON) text and deserialize JSON text to Google.Protobuf objects.
1111

12+
## Quickstart
13+
14+
Add `Protobuf.System.Text.Json` NuGet package to your project using dotnet CLI:
15+
16+
```
17+
dotnet add package Protobuf.System.Text.Json
18+
```
19+
20+
To enable Protobuf support you need to register it on `JsonSerializerOptions`:
21+
22+
```csharp
23+
var jsonSerializerOptions = new JsonSerializerOptions();
24+
jsonSerializerOptions.PropertyNamingPolicy = new JsonLowerCaseNamingPolicy();
25+
jsonSerializerOptions.AddProtobufSupport();
26+
```
27+
28+
You need to pass the options each time you want to serialize:
29+
30+
```csharp
31+
var msg = new SimpleMessage
32+
{
33+
DoubleProperty = 2.5d
34+
};
35+
var payload = JsonSerializer.Serialize(msg, options: jsonSerializerOptions);
36+
```
37+
38+
or deserialize `Google.Protobuf` objects:
39+
40+
```csharp
41+
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(payload, jsonSerializerOptions);
42+
```
43+
1244
## Performance
1345

1446
``` ini

0 commit comments

Comments
 (0)