From c2c821804ca7219027d0c94833b3d6f05020cc22 Mon Sep 17 00:00:00 2001 From: AmanAgnihotri Date: Mon, 23 Aug 2021 09:21:49 +0530 Subject: [PATCH] Add an Example Using MessagePack --- docs/README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/docs/README.md b/docs/README.md index c84de98..4a267f9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 = "some@example.com" +}; +``` + +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(data); + + Console.WriteLine(decodedPayload.Subject); // 123456789 + Console.WriteLine(decodedPayload.Name); // Some Name + Console.WriteLine(decodedPayload.Email); // some@example.com + + Console.WriteLine(DateTimeOffset.FromUnixTimeSeconds(createTime)); +} +``` + --- ### License