RISON, a compact JSON specially suited for URIs

I was looking for a way to efficiently pass data between cross-domain iframes using the location hash trick. The first thing that I checked was JSON but the payload gets huge when url encoded, then I tried with base64 encoding which helps but not very much. Finally I found RISON which in short is nothing more than a reformulation of JSON’s control characters to make them more URI friendly.

It’ll basically use ( instead of {, !( instead of [ or ! instead of \ to escape stuff. The result is quite good with savings of a 50% on average when url encoding the result. The only problem I had was that the source code no longer seems to be maintained and the only copy I could find was in the repository of a project in google code.

So I’ve implemented my own version taking as example JSON’s official reference implementation API and rolling in a really simple token based parser. It passes all the test fragments from RISON’s home page and the performance is quite good for moderately sized payloads.

The code is licensed under the liberal MIT License and can be downloaded from my subversion repository.

Nokia's N810 on the mail

After being trying to get an iphone for a couple of months and haven’t succeed at it, I sat down and did an extensive research on the market offerings for my needs. Finally I found what I needed and to my surprise it wasn’t the phone with the Apple logo.

I spend more than two hours each day on the train which gets absolutely packed at rush hours, so my plan to work on the laptop while traveling is not a feasible option (Even with my smallish Dell XPS M1330). Checking email and rss feeds on my Sony Ericsson K800i cellphone works but with the huge amount of tunnels in the trip the data connection keeps falling and the screen is not that big as to make it a nice experience. So I decided to try with the Apple’s phone which should offer a great experience while on the move with its large touch screen and the ability to sync imap accounts for offline reading of the inbox and nice rss reading. Since I couldn’t obtain one I searched on the net for alternatives and I felt in love with Nokia’s N810 internet tablet device.

The N810 is not a smart phone, it just offers Wifi and Bluetooth radios so it needs to tether with a phone to access mobile data networks. It does offer however a wonderful touch screen sporting a 800×480 resolution with an amazing 225dpi (quite good for ebook reading), a full qwerty keyboard and a Linux based (Maemo) operating system with a ton of available third party application and games. What I like the most about the device is that its environment is really open, which will allow me to tinker with it and suit it best to my needs.

So yesterday I placed the order on Nokia’s online shop for it and I should be enjoying this wonderful device next week. A big big big thanks to Cristina for giving me this great present!

A web development environment (III) - Firefox

Firefox is a great browser and is also a great development tool in itself. There are some problems though with the numerous extension available to developers. They are a great resource when programming but they slow down (and in some scenarios they might even break) our normal web browsing.

So the idea is to have a normal Firefox installation to use as our browser and then a separate install (and profile) used only for development purposes. This way we can install tons of development related addons without worrying about slowing down our normal browsing. Of course this is only useful if you use Firefox as your primary browser.

  • Download the Firefox installer

  • Launch the installer

  • Set the destination folder to c:\dev\Develfox

  • Uncheck the options to create shortcuts on desktop, start menu…

  • Uncheck the option to launch Firefox on finishing the setup

  • Go to the installation directory and start a command prompt there

    • Run Firefox with the following command: firefox.exe -no-remote -ProfileManager

      • Create a new profile and name it Devel
      • Exit the profile manager
    • Create a new batch file name develfox.bat with the following contents. It’ll copy on each run the firefox executable to develfox.exe so we can tell it apart from our normal browser on the task manager (we copy it on each run so that automatic updates are correctly applied).

      @echo off
      copy "c:\dev\develfox\firefox.exe" "c:\dev\develfox\develfox.exe" > null
      start "" "c:\dev\develfox\develfox.exe" -no-remote -P "Devel"
  • Still on the installation directory go to the chrome folder and create in there a directory named icons and inside that one another called default. Put in there a custom icon with the name main-window.ico to be used as the application icon. Check out IconBase for some nice ones.

    • Create a shortcut on your desktop or another suitable place and use the following properties:

      • Target: c:\dev\develfox\develfox.bat
      • Start in: c:\dev\develfox
      • Change the icon to c:\dev\develfox\chrome\icons\default\main-window.ico
    • Launch the shortcut to execute our new Firefox installation. It’ll be a vanilla one so now it’s time to install our development addons ( Firebug, Webdev toolbar, Yslow, Clearcache, Venkman… ) and also a custom theme so we can easily know in which firefox we are at each moment.

    • To further customize our new Firefox installation we can install the Firesomething addon, which allows to change the window title to anything we want.

Update 2008–11–03

A great extension for Flash/Flex development is FlashSwitcher which allows to easily change the Flash player version used in Firefox. One problem though is that it’ll interfere with your default Firefox installation. To solve this you can edit its settings dialog and point the Firefox Plugin directory to c:\dev\develfox\plugins instead of the default location. This way the Flash player changes will only affect our development firefox installation.

A web development environment (II) - Cygwin

Following with the series I will introduce in this chapter Cygwin.

Cygwin is an excellent collection of Unix utilities and programs ported to Windows. While its emulation of an Unix like environment is quite good I actually don’t use it. I install it just for the helpful utilities it has. Although I do occasionally use its shell to perform some command line tasks which on Windows’ cmd.exe would be a real nightmare to perform.

Lets set it up and then we can see some tips and tricks.

  • Download the Cygwin installer

  • Move the installer (setup.exe) to c:\dev\cygwin and launch it

  • Set the Root Directory to c:\dev\cygwin and the Local Package Directory to c:\dev\cygwin\packages

  • Choose at least the following packages to install. You won’t probably need all of them but in my experience these are the most commonly used and they don’t take much space actually:

    Admin
    shutdown
    Archive
    unzip, zip
    Base
    bash, findutils, grep, gzip, run, tar, sed, termcap, terminfo
    Database
    sqlite3
    Devel
    binutils, automake, cvs, cygport, gcc, make, subversion
    Editors
    nano, vim
    Graphics
    ImageMagick
    Interpreters
    m4, perl, python, ruby
    Net
    curl, openssh, openssl, rsync
    Shells
    bash, bash-completion
    Text
    less, tidy
    Utils
    bzip2, cygutils, diffutils, gnupg, patch, scree
    Web
    cadaver, curl, httping, links, lynx, wget
  • Now wait for all packages to download and install (it will take a while)

  • Once the installation is complete we can launch the Cygwin system by running c:\dev\cygwin\cygwin.bat

  • On the first run it’ll create a new configuration and home directory for our user. Follow the instructions given on the terminal to complete the installation.

Ok, so Cygwin is now setup and it’s time to learn a few tips.

On a standard installation Cygwin will create a home directory for the user in c:\dev\cygwin\home\[USER]. However we might want to re-use our Windows folder so we have a unique location for our stuff. To do it we just need to create an environment variable (in System Preferences) called HOME with the path to our profile folder (c:\Users\[USER] in Vista). Now we have to edit /etc/passwd and change the home folder to our Windows profile one (/cygdrive/c/Users/[USER]), we can move the contents of the old home folder to the new one with the following commands:

$ mv /home/[USER]/* /cygdrive/c/Users/[USER]/.
$ mv /home/.* /cygdrive/c/Users/[USER]/.

By default the Windows drive letters are accessible in /cygdrive/[DRIVE], that is ok but it’s used often so we can save a few key strokes by creating symlinks at the root of the file system. Create one for each of your drive letters so you can access them as /[DRIVE]/my/path/to/a/file.

$ ln -s /cygdrive/c /c

As we have seen Cygwin uses Unix like paths so to launch Windows programs from the shell we have to make an extra step and that is to convert between paths. Fortunately there is a little tool called cygpath. When used without modifiers it will translate a Windows path to a Cygwin one and when used with the -w or the -m modifiers it will do the opposite conversion.

Another nice tool is cygstart which launches a program (or the default program associated with a document). The good about it is that it will launch the program and detach the process from the console.

For commonly used programs it’s a bit uncomfortable to use cygpath and cygstart, like for example to launch our favourite text editor. In this example I’m going to use Notepad as an example. We just have to change our bash init script (~/.bashrc) to include a function to launch Notepad easily from the command line. Add the following to your .bashrc file:

notepad () {
    cygstart "c:/Windows/System32/notepad.exe" "`cygpath -w $1`"
}

Now we can edit the contents of a text file just doing notepad /var/log/setup.log. This is a great way to really integrate Cygwin with Windows, which actually makes the Cygwin system ready for real work.

Another useful utility is cdargs which while not an official part of the Cygwin system can be compiled from source. To install it we just have to download its latests source tarball and unpack it into your Cygwin home directory. Then issue the following commands:

$ cd cdargs-1.XX
$ ./configure
$ make
$ make install
$ cp contrib/cdargs-bash.sh /etc/profile.d/.
$ source /etc/profile.d/cdargs-bash.sh

Explaining how to use this tool is beyond the scope of this article. There is a nice mini tutorial at Linux.com and Google will give you plenty of information about this tool.

There is a lot of software compatible with Cygwin beyond the packages included in the official repository. The Cygwin Ports project offers a few dozens of applications already compiled and ready to be installed. It’s well worth a look. Besides there are many Unix/Linux applications which can be compiled to be used in Cygwin even if they don’t redistribute the binary packages.