Json Date Serialization Mvc
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upSome times decorating the json convert attribute will not work,it will through exception saying that '2010-10-01' is valid date. Star plus serials with english subtitles desirippers. To avoid this types i removed json convert attribute on the property and mentioned in the deserilizedObject method like below.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Some times decorating the json convert attribute will not work,it will through exception saying that '2010-10-01' is valid date. To avoid this types i removed json convert attribute on the property and mentioned in the deserilizedObject method like below. Proper JSON serialization in MVC 4. This code, at the pipeline level, handles serialization the way I'd like. I would like to accomplish the same thing in MVC 4 - have any JSON returned from controller action methods to be serialized properly. With a little searching I found the following code to throw in the Global.asax application startup.
Already on GitHub? Sign in to your account
Comments
commented Feb 14, 2016
return new JsonResult(){Data={new { Data = MYmodel}}; client result : '/Date(1239018869048)/' [{ How can I parse this case ? |
commented Feb 14, 2016
Do you mean to parse the value into a date object? Since the value is a timestamp and Angular doesn't represent date in timestamp format, you can't parse it with this module. But you can do it manually: http://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format |
I am developing an API to expose some data using ASP.NET Web API.
In one of the API, the client wants us to expose the date in yyyy-MM-dd
format. I don't want to change the global settings (e.g. GlobalConfiguration.Configuration.Formatters.JsonFormatter
) for that since it is very specific to this client. And I do developing that in a solution for multiple clients.
One of the solution that I could think of is to create a custom JsonConverter
and then put that to the property I need to do the custom formatting
e.g.
Just wondering if there is some other easy way of doing that.
Vadim Ovchinnikov7 Answers
You are on the right track. Since you said you can't modify the global settings, then the next best thing is to apply the JsonConverter
attribute on an as-needed basis, as you suggested. It turns out Json.Net already has a built-in IsoDateTimeConverter
that lets you specify the date format. Unfortunately, you can't set the format via the JsonConverter
attribute, since the attribute's sole argument is a type. However, there is a simple solution: subclass the IsoDateTimeConverter
, then specify the date format in the constructor of the subclass. Apply the JsonConverter
attribute where needed, specifying your custom converter, and you're ready to go. Here is the entirety of the code needed:
If you don't mind having the time in there also, you don't even need to subclass the IsoDateTimeConverter. Its default date format is yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK
(as seen in the source code).
You could use this approach:
And use it this way:
The DateTimeFormat string uses the .NET format string syntax described here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
carlin.scott
It can also be done with an IsoDateTimeConverter
instance, without changing global formatting settings:
Json Date Serialization Mvc Download
This uses the JsonConvert.SerializeObject
overload that takes a params JsonConverter[]
argument.
Also available using one of the serializer settings overloads:
Or
Overloads taking a Type are also available.
MattMattBuild helper class and apply it to your property attribute
Helper class:
Your code use like this:
Json Serialization Format
XinJson Date Serialization Format
XinSome times decorating the json convert attribute will not work ,it will through exception saying that '2010-10-01' is valid date. To avoid this types i removed json convert attribute on the property and mentioned in the deserilizedObject method like below.
There is another solution I've been using. Just create a string property and use it for json. This property wil return date properly formatted.

This way you don't have to create extra classes. Also, it allows you to create diferent data formats. e.g, you can easily create another Property for Hour using the same DateTime.