I am receiving JSON from a certain API has a dynamic property.
I have taken a a custom JsonConverter approach. Is there a simpler way to deal with this?
Here is the JSON returned:
{
"kind": "tm:ltm:pool:poolstats",
"generation": 1,
"selfLink": "https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/stats?ver=12.1.2",
"entries": {
"https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/~Common~test-mypoolname_https_main/stats": {
"nestedStats": {
"kind": "tm:ltm:pool:poolstats",
"selfLink": "https://localhost/mgmt/tm/ltm/pool/test-mypoolname_https_main/~Common~test-mypoolname_https_main/stats?ver=12.1.2"
}
}
}
}
The "entries": { "https://..." }
is that part that always changes, depending on what I request from the API.
Here is the class structure that will hold this information:
public partial class PoolStatistics
{
[JsonProperty("entries")]
public EntriesWrapper Entries { get; set; }
[JsonProperty("generation")]
public long Generation { get; set; }
[JsonProperty("kind")]
public string Kind { get; set; }
[JsonProperty("selfLink")]
public string SelfLink { get; set; }
[JsonConverter(typeof(PoolEntriesConverter))]
public partial class EntriesWrapper
{
public string Name { get; set; }
[JsonProperty("nestedStats")]
public NestedStats NestedStats { get; set; }
}
public partial class NestedStats
{
[JsonProperty("kind")]
public string Kind { get; set; }
[JsonProperty("selfLink")]
public string SelfLink { get; set; }
}
}
Override to Deserialize, via the following on the PoolEntriesConverter
:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
NestedStats nestedStats = (jo.First.First[NESTED_STATS]).ToObject<NestedStats>();
EntriesWrapper entries = new EntriesWrapper
{
NestedStats = nestedStats,
Name = jo.First.Path
};
return entries;
}
Overriden WriteJson (Serialization) - which is throwing an exception:
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
EntriesWrapper entries = (EntriesWrapper)value;
JObject jo = new JObject(
new JProperty(NESTED_STATS, entries.NestedStats),
new JProperty(entries.Name, entries.Name));
}
The error states:
System.ArgumentException: 'Could not determine JSON object type for
type F5IntegrationLib.Models.Pools.PoolStatistics+NestedStats.'
See Question&Answers more detail:
os