// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
namespace Doozy.Runtime.Common.Utils
{
/// Helper class for serialization and deserialization of types
public static class ByteArraySerializer
{
/// Serialize the given data type
/// Data to be serialized
/// Data type
/// The representation of the given data as a byte array
public static byte[] Serialize(this T data)
{
using var ms = new MemoryStream();
new BinaryFormatter().Serialize(ms, data);
return ms.ToArray();
}
/// Deserialize the given byte array
/// Byte array
/// Data type
/// The data deserialized from the byte array
public static T Deserialize(this byte[] byteArray)
{
using var ms = new MemoryStream(byteArray);
return (T)new BinaryFormatter().Deserialize(ms);
}
}
}