I’ve been playing around trying to parse and generate JSON from Objective-C the last few days. The core of the parser takes the form of a category on NSScanner, and I’m really chuffed with how neat and clear the code turned out. A tip of the hat to the designers of Objective-C and NSScanner!

A category—though originally conceived as a way to split large classes into multiple files (at least this is my understanding)—can be used to extend classes at run-time. This is a feature that is most commonly associated with interpreted languages. Used soberly it can be used to great effect to add functionality to a class without having to subclass it. You can add methods, and even override existing ones, but not add any instance variables.

The JSON Framework I’m working on provides no public classes on its own. Instead it adds a method to NSString that returns an object structure representing the JSON string. To emit JSON for a complex structure I’ve added a method to NSObject that will do the right thing for nulls, booleans, numbers, arrays, dictionaries and strings.

Please be aware that the code at point is the result of just one night and 2 days of coding. There are no docs. There are no options. There are no limits, so if you feed it carefully crafted JSON you can make my parser run out of C stack space. There is working proof-of-concept code, copyright statements, and tests.

Update 24/9/07: I’ve released version 0.1 of my JSON framework now.

Tuffmail & Sieve

April 11, 2007

One of the reasons I used to run my own mail server was so I could do server-side mail filtering of messages into various folders depending on their content. I’d normally reach for procmail for this but with Tuffmail, which I’ve recently started using, server-side mail filtering is provided by way of Sieve filters.

Writing Sieve filters isn’t particularly hard, but here’s a few things worth noting (I believe them to be Tuffmail-specific):

  • Most tutorials and examples on the ‘Net use ‘.‘ as the mailbox separator, while Tuffmail use ‘/‘. Your Sieve rules must use slashes as mailbox separators if you want to file mail into nested folders (e.g. INBOX/Lists instead of INBOX.list). (Alternatively, you can change the separator back to being a ‘.‘)
  • Folders are not created automatically. If you configure Sieve to deliver mail into a folder that doesn’t exist your mail will just go to INBOX instead.
  • The ANS (Alternate Name Space) setting must be taken into account when filing mail into subfolders. (If it is off, you’ll want to file into INBOX/Lists/Doxygen; if it is on, use Lists/Doxygen.)

In addition to these caveats I found the Sieve documentation to be rather impenetrable. To help you get started using it, here’s a snippet that files Svk and Doxygen mailing list messages into their own folders and all other list messages into a third:

require "fileinto";

if header :contains ["List-Id"] "doxygen" {
    fileinto "INBOX/Lists/Doxygen";

} elsif header :contains ["List-Id"] "svk" {
    fileinto "INBOX/Lists/Svk";

} elsif exists ["List-Id"] {
    fileinto "INBOX/Lists/Misc";
}

The “List-Id” header is an RFC<mumble> header used by well-behaving mailing lists to identify themselves, so it’s a great header to filter on. The last clause is a catch-all—it just checks if the List-Id header exists at all.

TV licensing

January 24, 2007

I sent the following mail to the TV licensing people a few days ago:

As far as I understand it is the tuner in a TV (the thing that makes the TV or VCR able to decode broadcast programs) that is requiring a license, not the actual screen (hence computer screens, projectors etc are not liable, but VCRs are). Therefore:

1 - If I buy a TV without a tuner I do not have to buy a TV license, since it cannot receive broadcasts on its own (obviously if I bought an external tuner for it I would have to buy a licence).

2 - If I bought a TV with an NTSC tuner (a broadcast format used in Northern America) it would not be able to receive broadcasts in the UK (which use the PAL system). Therefore I should not have to pay license.

Can you please confirm that I am correct in these two observations? If you, for some reason, disagree with any of them, please state why you think I am wrong.

Today I got this response:

Dear Mr Brautaset

Thank you for contacting us.

If the television set you purchase has it’s tuner removed and so it isn’t capable of receiving any broadcasts then no licence would be required.

If you purchase a NTSC only television and as long as it doesn’t receive any broadcasts . including foreign broadcasts then no licence would be required.

The bit about including foreign broadcasts is interesting, but this is all very encouraging. I’ve been drooling after a Samsung 32 inch TV for a week or so, and that page states that it only has an NTSC tuner; I actually suspect the page is wrong (and have sent an email off to the dealtime.co.uk people to confirm this) but if it’s not I’m going to buy one of those screens immediately.

Internet on the South Pole

August 17, 2006

I’ve occasionally wondered how they manage to get Internet to research bases at the North or South pole. You migth think they’d use satellites, but to communicate with a satellite you need direct line of sight; communications satellites in geostationary orbits, due to the curvaceous nature of our planet, are under the horizon and cannot be seen from the poles.

