File size: 1,823 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 38 39 40 41 42 43 44 45 |
using System.Collections;
using System.Text.Json.Nodes;
namespace VersOne.Epub.Test.Integration.JsonUtils.Serializers
{
internal class DictionarySerializer : TypeSerializer
{
private readonly Lazy<TypeSerializer> declaredValueTypeSerializer;
public DictionarySerializer(Type dictionaryType, TypeSerializerCollection typeSerializerCollection)
{
Type declaredValueType = dictionaryType.IsGenericType ?
dictionaryType.GetGenericArguments()[1] : throw new ArgumentException($"{dictionaryType.Name} is not a generic Dictionary<K,V> type.");
declaredValueTypeSerializer = new Lazy<TypeSerializer>(() => typeSerializerCollection.GetSerializer(declaredValueType));
}
public override JsonNode? Serialize(object? value, JsonSerializationContext jsonSerializationContext)
{
if (value is not IDictionary dictionary)
{
return null;
}
JsonObject dictionaryObject = new();
foreach (DictionaryEntry dictionaryItem in dictionary)
{
if (dictionaryItem.Key is not string key)
{
throw new ArgumentException($"Expected string dictionary key type but got {dictionaryItem.Key.GetType().Name}.");
}
JsonNode? serializedValue;
if (dictionaryItem.Value == null)
{
serializedValue = null;
}
else
{
serializedValue = declaredValueTypeSerializer.Value.Serialize(dictionaryItem.Value, jsonSerializationContext);
}
dictionaryObject.Add(key, serializedValue);
}
return dictionaryObject;
}
}
}
|