1

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.

Community
  • 1
  • 1
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • possible duplicate of [How to parse JSON to receive a Date object in JavaScript?](http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript) – xanatos Sep 03 '13 at 14:11
  • See http://stackoverflow.com/a/14509447/613130 – xanatos Sep 03 '13 at 14:11
  • It's not a duplicate, as I have explicitly asked about a way *without* a regex, and the provided answer uses a regex ;-) – Golo Roden Sep 03 '13 at 14:13
  • The point is that the linked response isn't using a regex to modify the JSON, it's using an extension of JSON.parse to parse non-recognized types. There can't be a "more standard way" because that format for Date is specific of .NET "stock" JSON serializer. The other possible response would be "use JSON.NET" – xanatos Sep 03 '13 at 14:15
  • Yes, but the extension internally uses a regex ;-) – Golo Roden Sep 03 '13 at 14:15
  • You could use a substring... – user7116 Sep 03 '13 at 14:16
  • To quote from my question: *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.* – Golo Roden Sep 03 '13 at 14:16
  • 1
    @GoloRoden We have to read your question with a 10 foot pole... You didn't even use the [Javascript] tag for a question (I know because I added it) that you seem to want to resolve client-side. Good answers need good questions. – xanatos Sep 03 '13 at 14:26
  • I didn't mention that I want to resolve it client-side. I just asked whether there is a better solution than regex / substring. Whether it's client- or server-side doesn't matter. But, never mind ;-) – Golo Roden Sep 03 '13 at 14:28
  • 1
    @GoloRoden You asked `Now my question is how to deal with that serialized date value.`... All your examples where of Javascript... 1+1 == 2. Technically even a response like "use JSON.NET" would be against your request. You can always modify your question to make it more clear. – xanatos Sep 03 '13 at 14:36

1 Answers1

2

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.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280