File size: 1,776 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System.Text.Json;
using System.Text.Json.Nodes;
using VersOne.Epub.Test.Integration.CustomSerialization.TypeSerializers;
using VersOne.Epub.Test.Integration.JsonUtils;
namespace VersOne.Epub.Test.Integration.CustomSerialization.Context
{
internal class TestCaseSerializationContext : JsonSerializationContext
{
private readonly Dictionary<Type, Lazy<CustomTypeSerializationContext>> customTypes;
public TestCaseSerializationContext(TestEpubFile testEpubFile)
{
customTypes = CustomTypeSerializers.TypeSerializers.ToDictionary(typeSerializer => typeSerializer.Key,
typeSerializer => new Lazy<CustomTypeSerializationContext>(() => new(typeSerializer.Value, testEpubFile)));
}
public override JsonNode? SerializePropertyValue(Type type, string propertyName, object serializingObject)
{
if (!customTypes.TryGetValue(type, out Lazy<CustomTypeSerializationContext>? customTypeSerializationContext))
{
throw new ArgumentException($"There is no custom type serializer for type {type.Name}.");
}
return customTypeSerializationContext.Value.SerializePropertyValue(propertyName, serializingObject);
}
public override object? DeserializePropertyValue(Type type, string propertyName, JsonElement serializedValue)
{
if (!customTypes.TryGetValue(type, out Lazy<CustomTypeSerializationContext>? customTypeSerializationContext))
{
throw new ArgumentException($"There is no custom type deserializer for type {type.Name}.");
}
return customTypeSerializationContext.Value.DeserializePropertyValue(propertyName, serializedValue);
}
}
}
|