-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StringArrayMergeConverter to process inconsistencies in API respo…
…nse model
- Loading branch information
Showing
4 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/RobinTTY.NordigenApiClient/JsonConverters/StringArrayMergeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using RobinTTY.NordigenApiClient.Models.Responses; | ||
|
||
namespace RobinTTY.NordigenApiClient.JsonConverters; | ||
|
||
/// <summary> | ||
/// For some errors the GoCardless API returns arrays for Summary/Detail properties inside the <see cref="BasicResponse"/>. | ||
/// I've never actually seen them contain multiple values, but this converter merges them into one string so that the | ||
/// <see cref="BasicResponse"/> can stay as simple as possible. | ||
/// </summary> | ||
internal class StringArrayMergeConverter : JsonConverter<string> | ||
{ | ||
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Null: | ||
return null; | ||
case JsonTokenType.StartArray: | ||
var list = new List<string>(); | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) break; | ||
var listItem = JsonSerializer.Deserialize<string>(ref reader, options); | ||
if (listItem != null) list.Add(listItem); | ||
} | ||
|
||
return string.Join("; ", list); | ||
default: | ||
return JsonSerializer.Deserialize<string>(ref reader, options); | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => | ||
JsonSerializer.Serialize(writer, value, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters