I have a basic web service that returns the following object as JSON:
public class GetCustomerResponse
{
public Guid InteractionId { get; set; }
public string CustomerInfo { get; set; }
}
The CustomerInfo member is also JSON so the return back to the caller contains escaped JSON, which to be honest is fair enough and to be expected
However, I would like the CustomerInfo JSON to be "embedded" within the response without any escaping. Does anybody know if this is possible and if so how?
The reason for having to deal with CustomerInfo as a string is because this is generated by a sub system that is not based on objects so all I get back is a raw JSON string.
I realised that this could be solved by creating a CustomerInfo class within the service however I would prefer to avoid this as this would be a large class with many members, more importantly it would require the service to be updated if any changes were made.
EDIT : I have accepted the answer from Sergey Kudriavtsev as this works, however in the end I opted for a different solution.
I have added the json.net libraries to the solution and edited my GetCustomer class as follows:
public Newtonsoft.Json.Linq.JObject CustomerInfo { get; set; }
Then in code I have altered the service interface from:
GetCustomerResponse GetCustomer(int customerId)
to:
void GetCustomer(int customerId)
Then in the implementation of the interface I am doing the following
public void GetCustomer(int customerId)
{
var customerJson = ... code to get json string ...
var response = new GetCustomerResponse()
{
InteractionId = Guid.NewGuid(),
CustomerInfo = JObject.Parse(customerJson)
};
string json = JsonConvert.SerializeObject(response, Formatting.Indented);
HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
HttpContext.Current.Response.Write(json);
}