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 up New issue

Some 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)/'

[{
'ID': 1,
'PublishedAt': '/Date(1330848000000-0800)/',
'Text': 'Best blog post ever',
'Title': 'Magical Title'
}, {
'ID': 2,
'PublishedAt': '/Date(1320825600000-0800)/',
'Text': 'No, really',
'Title': 'You rock'
}]

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

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Active2 months ago

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 Ovchinnikov
8,1104 gold badges31 silver badges55 bronze badges
Stay FoolishStay Foolish
1,5732 gold badges16 silver badges29 bronze badges

7 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).

Brian RogersBrian Rogers
85.8k22 gold badges212 silver badges218 bronze badges

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
1,5991 gold badge11 silver badges18 bronze badges
Keith HillKeith Hill
Json Date Serialization Mvc

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.

Saeb AminiSaeb Amini
14.8k5 gold badges54 silver badges59 bronze badges

Also available using one of the serializer settings overloads:

Or

Overloads taking a Type are also available.

MattMatt
5,6693 gold badges29 silver badges30 bronze badges

Build helper class and apply it to your property attribute

Helper class:

Your code use like this:

Json Serialization Format

Xin

Json Date Serialization Format

Xin
9,9214 gold badges43 silver badges51 bronze badges

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.

Muni ChittemMuni Chittem

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.

Json date serialization mvc code

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.

Antonio RodríguezAntonio Rodríguez

Not the answer you're looking for? Browse other questions tagged c#datetimeasp.net-web-apijson.net or ask your own question.