|
| 1 | +// *********************************************************************** |
| 2 | +// Assembly : RabbitExpress.MsgPackSerializer |
| 3 | +// Author : ReneWindegger |
| 4 | +// Created : 05-11-2019 |
| 5 | +// |
| 6 | +// Last Modified By : ReneWindegger |
| 7 | +// Last Modified On : 05-11-2019 |
| 8 | +// *********************************************************************** |
| 9 | +// <copyright file="MsgPackSerializer.cs" company="Rene Windegger"> |
| 10 | +// Copyright (c) Rene Windegger. All rights reserved. |
| 11 | +// </copyright> |
| 12 | +// <summary> |
| 13 | +// This file is part of RabbitExpress. |
| 14 | +// |
| 15 | +// RabbitExpress is free software: you can redistribute it and/or modify |
| 16 | +// it under the terms of the GNU Lesser General Public License as published by |
| 17 | +// the Free Software Foundation, either version 3 of the License, or |
| 18 | +// (at your option) any later version. |
| 19 | +// |
| 20 | +// RabbitExpress is distributed in the hope that it will be useful, |
| 21 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | +// GNU Lesser General Public License for more details. |
| 24 | +// |
| 25 | +// You should have received a copy of the GNU Lesser General Public License |
| 26 | +// along with this RabbitExpress. If not, see <http://www.gnu.org/licenses/>. |
| 27 | +// </summary> |
| 28 | +// *********************************************************************** |
| 29 | +namespace RabbitExpress |
| 30 | +{ |
| 31 | + using MsgPack.Serialization; |
| 32 | + using System; |
| 33 | + using System.Collections.Concurrent; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Class MsgPackSerializer. |
| 37 | + /// Implements the <see cref="RabbitExpress.IExpressSerializer" /> |
| 38 | + /// </summary> |
| 39 | + /// <seealso cref="RabbitExpress.IExpressSerializer" /> |
| 40 | + public class MsgPackSerializer : IExpressSerializer |
| 41 | + { |
| 42 | + /// <summary> |
| 43 | + /// The serializer cache |
| 44 | + /// </summary> |
| 45 | + private static readonly ConcurrentDictionary<Type, MessagePackSerializer> SerializerCache = |
| 46 | + new ConcurrentDictionary<Type, MessagePackSerializer>(); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Deserializes the specified data. |
| 50 | + /// </summary> |
| 51 | + /// <typeparam name="TObject">The type of the object.</typeparam> |
| 52 | + /// <param name="data">The data.</param> |
| 53 | + /// <returns>TObject.</returns> |
| 54 | + public TObject Deserialize<TObject>(byte[] data) |
| 55 | + { |
| 56 | + var responseSerializer = SerializerCache.GetOrAdd(typeof(TObject), MessagePackSerializer.Get<TObject>()) as MessagePackSerializer<TObject>; |
| 57 | + if (responseSerializer != null) |
| 58 | + return responseSerializer.UnpackSingleObject(data); |
| 59 | + |
| 60 | + return default(TObject); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Serializes the specified value. |
| 65 | + /// </summary> |
| 66 | + /// <typeparam name="TObject">The type of the object.</typeparam> |
| 67 | + /// <param name="value">The value.</param> |
| 68 | + /// <returns>System.Byte[].</returns> |
| 69 | + public byte[] Serialize<TObject>(TObject value) |
| 70 | + { |
| 71 | + var responseSerializer = SerializerCache.GetOrAdd(typeof(TObject), MessagePackSerializer.Get<TObject>()) as MessagePackSerializer<TObject>; |
| 72 | + if (responseSerializer != null) |
| 73 | + return responseSerializer.PackSingleObject(value); |
| 74 | + |
| 75 | + return new byte[0]; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments