Skip to content

Commit

Permalink
Merge pull request #81 from dragonfruitnetwork/apiclient-cleanup
Browse files Browse the repository at this point in the history
update default serializers to use new methods
  • Loading branch information
aspriddell authored Sep 8, 2021
2 parents c93d15c + 8da7298 commit 591412b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T input) where T : class => throw new System.NotImplementedException();
public override T Deserialize<T>(Stream input) where T : class => throw new System.NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override HttpContent Serialize<T>(T input) where T : class
Serializer.Serialize(jsonWriter, input);
}

return SerializerUtils.ProcessStream(this, stream);
return GetHttpContent(stream);
}

public override T Deserialize<T>(Stream input) where T : class
Expand Down
8 changes: 8 additions & 0 deletions DragonFruit.Common.Data/Serializers/ApiSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public abstract class ApiSerializer : ISerializer
/// </summary>
public abstract string ContentType { get; }

/// <summary>
/// Whether this <see cref="ApiSerializer"/> is generic (meaning any class can be serialized to/from).
/// </summary>
/// <remarks>
/// Setting this to <c>false</c> will throw an exception if the serializer is set as a default in a client.
/// </remarks>
public virtual bool IsGeneric => true;

/// <summary>
/// Gets or sets the encoding the <see cref="ApiSerializer"/> uses
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -34,7 +33,7 @@ public override HttpContent Serialize<T>(T input) where T : class
new XmlSerializer(typeof(T)).Serialize(writer, input);
}

return SerializerUtils.ProcessStream(this, stream);
return GetHttpContent(stream);
}

public override T Deserialize<T>(Stream input) where T : class
Expand Down
25 changes: 21 additions & 4 deletions DragonFruit.Common.Data/Serializers/SerializerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class SerializerResolver
private static readonly Dictionary<Type, Type> SerializerMap = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, Type> DeserializerMap = new Dictionary<Type, Type>();

private ISerializer _default;
private readonly ConcurrentDictionary<Type, ApiSerializer> _serializerCache = new ConcurrentDictionary<Type, ApiSerializer>();

/// <summary>
/// Initialises a new instance of <see cref="SerializerResolver"/>, providing a default <see cref="ApiSerializer"/>
/// </summary>
Expand Down Expand Up @@ -63,8 +66,22 @@ public static void Unregister<T>(DataDirection direction = DataDirection.All)
}
}

public ISerializer Default { get; set; }
private ConcurrentDictionary<Type, ApiSerializer> SerializerCache { get; } = new ConcurrentDictionary<Type, ApiSerializer>();
/// <summary>
/// The default <see cref="ISerializer"/> to use
/// </summary>
public ISerializer Default
{
get => _default;
set
{
if (value is ApiSerializer { IsGeneric: false })
{
throw new ArgumentException("The provided serializer is non-generic.");
}

_default = value;
}
}

/// <summary>
/// Resolves the <see cref="ApiSerializer"/> for the type provided
Expand Down Expand Up @@ -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
Expand All @@ -113,7 +130,7 @@ public void Configure<TSerializer>(Action<TSerializer> options) where TSerialize
}
else if (DeserializerMap.ContainsValue(typeof(TSerializer)) || SerializerMap.ContainsValue(typeof(TSerializer)))
{
var serializer = SerializerCache.GetOrAdd(typeof(TSerializer), _ => Activator.CreateInstance<TSerializer>());
var serializer = _serializerCache.GetOrAdd(typeof(TSerializer), _ => Activator.CreateInstance<TSerializer>());
options?.Invoke((TSerializer)serializer);
}
else
Expand Down

0 comments on commit 591412b

Please sign in to comment.