Files
DebugVisualizerExtension/NamedPipes/PipeSerializer.cs
T

35 lines
885 B
C#
Raw Normal View History

2026-04-08 08:20:33 +02:00
using PipeMethodCalls;
using System;
using System.IO;
using System.Text.Json;
namespace NamedPipes;
/// <summary>
/// Serializes pipe method call information with System.Text.Json.
/// </summary>
public class PipeSerializer : IPipeSerializer
{
2026-04-15 15:59:42 +02:00
private JsonSerializerOptions options = new()
{
IncludeFields = true
};
2026-04-08 08:20:33 +02:00
public object Deserialize(byte[] data, Type type)
{
2026-04-15 15:59:42 +02:00
var obj = JsonSerializer.Deserialize(data, type, options) ?? throw new InvalidDataException($"Can not deserialize to type: {type} ");
return obj;
2026-04-08 08:20:33 +02:00
}
public byte[] Serialize(object o)
{
2026-04-15 15:59:42 +02:00
using var memoryStream = new MemoryStream();
using var utf8JsonWriter = new Utf8JsonWriter(memoryStream);
JsonSerializer.Serialize(utf8JsonWriter, o, options);
var data = memoryStream.ToArray();
return data;
2026-04-08 08:20:33 +02:00
}
}