The code below generates output like {"error_message":null,"status":"InvalidRequest"}where I would like to get it as{"error_message":null,"status":"INVALID_REQUEST"}
So simple, I was wondering how to honor an attribute PropertyNamewhen serializing with JSON.NET?
void Main()
{
var payload = new GooglePlacesPayload();
payload.Status = StatusCode.InvalidRequest;
JsonConvert.SerializeObject(payload).Dump();
}
public class GooglePlacesPayload
{
[JsonProperty(PropertyName = "error_message")]
public string ErrorMessage { get; set; }
[JsonProperty(PropertyName = "status")]
[JsonConverter(typeof(StringEnumConverter))]
public StatusCode Status { get; set; }
}
[Flags]
public enum StatusCode
{
None = 0,
[JsonProperty(PropertyName = "OK")]
Ok = 1,
[JsonProperty(PropertyName = "ZERO_RESULTS")]
ZeroResults = 2,
[JsonProperty(PropertyName = "OVER_QUERY_LIMIT")]
OverQueryLimit = 4,
[JsonProperty(PropertyName = "REQUEST_DENIED")]
RequestDenied = 8,
[JsonProperty(PropertyName = "INVALID_REQUEST")]
InvalidRequest = 16,
Positive = Ok | ZeroResults,
Negative = OverQueryLimit | RequestDenied | InvalidRequest
}
source
share