So how do they do it? Cable? Pretty unthinkable, I thought. However, according to this article that’s exactly what they’re planning to do: lay a cable nearly 2000 km across the polar ice to the South Pole. It also, more interestingly, explained how they currently manage to get Internet access: by exploiting ageing communications satellites that have drifted out of their geostationary orbits and can therefore be just seen part of the day…

Ask Later

July 26, 2006

Steve Coast and Tom Carden arranged an evening of talks using the Pecha Kucha format (unfortunately they ran into trademark issues so the night was renamed to Ask Later on short notice). Nevertheless, good fun was had by all.

Matt’s talk about building an automated Sudoku solver to win $PAPER’s Sudoku challenge seemed to draw the biggest cheer. It came complete with its own homebrewed OCR component, to deal with the dodgy scanned JPG format the challenge was presented in. Unfortunately, just as he finished his fully automatic Soduko solver the $PAPER stopped presenting today’s challenge on the web…

Printing on water

July 25, 2006

Pink Tentacle reports a couple of interesting stories, including this fabulous research into printing on water. If nothing else, the machine itself looks uber-cool.

Connected!

April 30, 2006

We finally have a network connection at the new place. Though we don’t have ADSL yet (there’s a problem transferring it here because plus’s website thinks my flat is a business address) plus kindly gives us a backup phone connection. We tried to get it working yesterday but got no dialtone and figured the phone was not working yet (it was only connected on Thursday, and there was talks about a fault on the line somewhere). It still didn’t work today but after unpacking a phone and successfully making a call to my mobile we smelled a rat, and, indeed, we had a dead socket. Plugging the modem into the master socket we could call out just fine.

Only problem is the cable is very short and the master socket is in the hallway. Luckily there’s a small closet just across for it with room for a seat. It’ll be great to have ADSL again though. Here’s Nadia enjoying our current connection arrangement.

WordPress 2.0.1

March 3, 2006

I just upgraded to the latest WordPress, after seing they did a 2.0.1 release about a month ago. It may be just my imagination, but it “feels” faster than 2.0.0. Good job guys!

New Headphones

December 2, 2005

Earlier when I’ve griped about the iPod Shuffle, saying it needed a dynamic compressor, Mark Fowler told me I just needed better headphones. I didn’t really take him too seriously as I didn’t think there could be that much difference between in-ear headphones.

Nevertheless, when my headphones failed recently, I decided to try the Shure EC2 he were recommending. I found them a bit weird and painful to wear to start with (they don’t just go in the ear, they go inside the ear canal) but that was just a matter of finding which of the 9 different sizes and types of sleeves fit my ears best. Because they fit so snugly into the ear canal, they significantly reduce the amount of surrounding noise that you hear. This means I don’t have to jog the volume on my iPod up and down all the time if a song has quiet and loud parts; the effective dynamic range is much greater. They also manage much lower frequencies than I’ve heard in any other headphones.

In short they have made it enjoyable to listen to music on my iPod again. That makes them worth the (rather stiff) price, in my opinion.

New mobile phone

November 9, 2005

Nadia has bought me a new mobile phone. Up till now I’ve been with O2, but this is on Orange. This means my mobile number will change in the nearby future, but my old phone will be lying around turned on for a transition period.

I’ve been wanting to change phones for a number of reasons, chief among them being:

  • The keylock on my current Siemens phone (a c65, I think) is shockingly bad; in effect, it has no keylock. The result is that my phonebook is chock-full of garbage such as “##*#”, “###” and variations of that theme, and my bag has a habit of calling people on its own.
  • I’m really fed up with being on pay-as-you-go. Every time I really need to call I’m out of credit, and O2’s credit top-up procedure blows goats. Unfortunately with my usage (5-10 ppm) switching to a contract phone is going to be at least twice as expensive as pay-as-you-go.
  • I want bluetooth synching to my Mac’s addressbook. I abhor typing on phones, and trying to manually keep two sets of address books in synch is rather horrible.
  • I have a terrible sense of direction so I’d like the ability to check google maps, streetmap or multimap when I’m trying to find the place I’ve agreed to meet with friends.

Thoughts about the new phone: since this is a Nokia clam phone, the keylock is not going to be an issue now. I’m still going to be on pay-as-you-go, but Orange offer direct debit so maybe this will not be so bad. Still no bluetooth synching; since getting a contract phone is out of the question, bluetooth phones are way out of my price range. However, Nadia transferred the numbers from my old phone for me, so I didn’t have to do that part, at least. As for maps, this thing does GPRS but I’m not sure if I can use any of the mapping services with that yet.

Update: After advice from a friend I’ve now gone through the steps required for keeping my old phone number. You can stop panicking now.