useful Web API JSON.Net attributes - controlling how it serializes to JSON

Some useful Web API JSON.Net attributes:
  - controlling how it serializes to JSON:

//control the client-side name -> isolate client from C# renames
[JsonProperty(PropertyName = "DashboardType")]
public string MyAmazingRenamedProperty {get; set;}

//serialize an enum to a string NOT a value (helps JavaScript developers to live longer).
[JsonConverter(typeof (StringEnumConverter))]
public MyEnum Prop1 { get; set; }

//control how enum values appear in json:
public enum MySize
{
    [EnumMember(Value = "small")]
    Small,
    [EnumMember(Value = "medium")]
    Medium,
    [EnumMember(Value = "large")]
    Large,
    [EnumMember(Value = "fit")]
    Fit

}


Comments