Reputation: 1158
I have a custom contract resolver to resolve property names in my model to those returned by a RESTful API call. This is an example API call:
{
"record_id": "f461dc88-2a3c-428c-ad92-1d03477667cf",
"company": "Palm Beach Photographics",
"company_info": {
"images": [
"https://picsum.photos/200/300?image=719",
"https://picsum.photos/200/300?image=653",
"https://picsum.photos/200/300?image=965"
],
"industries": [
"Real Estate",
"Photo/Film",
"Utilities/Energy/Infrastructure",
"Emergency Services"
],
"location_state": "FL",
"overview": "Per fusce luctus diam hac dictum posuere non interdum conubia eleifend, fusce suscipit purus aliquam porttitor amet fames nostra sapien potenti fusce, quisque felis quisque ante fringilla nulla rutrum porta at inceptos mi per mattis rhoncus id."
}
}
and my Models are as follows:
public class Company
{
public Guid Id { get; set; }
public string Name { get; set; }
public CompanyInfo Info { get; set; }
}
public class CompanyInfo
{
public IEnumerable<Uri> Images { get; set; }
public string State { get; set; }
public IEnumerable<string> Industries { get; set; }
public string Overview { get; set; }
}
public class CustomContractResolver : DefaultContractResolver
{
private Dictionary<string, string> PropertyMappings { get; }
public CustomContractResolver(Dictionary<string, string> propertyMappings)
{
PropertyMappings = propertyMappings;
}
protected override string ResolvePropertyName(string propertyName)
{
var resolved = PropertyMappings.TryGetValue(propertyName, out var resolvedName);
return (resolved) ? resolvedName : base.ResolvePropertyName(propertyName);
}
}
Without polluting my model with JsonProperty and other tags, how can I get the nested company_info
to bind correctly? When I am deserializing the object using JsonConvert, I pass in property mappings as such:
private readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
{
{"Id", "record_id"},
{"Name", "company"},
{"Info.Images", "company_info.images"},
{"Info.Industries", "company_info.industries"},
{"Info.State", "company_info.location_state"},
{"Info.Overview", "company_info.overview"}
};
The Id and Name correctly bind, but none of the Info maps.
Upvotes: 3
Views: 1191
Reputation: 617
If you don't want to change any property name to match with Json property and don't wanna use JsonProperty you can just fix your wrong property mapping to this
private static readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
{
{"Id", "record_id"},
{"Name", "company"},
{"Info", "company_info"},
{"State", "location_state"}
};
Just tell Deserializer to map name-to-name no need to put any nested object on it hope it can solve your problem
Upvotes: 2
Reputation: 1836
The example you asked for, formatted here:
public class Company
{
public Guid Id { get; set; }
public string Name { get; set; }
public CompanyInfo Info { get; set; }
public IEnumerable<Uri> Info_Images {
get {
return Info.Images;
}
set {
if (Info == null) {
Info = new CompanyInfo();
}
Info.Images = value
}
}
// etc., with the other properties.
}
public class CompanyInfo
{
public IEnumerable<Uri> Images { get; set; }
public string State { get; set; }
public IEnumerable<string> Industries { get; set; }
public string Overview { get; set; }
}
And the mapping could go as:
private readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
{
{"Id", "record_id"},
{"Name", "company"},
{"Info_Images", "company_info.images"},
{"Info_Industries", "company_info.industries"},
{"Info_State", "company_info.location_state"},
{"Info_Overview", "company_info.overview"}
};
Upvotes: 1