-
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.
Merge pull request #9 from whippet71/main
Fixes #8 - Allow CurrencyExchange section to be either a single item or an array, and add element for InstructedAmount
- Loading branch information
Showing
5 changed files
with
91 additions
and
9 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
42 changes: 42 additions & 0 deletions
42
src/RobinTTY.NordigenApiClient/JsonConverters/SingleOrArrayConverter.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,42 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace RobinTTY.NordigenApiClient.JsonConverters; | ||
|
||
internal class SingleOrArrayConverter<TCollection, TItem> : JsonConverter<TCollection> | ||
where TCollection : class, ICollection<TItem>, new() | ||
{ | ||
public override TCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.Null: | ||
return null; | ||
case JsonTokenType.StartArray: | ||
var list = new TCollection(); | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) break; | ||
var listItem = JsonSerializer.Deserialize<TItem>(ref reader, options); | ||
if (listItem != null) list.Add(listItem); | ||
} | ||
return list; | ||
default: | ||
var item = JsonSerializer.Deserialize<TItem>(ref reader, options); | ||
return item != null ? new TCollection {item} : null; | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options) | ||
{ | ||
if (value.Count == 1) | ||
JsonSerializer.Serialize(writer, value.First(), options); | ||
else | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in value) | ||
JsonSerializer.Serialize(writer, item, options); | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -30,4 +30,4 @@ public AmountCurrencyPair(decimal amount, string currency) | |
Amount = amount; | ||
Currency = currency; | ||
} | ||
} | ||
} |
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