|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace Ydb.Sdk.Services.Topic; |
| 4 | + |
| 5 | +public interface IDeserializer<out TValue> |
| 6 | +{ |
| 7 | + /// <summary>Deserialize a message key or value.</summary> |
| 8 | + /// <param name="data">The data to deserialize.</param> |
| 9 | + /// <returns>The deserialized value.</returns> |
| 10 | + TValue Deserialize(byte[] data); |
| 11 | +} |
| 12 | + |
| 13 | +public static class Deserializers |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// String (UTF8 encoded) deserializer. |
| 17 | + /// </summary> |
| 18 | + public static IDeserializer<string> Utf8 = new Utf8Deserializer(); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// System.Int64 (big endian encoded, network byte ordered) deserializer. |
| 22 | + /// </summary> |
| 23 | + public static IDeserializer<long> Int64 = new Int64Deserializer(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// System.Int32 (big endian encoded, network byte ordered) deserializer. |
| 27 | + /// </summary> |
| 28 | + public static IDeserializer<int> Int32 = new Int32Deserializer(); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// System.Byte[] deserializer. |
| 32 | + /// </summary> |
| 33 | + /// <remarks> |
| 34 | + /// Byte ordering is original order. |
| 35 | + /// </remarks> |
| 36 | + public static IDeserializer<byte[]> ByteArray = new ByteArrayDeserializer(); |
| 37 | + |
| 38 | + internal static readonly Dictionary<System.Type, object> DefaultDeserializers = new() |
| 39 | + { |
| 40 | + { typeof(int), Int32 }, |
| 41 | + { typeof(long), Int64 }, |
| 42 | + { typeof(string), Utf8 }, |
| 43 | + { typeof(byte[]), ByteArray } |
| 44 | + }; |
| 45 | + |
| 46 | + private class Utf8Deserializer : IDeserializer<string> |
| 47 | + { |
| 48 | + public string Deserialize(byte[] data) |
| 49 | + { |
| 50 | + return Encoding.UTF8.GetString(data); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private class Int64Deserializer : IDeserializer<long> |
| 55 | + { |
| 56 | + public long Deserialize(byte[] data) |
| 57 | + { |
| 58 | + if (data.Length != 8) |
| 59 | + { |
| 60 | + throw new ArgumentException( |
| 61 | + $"Deserializer<Long> encountered data of length ${data.Length}. Expecting data length to be 8"); |
| 62 | + } |
| 63 | + |
| 64 | + return ((long)data[0] << 56) | ((long)data[1] << 48) | ((long)data[2] << 40) | ((long)data[3] << 32) | |
| 65 | + ((long)data[4] << 24) | ((long)data[5] << 16) | ((long)data[6] << 8) | data[7]; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private class Int32Deserializer : IDeserializer<int> |
| 70 | + { |
| 71 | + public int Deserialize(byte[] data) |
| 72 | + { |
| 73 | + if (data.Length != 4) |
| 74 | + { |
| 75 | + throw new ArgumentException( |
| 76 | + $"Deserializer<Int32> encountered data of length ${data.Length}. Expecting data length to be 4"); |
| 77 | + } |
| 78 | + |
| 79 | + return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private class ByteArrayDeserializer : IDeserializer<byte[]> |
| 84 | + { |
| 85 | + public byte[] Deserialize(byte[] data) |
| 86 | + { |
| 87 | + return data; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments