I am reading a json response from an API which contains information about images.
It is in a dictionary format like so:
images: {
low_resolution: {
url: "...",
width: 150,
height: 150
},
high_resolution: {
url: "...",
width: 450,
height: 450
}
}
I am deserializing the response into an object, and the images into a Dictionary property like so:
[DataContract()]
public class Post
{
...
[DataMember(Name = "images")]
public IDictionary<string, Media> Images { get; set; }
...
}
HttpResponseMessage response = await client.GetAsync(query);
if (response.IsSuccessStatusCode)
{
post = await response.Content.ReadAsAsync<Post>();
}
This all works fine so far, but I'd rather deserialize the image resolution information into an enumeration value.
So I created an enumeration ImageResolution
and changed the dictionary key from string
to ImageResolution
.
This also deserializes successfully, as long as the actual enumeration value equals the json string, but I want to change the enum values.
As per various other posts, I have tried the following:
[DataContract()]
public enum ImageResolution
{
[EnumMember(Value = "low_resolution")]
Low,
[EnumMember(Value = "high_resolution")]
High,
}
Also from searching I have also tried adding:
[JsonConverter(typeof(StringEnumConverter))]
But nothing so far has worked.
There is another property in the response which I am successfully deserializing to an enum and changing the enum value using the JsonConverter
attribute, but this is a straight forward property and not a dictionary key, so I am guessing that it being a dictionary key is causing some issues.
Is it possible to deserialize the json value to an enum dictionary key of different text value?