I have an API that talks to another API. The response model looks something like this:
public class AddressResponseModel
{
public string Id { get; set; }
public string SaveAs { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string Country { get; set; }
public string PostCode { get; set; }
}
So, I need to send this to another API. I don't really want to play around with the response in JavaScript, I would just like to send it as it is to my endpoint and let the server handle its factorization. So, I tried to do this:
public class AddressBindingModel
{
[Required]
[JsonProperty("address_1")]
public string Address1 { get; set; }
[JsonProperty("address_2")]
public string Address2 { get; set; }
[Required]
[JsonProperty("city")]
public string City { get; set; }
[Required]
[JsonProperty("county")]
public string County { get; set; }
[Required]
[JsonProperty("postcode")]
public string PostCode { get; set; }
[Required]
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("save_as")]
public string SaveAs { get; set; }
}
but the problem with that is that it expects the json to follow to same property format. How can I get it to expect my unmodified response model, but output the JSON with the underscores?
To clarify, I will post my model like this:
{
address1: '123',
address2: 'Some street',
city: 'London',
county: 'London',
country: 'GB',
saveAs: 'Home'
}
and my API will then send this to another API like this:
{
address_1: '123',
address_2: 'Some street',
city: 'London',
county: 'London',
country: 'GB',
save_as: 'Home'
}
If you want to use the same classes to automatically generate JSON with different property names, without having to write a custom
JsonConverter
for each one, you're going to need to create your own customContractResolver
, for instance:Json.NET provides
CamelCasePropertyNamesContractResolver
.This Answer has a prototype
PascalCaseToUnderscoreContractResolver
.If you have a deterministic way to map all .Net property names to the appropriate property names for a given usage context (post or get-from-api), you can create a contract resolver like one of the above.
If, however, there's no general rule for mapping .Net property names for JSON property names, and each context may require some per-property customization, you could create your own
ContractResolver
that applies in a specific named context, and your ownSystem.Attribute
that supplies a JSON context name and property name to use in this context. I.e.:Then your binding model would look something like:
To test:
To change the contract resolver for all results returned from Web API, see JSON and XML Serialization in ASP.NET Web API: Camel Casing. To use a custom contract resolver when returning results from a specific Web Api call, see Customize Json result in Web API.