JD2022-TU1/main/tools/framework/JD.Serialization/Serializers/BlobExtensions.cs

42 lines
No EOL
1.2 KiB
C#

using System.IO;
using JD.Remoting;
namespace JD.Serialization
{
/// <summary>
/// Adds serialization features to the blob.
/// </summary>
public static class BlobExtensions
{
/// <summary>
/// Pushes the object in its lua format.
/// </summary>
public static void PushObject<T>(this IBlob blob, T value)
where T : SpecialCustomTypeDescriptor
{
var serializer = new BinarySerializer();
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, value);
var data = stream.ToArray();
blob.Push(data);
}
}
/// <summary>
/// Pushes the object from its lua format.
/// </summary>
public static void ExtractObject<T>(this IBlob blob, T value)
where T : SpecialCustomTypeDescriptor
{
var serializer = new BinarySerializer();
var data = blob.ExtractBytes();
using (var stream = new MemoryStream(data))
{
serializer.Deserialize(stream, value);
}
}
}
}