0

I am writing a sample GET method to return data from on-premise database. I serialize it first to a string so that the dates will be converted to ISO format. But when I deserialize it, it goes back to Microsoft format. I don't know what I am doing wrong

Any help is appreciated.

public class SalesOrderHeaderController : Controller
{
    //
    // GET: /SalesOrderHeader/

    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult GetHeaders(string id)
    {

        var obj = DBHelpers.ExecuteQuery<SalesOrderHeader>(string.Format("select * from sales_order_hdr where sales_order_id = '{0}'", id));

            JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
                {
                    DateFormatHandling = DateFormatHandling.IsoDateFormat
                };

            string json = JsonConvert.SerializeObject(obj, microsoftDateFormatSettings);
            ResponseObject res = new ResponseObject()
                {
                    Code = "200",
                    Data = JsonConvert.DeserializeObject<List<SalesOrderHeader>>(json, microsoftDateFormatSettings)
                };

            return Json(res, JsonRequestBehavior.AllowGet);

    }

}

class ResponseObject
{
    public string Code { get; set; }
    public object Data { get; set; }
}
Joseph
  • 502
  • 4
  • 15
  • 2
    `But when I deserialize it, it goes back to Microsoft format` I don't understand what you mean by this - when you **de**serialize you're turning a string into an object, where a DateTime is a DateTime, there's no notion of different date formats in this representation. So could you please explain with an example? – Tom W May 11 '18 at 06:05
  • @TomW here is an example of the date that is returned by the response. /Date(1492311600000)/. Shouldn't be that way. – Joseph May 11 '18 at 06:17
  • You are serializing with specific settings, then deserializing to a list of SalesOrderHeader objects (with regular DateTime values), and then letting the response serialize it again to json (without specific settings) – Hans Kesting May 11 '18 at 07:35
  • by deserialising it again, you're losing anything you did when serialising it. You turned it back into a Date object, so next time you serialise _without_ your custom settings, it looks at the Date object, sees a Date object, and serialises it the normal way. It has no idea you previously serialised it differently, because you immediately turned it back again, and no information about that event is stored within the DateTime object. Is this an old version of MVC? Newer ones don't produce this date format. – ADyson May 11 '18 at 07:43

1 Answers1

0

I have implemented a custom ActionResult class, overriding the ExecuteResult method. Then on the method, made the conversions and wrote it in the HttpResponseBase object.

From: ASP.NET MVC JsonResult Date Format - Perishable Dave's answer

Joseph
  • 502
  • 4
  • 15
  • This means that your actual question was wrong to begin with. Json.NET uses the ISO format *by default* since version 4.5. It's now at version 11. As others have explained, you were *not* using JSON.NET at all - by passing the *actual* object instead of the JSON string to `Json()` you were using the obsolete JavaScriptSerializer – Panagiotis Kanavos May 11 '18 at 07:51