I am trying to write C# types and deserialization code for this WEB-API method, using Newtonsoft JSON libs for C#: https://docs.kraken.com/rest/#operation/getTickerInformation
Example JSON looks like this:
{
"error": [],
"result": {
"XXBTZUSD": {
"a": [
"52609.60000",
"1",
"1.000"
],
"b": [
"52609.50000",
"1",
"1.000"
],
"c": [
"52641.10000",
"0.00080000"
],
"v": [
"1920.83610601",
"7954.00219674"
],
"p": [
"52389.94668",
"54022.90683"
],
"t": [
23329,
80463
],
"l": [
"51513.90000",
"51513.90000"
],
"h": [
"53219.90000",
"57200.00000"
],
"o": "52280.40000"
}
}
}
For other API methods I am using direct mapping to my types with code like this:
public static Ticker GetTicker(string pair)
{
try
{
return Deserialize<Ticker>GetJObject($"https://api.kraken.com/0/public/Ticker?pair={pair}");
}
catch
{
throw;
}
}
private static T Deserialize<T>(JObject jObject)
{
try
{
var err = jObject["error"];
if (err.HasValues)
{
throw new Exception("Kraken Error" + String.Join(',',err.ToObject<string[]>()));
}
return jObject["result"].ToObject<T>();
}
catch
{
throw;
}
}
But in this case, the JSON returns nameless values in arbitrary arrays - and also Ticker
is nested under another element named by pair
. Worse, the arrays use mixed types. So I'm resorting (so far) to something like this:
public class Ticker
{
[JsonProperty("a")]
public float[] ask { get; set; }
[JsonProperty("b")]
public float[] bid { get; set; }
[JsonProperty("c")]
public float[] close { get; set; }
[JsonProperty("v")]
public float[] volume { get; set; }
[JsonProperty("p")]
public float[] volumeWeightedPrice { get; set; }
[JsonProperty("t")]
public int[] trades { get; set; }
[JsonProperty("l")]
public float[] low { get; set; }
[JsonProperty("h")]
public float[] high { get; set; }
[JsonProperty("o")]
public float opening { get; set; }
}
}
I am new to NewtonSoft's JSON libraries and wondering if I can get this to work purely on the Ticker
class declaration, presumably using annotations? Or if I have to delve into custom deserialization code... and what that might look like?