Search code examples
c#javascriptjsondatetimenancy

How to deal with NancyFX serialized DateTime?


I am sending a DateTime instance to the browser using NancyFX. The object that contains the date gets serialized as:

{ "foo": "\/Date(1378108800000)\/", "bar": "baz", … }

Now my question is how to deal with that serialized date value. Apparently, I can not hand it over to a Date constructor call in JavaScript. Of course I could use substring and / or a regular expression to strip out the number and hand that over to the Date constructor, but I guess that there must be a more intelligent (= standard) way.

Any hints?

PS: I have seen How to serialize DateTimeOffset as JSON in NancyFX?, but that does not answer my question.


Solution

  • There can't be a more intelligent way. You have two possibilities:

    (and surely there can't be a more standard way, because that formatting is used only by the .NET stock JSON serializers)

    I'll say that normally I would choose the first, but then you would have to recheck every generated JSON, so perhaps the second option is easier to implement. You could even replace the JSON.parse with a JSON.parse that does it.