Skip to content

Commit

Permalink
Add an Example Using MessagePack
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanAgnihotri committed Aug 23, 2021
1 parent 0995162 commit c2c8218
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,65 @@ if (branca.TryDecode(token, out byte[] data, out uint createTime))
}
```

#### Using MessagePack

```c#
using MessagePack;
using System;
```

Suppose you have a payload structure as follows:

```c#
[MessagePackObject]
public sealed record Payload
{
[Key(0)]
public ulong Subject { get; init; }

[Key(1)]
public string Name { get; init; }

[Key(2)]
public string Email { get; init; }
}
```

Let's initialize the payload with some data:

```c#
Payload payload = new()
{
Subject = 123456789, Name = "Some Name", Email = "[email protected]"
};
```

We can tell `MessagePackSerializer` to serialize this payload to a byte array:

```c#
byte[] bytes = MessagePackSerializer.Serialize(payload);
```

And finally, with a `BrancaService` instance, we can encode and decode it as follows:

```c#
string token = branca.Encode(bytes);
// Jm79HC2CHnD1glDNBPucf02yCnyF70Kv2M7ELsNNqI1kJlX8ftrlV0IIYvuyHURpgrzXmvu4uuhmbmOkX3d0gBpxguUDXomxMgKeuCtpj
```

```c#
if (branca.TryDecode(token, out byte[] data, out uint createTime))
{
var decodedPayload = MessagePackSerializer.Deserialize<Payload>(data);

Console.WriteLine(decodedPayload.Subject); // 123456789
Console.WriteLine(decodedPayload.Name); // Some Name
Console.WriteLine(decodedPayload.Email); // [email protected]
Console.WriteLine(DateTimeOffset.FromUnixTimeSeconds(createTime));
}
```

---

### License
Expand Down

0 comments on commit c2c8218

Please sign in to comment.