0

Deserializing a complex type in WebAPI is giving me serious grief. The data contains keys that are syntactically invalid in c# as property names. How can I translate the key names?

Relevant: Web API form-urlencoded binding to different property names

Community
  • 1
  • 1
Ablue
  • 785
  • 1
  • 7
  • 22

2 Answers2

1

You can use JSON.NET's JsonProperty to do the trick:

public class SomeModel {
    [JsonProperty("YourCustomName")]
    public string SomeProperty { get; set; }
}
Justin Helgerson
  • 23,852
  • 17
  • 92
  • 123
  • FormUrlEncodedMediaTypeFormatter will recognize that attribute? – Ablue Jun 25 '14 at 05:26
  • Looks like for the purposes of JSON deserialization and serialization it works but not for form urlencoded data. – Ablue Jun 25 '14 at 06:13
0

I wrote my own url encoded object deserializer that made use of the JsonProperty attribute. Then I used it with a custom modelbinder.

Ablue
  • 785
  • 1
  • 7
  • 22