1

There is incoming response like this;

{
   "response_code":23
}

there is no issue reading data,

and I am able to read this value with this object;

public class APIResponse
{
    [JsonProperty("response_code")]
    public HttpStatusCode ResponseCode { get; set; }
}

But when I need to return this object to client as JSON it should look like this;

{
   "responseCode":23
}

so basically I want to change property name for serialization only, how can I do that?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96

2 Answers2

1

If you are using Json.Net then you can use conditional serialization and a getter-only property.

public class APIResponse
{
    [JsonProperty("response_code")]
    public HttpStatusCode ResponseStatusCode { get; set; } //For deserialization
    
    public bool ShouldSerializeResponseStatusCode() => false;

    //[JsonProperty("ResponseCode")]
    public HttpStatusCode ResponseCode => ResponseStatusCode; //For serialization
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

Change [JsonProperty("response_code")] to [JsonProperty("responseCode")] of class APIResponse

May be two different classes for serialization and deserialization!

public class APIReadResponse
    {
        [JsonProperty("response_code")]
        public HttpStatusCode ResponseCode { get; set; }
    }

and

public class APIReturnResponse
    {
        [JsonProperty("responseCode")]
        public HttpStatusCode ResponseCode { get; set; }
    }
n-azad
  • 69
  • 5