Skip to content

Commit

Permalink
Finished converter and contract resolver. (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Dec 20, 2017
1 parent 7fdba57 commit a5efc63
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 1 addition & 2 deletions BunqSdk/Json/AnchorObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var list = new List<Type> {typeof(IAnchorObjectInterface)};
var jsonSerializer = JsonSerializer.CreateDefault(
new JsonSerializerSettings
{
ContractResolver = new BunqContractResolver(list),
ContractResolver = new BunqContractResolver(new List<Type> {typeof(IAnchorObjectInterface)}),
DateFormatString = FORMAT_DATE,
FloatParseHandling = FloatParseHandling.Decimal,
Formatting = Formatting.Indented,
Expand Down
28 changes: 26 additions & 2 deletions BunqSdk/Json/BunqContractResolver.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Bunq.Sdk.Context;
using Bunq.Sdk.Http;
using Bunq.Sdk.Model.Core;
using Bunq.Sdk.Model.Generated.Endpoint;
using Bunq.Sdk.Model.Generated.Object;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace Bunq.Sdk.Json
Expand All @@ -14,9 +17,9 @@ namespace Bunq.Sdk.Json
/// </summary>
public class BunqContractResolver : DefaultContractResolver
{
private readonly Dictionary<Type, JsonConverter> converterRegistry = new Dictionary<Type, JsonConverter>();
protected readonly Dictionary<Type, JsonConverter> converterRegistry = new Dictionary<Type, JsonConverter>();

public BunqContractResolver()
public BunqContractResolver(IReadOnlyCollection<Type> typesToExclude=null)
{
RegisterConverter(typeof(ApiEnvironmentType), new ApiEnvironmentTypeConverter());
RegisterConverter(typeof(Geolocation), new GeolocationConverter());
Expand All @@ -28,6 +31,20 @@ public BunqContractResolver()
RegisterConverter(typeof(double?), new NonIntegerNumberConverter());
RegisterConverter(typeof(float?), new NonIntegerNumberConverter());
RegisterConverter(typeof(Pagination), new PaginationConverter());
RegisterConverter(typeof(IAnchorObjectInterface), new AnchorObjectConverter());

if (typesToExclude == null)
{
return;
}

foreach (var type in typesToExclude)
{
if (converterRegistry.ContainsKey(type))
{
converterRegistry.Remove(type);
}
}
}

private void RegisterConverter(Type objectType, JsonConverter converter)
Expand All @@ -50,6 +67,13 @@ protected override JsonContract CreateContract(Type objectType)

private JsonConverter GetCustomConverterOrNull(Type objectType)
{
if (typeof(IAnchorObjectInterface).IsAssignableFrom(objectType))
{
return converterRegistry.ContainsKey(typeof(IAnchorObjectInterface))
? converterRegistry[typeof(IAnchorObjectInterface)]
: null;
}

return converterRegistry.ContainsKey(objectType) ? converterRegistry[objectType] : null;
}
}
Expand Down

0 comments on commit a5efc63

Please sign in to comment.