From 8da72984450f1c63af5d853a3399ab2a811e0e9c Mon Sep 17 00:00:00 2001 From: Albie Date: Wed, 8 Sep 2021 19:17:18 +0100 Subject: [PATCH] update default serializers to use new methods and add generic switch --- .../Serializer/SerializerResolverTests.cs | 1 + .../Serializers/ApiJsonSerializer.cs | 2 +- .../Serializers/ApiSerializer.cs | 8 ++++++ .../Serializers/ApiXmlSerializer.cs | 3 +-- .../Serializers/SerializerResolver.cs | 25 ++++++++++++++++--- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/DragonFruit.Common.Data.Tests/Serializer/SerializerResolverTests.cs b/DragonFruit.Common.Data.Tests/Serializer/SerializerResolverTests.cs index 997d130..ea65747 100644 --- a/DragonFruit.Common.Data.Tests/Serializer/SerializerResolverTests.cs +++ b/DragonFruit.Common.Data.Tests/Serializer/SerializerResolverTests.cs @@ -61,6 +61,7 @@ public class YetAnotherTestObject public class DummySerializer : ApiSerializer { public override string ContentType => "nothing"; + public override bool IsGeneric => true; public override HttpContent Serialize(T input) where T : class => throw new System.NotImplementedException(); public override T Deserialize(Stream input) where T : class => throw new System.NotImplementedException(); diff --git a/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs b/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs index de24548..a3575b0 100644 --- a/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs +++ b/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs @@ -85,7 +85,7 @@ public override HttpContent Serialize(T input) where T : class Serializer.Serialize(jsonWriter, input); } - return SerializerUtils.ProcessStream(this, stream); + return GetHttpContent(stream); } public override T Deserialize(Stream input) where T : class diff --git a/DragonFruit.Common.Data/Serializers/ApiSerializer.cs b/DragonFruit.Common.Data/Serializers/ApiSerializer.cs index 107397b..8aa0616 100644 --- a/DragonFruit.Common.Data/Serializers/ApiSerializer.cs +++ b/DragonFruit.Common.Data/Serializers/ApiSerializer.cs @@ -22,6 +22,14 @@ public abstract class ApiSerializer : ISerializer /// public abstract string ContentType { get; } + /// + /// Whether this is generic (meaning any class can be serialized to/from). + /// + /// + /// Setting this to false will throw an exception if the serializer is set as a default in a client. + /// + public virtual bool IsGeneric => true; + /// /// Gets or sets the encoding the uses /// diff --git a/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs b/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs index 80c864c..288da33 100644 --- a/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs +++ b/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs @@ -6,7 +6,6 @@ using System.Net.Http; using System.Text; using System.Xml.Serialization; -using DragonFruit.Common.Data.Utils; namespace DragonFruit.Common.Data.Serializers { @@ -34,7 +33,7 @@ public override HttpContent Serialize(T input) where T : class new XmlSerializer(typeof(T)).Serialize(writer, input); } - return SerializerUtils.ProcessStream(this, stream); + return GetHttpContent(stream); } public override T Deserialize(Stream input) where T : class diff --git a/DragonFruit.Common.Data/Serializers/SerializerResolver.cs b/DragonFruit.Common.Data/Serializers/SerializerResolver.cs index b6c58e1..6a3684a 100644 --- a/DragonFruit.Common.Data/Serializers/SerializerResolver.cs +++ b/DragonFruit.Common.Data/Serializers/SerializerResolver.cs @@ -14,6 +14,9 @@ public class SerializerResolver private static readonly Dictionary SerializerMap = new Dictionary(); private static readonly Dictionary DeserializerMap = new Dictionary(); + private ISerializer _default; + private readonly ConcurrentDictionary _serializerCache = new ConcurrentDictionary(); + /// /// Initialises a new instance of , providing a default /// @@ -63,8 +66,22 @@ public static void Unregister(DataDirection direction = DataDirection.All) } } - public ISerializer Default { get; set; } - private ConcurrentDictionary SerializerCache { get; } = new ConcurrentDictionary(); + /// + /// The default to use + /// + public ISerializer Default + { + get => _default; + set + { + if (value is ApiSerializer { IsGeneric: false }) + { + throw new ArgumentException("The provided serializer is non-generic."); + } + + _default = value; + } + } /// /// Resolves the for the type provided @@ -93,7 +110,7 @@ public ISerializer Resolve(Type objectType, DataDirection direction) // if the map has the type registered, check the type in cache if (mapping.TryGetValue(objectType, out var serializerType)) { - return SerializerCache.GetOrAdd(serializerType, _ => (ApiSerializer)Activator.CreateInstance(serializerType)); + return _serializerCache.GetOrAdd(serializerType, _ => (ApiSerializer)Activator.CreateInstance(serializerType)); } // use generic @@ -113,7 +130,7 @@ public void Configure(Action options) where TSerialize } else if (DeserializerMap.ContainsValue(typeof(TSerializer)) || SerializerMap.ContainsValue(typeof(TSerializer))) { - var serializer = SerializerCache.GetOrAdd(typeof(TSerializer), _ => Activator.CreateInstance()); + var serializer = _serializerCache.GetOrAdd(typeof(TSerializer), _ => Activator.CreateInstance()); options?.Invoke((TSerializer)serializer); } else