0

I have an object that I'm serializing to string with JavaScriptSerializer. I've already registered a custom converter to omit null properties, but I also want to change how Date properties are rendered.

I want the date property to appear something like

{
   DateProperty = new Date(2019, 0, 31)
}

Instead of:

{
   DateProperty = "\/Date(1483776000000)\/";
}

Is it possible to customize how the JavaScriptSerializer serializes DateTime, or is there another serializer? I'm hoping to not hve to go to a 3rd party serializer.

Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • `{ DateProperty = new Date(2019, 0, 31) }` is not well-formed JSON. Try uploading it to https://jsonlint.com/ and you will get errors. `JavaScriptSerializer` cannot generate such badly-formed JSON. However, the [tag:json.net] library can parse and generate psuedo-JSON in this format, see e.g. [How to convert new Date(year, month, day) overload with JSON.Net](https://stackoverflow.com/a/31578239/3744182). That answer discusses how to deserialize JSON in such a format, but could be extended to serialization. Is that what you need? – dbc Jan 13 '19 at 19:54
  • Also, property names such as `DateProperty` must be quoted as required by the JSON standard https://json.org/. Do you really require them to be unquoted? – dbc Jan 13 '19 at 19:56

1 Answers1

0

That date format you want to change it's Microsoft format and JavaScriptSerializer convert object to JSON format. Please see this link for more details and hope can help you

Dates in JSON

pim3nt3l
  • 96
  • 5