Cannot represent NaN properly using JSON
Gosh, this took me almost 2 hours to figure out… gotta write it down, so I’ll never forget it again!
I am working on a project where I communicate with a WCF service using jQuery. The service methods return state objects serialized to JSON which I expect jQuery to correctly parse into JavaScript objects.
One of my state objects contained a property of type double? which were sometimes initialized with double.NaN as the value.
Let’s assume that the state object is called d and the property in question we call Value, so server-side Value was initialized like this:
d.Value = double.NaN;
And when serialized to JSON by the WCF service it turned into a string looking like this:
"{ \"d\": NaN }"
Perhaps I am the only person in the whole world who thought this would be okay, since JavaScript does have the concept of NaN, but JavaScript is not able to parse the JSON string above.
I tried to write a small script to clarify this a bit:
<html>
<head>
<script type="text/javascript">
document.writeln(NaN);
document.writeln(Number.NaN);
document.writeln(NaN.toString());
document.writeln(Number.NaN.toString());
var jsob = { d: Number.NaN };
document.writeln(JSON.stringify(jsob));
var json = '{ "d": NaN }';
document.writeln(JSON.parse(json));
</script>
</head>
</html>
The page above yields the following output:
NaN NaN NaN NaN {"d":null}
And in the console:
JSON.parse: unexpected character
document.writeln(JSON.parse(json));
I found this a bit surprising, however in my case I was able to fix it fairly easy by not using double.NaN, but I still don’t understand why, perhaps it’s simply because it’s not part of the JSON specification.
Dad draw me a superman!
Oliver is moving on from YouTube on his iPad, now we are drawing together, well I do most of the drawing and Oliver decides what the motive should be.
We used Brushes to draw the Superman below, it’s a really cool app which let you replay the drawing process.
Glowing shuttlecocks?
I’m on my way home by taxi from Pham Ngu Lao street where I’ve been all day working and receiving signatures from former employees.
Driving along the park which is squeezed in between the streets running on both sides I see people playing badminton, it’s around 7:30 PM and already dark, I can imagine it must be difficult to see the shuttlecock sometimes.
So it occurred to me, why not create a device to mount on shuttlecocks which would light up on impact with the racket, or perhaps even build shuttlecocks with such a device build in so the weight were matched correctly? Does it already exist?
One could imagine that the feathers should be exchangeable to reuse the probably more expensive hard part containing the glowing device.
That way it would be easier to see the ball when playing badminton, outside in the park, at 7:30 PM or later, when it’s dark in many equatorial countries around the globe.
A possible alternative to JS Bin
Today while rumbling around with some JavaScript and HTML I accidentally stumbled upon another JavaScript lab similar to JS Bin which I mentioned in my previous post. It’s called JSFIDDLE and it looks very promising, and it has more features than JS Bin.
You can type in your CSS and HTML in separate windows for starters, and one really cool feature is the TidyUp which helps you reformat unreadable code into readable code whether it’s HTML or JavaScript, really nice.
JSFIDDLE uses JSLint to validate JavaScript and you can include your favorite JavaScript API to code up against, although it doesn’t seem like you can select more than one at a time, and it’s not shown in your code which framework you are using, however it’s visible in the sidebar on the left which also contains a number of examples by the way.
So far I’ve tested it in Google Chrome and Internet Explorer 8, and it works a lot better in IE8 than JS Bin does, so I might have found a replacement for JS Bin. Furthermore there is also support for testing AJAX requests.
Happy hacking!
JS Bin the perfect JavaScript lab
How often haven’t I created that infamous test.html page to verify that my JavaScript manipulated the DOM in the way I expected it to? Often such experiments include usage of jQuery or other commonly used JavaScript libraries. Precious time lost creating similar test files over and over again, okay well perhaps a bit exaggerated, but still a pain.
However now I’ll never need to create another test.html page again thanks to JS Bin. It’s the perfect JavaScript lab at least for me; the screen is split in two, and on the left you have a JavaScript editor on the right an HTML editor, both with Syntax Highlighting, and other nice to have features. From a drop down list in the top you can include your favorite JavaScript libraries and you are ready to hack away!
It is in my opinion a perfect learning environment if you are new to JavaScript, HTML5 or both, you can play around with new ideas, more abstract aspects of JavaScript, or what-ever you want. JS Bin was built by Remy Sharp who you can follow on Twitter here @rem, that’s how I found out about JS Bin. Remy tweets about JavaScript, HTML 5 and other such gems very useful stuff indeed.
I also recommend reading John Resig’s blog, John Resig is probably already well-known to you as the creator and lead developer of the jQuery library, but he has also written some books about JavaScript programming and created a very useful tutorial for learning more advanced JavaScript topics. You can follow John on Twitter here @jeresig
Importing a PST-file to Thunderbird without using Outlook
It is in situations like this that I really appreciate my virtual Ubuntu!
I was preparing an old hard disk for reuse, I mean it was really old, 7 years old minimum. I found a PST-file on it from when I used Microsoft Outlook, and I got curious, what old emails would I find there?
Oh but behold, Thunderbird required Microsoft Outlook to import the PST-file, so what to do :-S? Ubuntu please rescue me! Ta-daaa, after some search I found a command utility called readpst, which can convert a PST-file to a folder hierarchy of plain text-files, one for each email in the PST-file, thank you Ubuntu (well actually Linux command utilities in general)!
First of all you might not have readpst installed, but that’s only an apt-get-call away, running the following command from the terminal:
> sudo apt-get install readpst
It doesn’t really matter if you already have the utility installed or not, you can use the command above to confirm whether the utility is installed and if it isn’t, well then you saved yourself a bit of time since it will be installed
My first few attempts with readpst didn’t turn out so well, but after some fine-tuning I came up with the following set of options for the readpst command:
> readpst -D -M -b -o pst thomas.pst
I will just briefly walk through the command options:
- The -D option tells readpst to include deleted emails
- The -M option tells readpst to include attachments in the mail-files and not as separate files in the file system
- The -b option tells readpst to omit the RTF version of the email, which would otherwise be included as attachment in the mail-files
- The -o pst option specifies where the output goes
- And last but not least include your PST-file which needs to be converted
Voila! That was a huge step in the right direction, after running the command above I had a folder structure mirroring the original folder structure from the PST-file, well at least I assume that it’s pretty close to the same. However in the conversion all the email-files were given a number and nothing else, no extension, nothing!
I renamed one file from 1 to 1.eml and dragged it into a folder in Thunderbird installed on my native Windows 7, and the email was imported to Thunderbird, the email even got all of it’s attachments intact, super! I could just drag’n'drop import all my emails.
My next problem turned out to be, how on Earth do I add the eml-extension to all of these files? Should I do it manually? No no no, not in a million years, Ubuntu, rescue me again, please!
I needed to find a way to recursively scan through all the folders created by readpst and add the eml-file extension to the name, after a bit of Googling I found a shell script for recursion (sorry for my ignorance, not a master of shell scripting, not yet at least). I found the shell command for renaming, funny enough it’s called rename, and it allows you to use Perl regular expressions when searching for files to rename, sweet!
#!/bin/bash
rename_fun()
{
# For each folder do
for i in `ls -ltr | grep '^d'| awk '{print $8}'`
do
# Output the folder's name, to track progress
echo $i;
# Move down into the sub-folder
cd $i;
# Add the eml-extension to all files with numeric names
rename 's/([0-9]+)$/$1.eml/' *
# Recurse deeper down into the folder structure
rename_fun
# When done move up from the sub-folder
cd ..
done
}
# Call to rename files
rename_fun
WARNING: The script is not child-safe, start from the wrong working directory in the prompt and suddenly you have added the eml-extension for a whole lot of files, which were never supposed to be changed. Furthermore it assumes that no mail folders use spaces in the folder name, I only had to folders which contained a space in the name, so I opted for the lazy solution: I removed spaces from my mail folders.
The only setback about this setup, is that I had to manually drag’n'drop the converted emails into my Thunderbird, that did take me some time, but I ran out of good ideas to how I could get the eml-files into Thunderbird in a smarter way, if you know just how to do that, then please add a comment below, thank you in advance!
It goes with the story to tell, that these emails turned out to be from the period of 2000 – 2004 and they were not that interesting actually, so perhaps this was just a waste of time for me, but I thought it might help someone else. Feel free to comment on this post, good ideas and constructive critique are always welcome!
I have listed some references below, which helped me in the right direction, I thank everyone who has unwillingly or unknowingly contributed to set my curiosity at rest! Among the links below you will also find a Thunderbird plugin, but according to the comments it doesn’t work, and I didn’t try. I decided to pursue the readpst solution instead based on the Ruby project at Google Code, which by the way didn’t work for me either.
http://code.google.com/p/ruby-msg/
http://www.five-ten-sg.com/libpst/rn01re01.html
http://kb.mozillazine.org/index.php?title=Import_.pst_files&printable=yes
http://freshmeat.net/projects/pstimportplugin/?branch_id=72191&release_id=265973
http://www.unix.com/shell-programming-scripting/84210-recursive-call-find-files-directories-shell-script-current-path.html
Desktop cleaning and uh-lala
Oh man my desktop is a mess, once again, I can’t believe it! I try to keep it clean, but sometimes my downloads control me, and yeah well, then it’s just easier to put it on the desktop, my Eeec’s desktop is chaos.
In the past I cleaned up once or twice on my old laptop simply by moving everything to a folder called “Must clean-up”, didn’t help much, before long I had a folder called “Must clean-up part II”.
Anyway during my recent look-at-and-throw-away session I came across another sketch I made with Harmony:
Mounting Windows shared folders with VirtualBox running Linux
I had problems mapping my shared windows folders onto the virtual Ubuntu installation, the documentation didn’t help at all, this is what I found in the help files:
> mount -t vboxsf [-o OPTIONS] sharename mountpoint
But that didn’t work, however after Googling a bit I found a blog entry which worked for me, using the following command instead:
> mount.vboxsf storage /storage
Where my shared folder on windows is named storage and map to my D drive:
Screen resolution for Ubuntu on VirtualBox
I found a really interesting framework called Node which is using JavaScript to create network applications, it’s build on Google’s V8 engine, really sweet! I hope to write some interesting posts later about Node but I have only just started looking at the examples Today.
However my first challenge was that Node is only tested under cygwin on Windows, so instead of installing cygwin I decided to create a virtual machine using VirtualBox, because it shouldn’t be too easy and because I have long wanted to try out a Linux desktop, but I still need my Windows for a number of things, so while I figure out how to completely move to Linux, it’s going to be nice with a virtual solution just to try it out.
Installing VirtualBox was not hard, it took only a few minutes, I prepared a virtual machine for Ubuntu configured with 512 MB memory and 8 GB virtual hard disk. Over night the Ubuntu ISO was downloaded – yeah, my Internet connection is not that fast, comes with the territory.
I am really amazed by the improvements on installers for Linux now a days and Ubuntu’s installer was no exception, it’s just so much easier than it used to be. I recall the first easy to use installer as being for the Mandrake distribution, but I am not entirely sure about that. I got Ubuntu installed and I just had one small problem; the screen resolution of the virtual machine was not more than 800 x 600, which didn’t look nice on my 1440 x 900 monitor.
After messing around a bit with things and having made a number of wrong installs on the virtual machine, realizing that my Linux capabilities have become a bit rusty, I finally figured it out; setting the resolution should not be made from within the virtually installed OS – no, in my case a setting on the virtual machine’s configuration did the trick, I enabled the Remote Display server and suddenly I had access to many different screen resolutions when I booted my Ubuntu virtual machine.
At first I didn’t realize I had solved my problem, because the screen resolution only really worked for me when I was running in full screen mode, so it’s a great advantage that I have two monitors. Now one monitor can run Ubuntu on VirtualBox in full screen, and the other monitor displays my Windows 7 desktop and because of seamless mode I just move my mouse over to the virtual machine and I can begin to interact with it, really nice!
I am very impressed with VirtualBox, I have used VMWare in the past, but I must say that this beats it, especially on the price
And perhaps in the future my main operating system will become Ubuntu and I will be back on Linux again, that wouldn’t be too bad at all, then my Windows 7 can run virtually instead and I might actually be able to move the mouse even during I/O operations.
HTML5 the beginning
I haven’t had much time to learn much about HTML5 but here the other day I came across Chrome Experiments which contains a lot of small to larger experiments utilizing HTML5 features.
The site is awesome and there is a lot of inspiration to gather from the experiments. I tried out a few, and will return again later, with the Harmony experiment I created the following sketch in few minutes, so much fun.








