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?

2 Answers 2

3

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 } 
Sign up to request clarification or add additional context in comments.

Comments

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; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.