Skip to content

Commit 7e7c3c3

Browse files
committed
custom json converter for request arguments in response to handle both arrays and single items
1 parent 0568eee commit 7e7c3c3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

FroniusSolarClient/Extensions/JsonExtension.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace FroniusSolarClient.Extensions
77
{
88
/// <summary>
99
/// Converts RequestArguments into string list with either single or multiple item(s)
10+
/// This is needed due to potentially providing multiple param values in an archive request (Channel), this means that the json will include an array of values
1011
/// </summary>
1112
/// <typeparam name="T"></typeparam>
1213
class RequestArgumentsConverter<T> : JsonConverter
@@ -18,12 +19,22 @@ public override bool CanConvert(Type objectType)
1819

1920
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2021
{
22+
var di = new Dictionary<string, List<string>>();
23+
2124
JToken token = JToken.Load(reader);
22-
if (token.Type == JTokenType.Array)
25+
26+
foreach (JProperty jprop in token.Children<JProperty>())
2327
{
24-
return token.ToObject<List<T>>();
28+
if (jprop.First.Type == JTokenType.Array)
29+
{
30+
di.Add(jprop.Name, ((JArray)jprop.Value).ToObject<List<string>>());
31+
}
32+
else
33+
{
34+
di.Add(jprop.Name, new List<string>() { (string)(JValue)jprop.Value });
35+
}
2536
}
26-
return new List<T> { token.ToObject<T>() };
37+
return di;
2738
}
2839

2940
public override bool CanWrite

0 commit comments

Comments
 (0)