I have an MVC Web API Controller that exposes a method:
[HttpPost]
public JsonResult<CustomClass> Details(RequestClass request)
{
var encoding = Encoding.GetEncoding("iso-8859-1");
var settings = new Newtonsoft.Json.JsonSerializerSettings();
return Json(_InputService.CustomClassGet(request.Id), settings, encoding);
}
CustomClass
is a really complex class which contains several levels of objects of different classes. Objects from these classes are somehow built in another part of the code using a mapper that relies on Newtonsoft JSON.
I just got a request to modify my code to change some of CustomClass property names (along the whole tree). My first approach was to create another set of classes, so I could have one for receiving data and other for exposing data with a converter in the middle but there are so many classes in the structure and they are so complex that it would consume a lot of effort. Also, during the process of converting from input to output classes, it would require twice the memory to hold 2 copies of the same exact data.
My second approach was using JsonProperty(PropertyName ="X")
to change the resulting JSON BUT, as the input also relies in Newtonsoft JSON, I completely broke the input process.
My next approach was to create a custom serializer and user [JsonConverter(typeof(CustomCoverter))]
attribute BUT it changes the way CustomClass
is serialized everywhere and I can't change the way the rest of the API responds, just some specific methods.
So, the question is... does anyone imagine a way to change the way my CustomClass
is serialized just in certain methods?