Long ago, I set a coding standard for my app that all actions returning JSON would have their results put into a top-level wrapper object:
var result = {
success: false,
message: 'Something went wrong',
data: {} // or []
}
That has worked well, and provided me with good code standardization happiness.
Today, however, I realized that my server-side code assumes that it always gets to do the full serialization of what's getting returned. Now I would like to serialize one of these guys where the "data" payload is already a well-formed JSON string of its own.
This is the general pattern that had been working:
bool success = false;
string message = "Something went wrong";
object jsonData = "[{\"id\":\"0\",\"value\":\"1234\"}]"; // Broken
dynamic finalData = new { success = success, message = message, data = jsonData };
JsonResult output = new JsonResult
{
Data = finalData,
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
MaxJsonLength = int.MaxValue
};
return output;
Where it breaks is that the "data" element will be received as a string when it gets to the browser, and not as the proper JSON object (or array in the example above) it should be.
Is there some way I can decorate a property with an attribute that says "serialize as raw", or am I in the realm of writing a custom JSON serializer to make this work?