Wednesday, January 06, 2016

WebAPI: default formatting, better dates and KnockoutJS friendliness

The notorious problem with JavaScript dates reminds me the technical relationship between space shuttle and a horse: once upon a time IT was ruled by Unix people who liked incomprehensible dates (probably they were already programming towards the world domination - ICBM navigation, global stock trading and private jet scheduling).
Scott Hanselman dedicated a post to this problem back in March 2012 and sworn that natural JSON dates will be no more. David Warp seconded. The change seemed to make it in the release available as of August 2012 - awesome, but I wanted to keep playing with the nice formatters mentioned in the said posts. Unfortunately I couldn't even compile them and guessed that by the time I reached for the installer the code behind the package has progressed significantly. Figuring out which assemblies to NuGet into solution or which references to add is a bigger pain (and less fun) than building more code - after all, who would enjoy copying somebody's code instead of doing own research?
The idea was to change the default serialization behavior for null properties to prevent my KnockoutJS from failing. Secondly I wanted to try and change default formatting for HTTP GET implementation, such as when API is called from the browser (not that it is really important with the content negotiation). And third - change the date formats to something more predictable and culture neutral.
The first compilation kind of worked (and it seemed to work faster in the browser):

>
Note the Unit property in the second address: in XML property is serialized with "nil" attribute, while the JSON implementation is actually missing it altogether - which would blow up the KnockoutJS bindings.


© 2008-2013 Michael Goldobin. All rights reserved