Skip to content

Commit a5efc63

Browse files
author
Kevin Hellemun
committed
Finished converter and contract resolver. (#43)
1 parent 7fdba57 commit a5efc63

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

BunqSdk/Json/AnchorObjectConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2020

2121
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2222
{
23-
var list = new List<Type> {typeof(IAnchorObjectInterface)};
2423
var jsonSerializer = JsonSerializer.CreateDefault(
2524
new JsonSerializerSettings
2625
{
27-
ContractResolver = new BunqContractResolver(list),
26+
ContractResolver = new BunqContractResolver(new List<Type> {typeof(IAnchorObjectInterface)}),
2827
DateFormatString = FORMAT_DATE,
2928
FloatParseHandling = FloatParseHandling.Decimal,
3029
Formatting = Formatting.Indented,

BunqSdk/Json/BunqContractResolver.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using Bunq.Sdk.Context;
45
using Bunq.Sdk.Http;
56
using Bunq.Sdk.Model.Core;
7+
using Bunq.Sdk.Model.Generated.Endpoint;
68
using Bunq.Sdk.Model.Generated.Object;
79
using Newtonsoft.Json;
10+
using Newtonsoft.Json.Converters;
811
using Newtonsoft.Json.Serialization;
912

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

19-
public BunqContractResolver()
22+
public BunqContractResolver(IReadOnlyCollection<Type> typesToExclude=null)
2023
{
2124
RegisterConverter(typeof(ApiEnvironmentType), new ApiEnvironmentTypeConverter());
2225
RegisterConverter(typeof(Geolocation), new GeolocationConverter());
@@ -28,6 +31,20 @@ public BunqContractResolver()
2831
RegisterConverter(typeof(double?), new NonIntegerNumberConverter());
2932
RegisterConverter(typeof(float?), new NonIntegerNumberConverter());
3033
RegisterConverter(typeof(Pagination), new PaginationConverter());
34+
RegisterConverter(typeof(IAnchorObjectInterface), new AnchorObjectConverter());
35+
36+
if (typesToExclude == null)
37+
{
38+
return;
39+
}
40+
41+
foreach (var type in typesToExclude)
42+
{
43+
if (converterRegistry.ContainsKey(type))
44+
{
45+
converterRegistry.Remove(type);
46+
}
47+
}
3148
}
3249

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

5168
private JsonConverter GetCustomConverterOrNull(Type objectType)
5269
{
70+
if (typeof(IAnchorObjectInterface).IsAssignableFrom(objectType))
71+
{
72+
return converterRegistry.ContainsKey(typeof(IAnchorObjectInterface))
73+
? converterRegistry[typeof(IAnchorObjectInterface)]
74+
: null;
75+
}
76+
5377
return converterRegistry.ContainsKey(objectType) ? converterRegistry[objectType] : null;
5478
}
5579
}

0 commit comments

Comments
 (0)