David Janes' Code Weblog

November 10, 2013

Pretty Printing JSON in Objective-C

code fragments,iphone,json,objective-c · admin · 2:23 pm ·

Apple’s JSON serializer NSJSONSerialization – despite having years to get it right – makes a horrible hash of pretty printing JSON. It does not sort the keys and it puts backslashes in front of all slashes.

SBJson does a much nicer job of this. Here’s a code fragment that will use SBJson if you’ve linked to it, otherwise it will fall back to Apple’s code.

+ (NSString*) formatJSONPretty:(id)_obj
{
    /*
     *  If we are linked with SBJsonWriter, we use those libraries as
     *  it has a much nicer formatter
     */
    id writer = [[NSClassFromString(@"SBJsonWriter") alloc] init];
    if (writer) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [writer performSelector:NSSelectorFromString(@"setHumanReadable:") withObject:@YES];
        [writer performSelector:NSSelectorFromString(@"setSortKeys:") withObject:@YES];
        return [writer performSelector:NSSelectorFromString(@"stringWithObject:") withObject:_obj];
#pragma clang diagnostic pop
    }

    NSData* d = [NSJSONSerialization dataWithJSONObject:_obj options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
}

January 7, 2013

I Made It

administrivia · admin · 7:20 am ·

For the foreseeable future, I’ll be writing at my latest blog “I Made It“, where I’ll be covering DIY, the Internet of Things and my 2013 projects.

Please subscribe, follow, etc.!

November 25, 2012

“MMObility: Why the closing of Glitch matters”

games · admin · 8:29 am ·

Beau Hindman:

On top of all of the wonderful art, music, and hipper-than-hip nods and winks that went on during Glitch’s run, Tiny Speck released a very robust sandbox that was not all about “hardcore” PvP, looting, and griefing other players. It was a sandbox that was first and foremost a journey into a very unique world with elements, characters, and systems we had never seen before. More innovations, please? Glitch delivered innovation in the tired and often angry sandbox gaming genre.

Glitch encouraged players to work together, made combat an occasional and shocking thing (as it should be), brought some of us almost to tears with lore that spoke of alien yet parental love, and gave us choices to do almost anything we wanted with our characters. Even our character’s sex was unknown. It was up to us to do what we wanted, which is the true reason a sandbox should exist, not just for hardcore PvP that is actually quite unrealistic and silly.

I.e. Glitch closed because it provided a game mechanic that the designers wanted but (potential) players didn’t.

August 25, 2012

Simperium – cross platform object synchronization

links · admin · 7:03 am ·

Simperium.

Cross-platform (including iOS) fine-grained data object sharing. Kinda like iCloud but better? There’s a video at the link.

I don’t see Android in the list, this seems like something of an oversite.

Cost is around 2-4¢/user/month.

June 4, 2012

Editorial – How to Save RIM

ideas,rim · admin · 5:57 am ·

Apple and IBM are two companies that made successful turnarounds from seemingly fairly desperate situations. Generally the turn arounds were done by a powerful outsider[1] brought to the company, who cut through a Gordian Knot type situation.

RIM has two major issues: their hardware and their apps.

Hardware

Their hardware lineup is confusing, frequently breaks and uninspired of design and engineering.

When Steve Jobs returned to Apple, he simplified the product line from a jumble of numbered products with overlapping feature sets into 4 four products, based on a 2×2 matrix: inexpensive / expensive × desktop / laptop.

RIM should do something similar, simplifying its hardware line into three or four models easily grasped in purpose:

  • BlackBerry Student / Third World — Nokia competitor inexpensive  starter model
  • BlackBerry Business — Keyboard model
  • BlackBerry Touch — Touch Screen model, iPhone / Android competitor

And that’s that. I don’t know what to do with the PlayBook but unless it supports some critical business function that people buy BlackBerry mobiles for, it should be dropped.

Apps

Apps used to be almost impossible to develop cost effectively for BlackBerries due to a plethora of semi-compatible devices and a backwards Java ME development environment. RIM’s solution — for the future — is to allow you to develop in HTML5, C++, Java ME, Android, and Adobe AIR.

Where does this leave us? There is no reason to develop anything for RIM now, as things that run on the old OS are certainly approaching end-of-life; things that run on the new OS BB10 don’t have a device to deploy to! And what a confusing mess of choices.

What to do?

IMHO Simple: RIM should become (and announce that it is becoming) the best device to run HTML5 on it the world. It should hire the best the team of developers to make this happen and make sure that when anyone views a webpage on a BB device, it’s the best possible experience possible. RIM should provide an environment for security and e-commerce infrastructure that works HTML5 websites — that can be run on non-BB devices. Add massive and/or intelligent caching in the device to speed things up. Built in jQuery. And so forth.

And it should run on all the BB models above.

And it should drop support for C++, Java ME, Android and Adobe AIR.

[1] it’s safe to say Steve Jobs had any major favours left  owed to senior Apple management

April 8, 2012

NSURLConnection, ETag and caching

code fragments,iphone,tips · admin · 7:40 am ·

If you’re using NSURLConnection and using If-None-Match/ETag and you’re depending on getting a 304 status to determine that “nothing changed”, make sure you do this:

[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

If you don’t, after your first request/response cycle, NSURLConnection will start returning status 200 instead as it’s pulling the result from its local cache.

February 26, 2012

iTunes & Missing Files

macintosh,tips · admin · 2:45 pm ·

If you’re like me and keep your iTunes files on external disks, you’ll occasionally end up in a situation where the external disk is not available and iTunes ends up marking all your files with an “exclamation mark”, indicating they’re not available.

To have to do to fix this is hold CTRL+ALT when starting iTunes and it’ll re-figure everything out.

Hat tip.

February 2, 2012

times

python · admin · 7:49 pm ·

Interesting

Times is a small, minimalistic, Python library for dealing with time conversions to and from timezones, for once and for all.

times tries to make working with times and timezones a little less of a clusterfuck and hopefully set a standard of some sort.

See my previous posts on working with pytz and dateutil (and also). I’m looking forward to trying out times.

December 7, 2011

Thoughts on Python 3

python · admin · 7:36 am ·

I’m learning a new language next year (suggestions welcome). I’ve totally ignored Python 3 and plan to continue doing so.

Read Armin Ronacher on “Thoughts on Python 3“.

Because as it stands, Python 3 is the XHTML of the programming language world. It’s incompatible to what it tries to replace but does not offer much besides being more “correct”.

Python 3 kills what makes Python great.

November 15, 2011

Python, Sqlite3, FTS3 & MacOSX

db,python,tips · admin · 8:49 am ·

If you can’t load FTS3 on a Mac, this post on StackOverflow tells you how to solve the problem.

The final bit of magic is in:

from pysqlite2 import dbapi2 as sqlite3

Older Posts »

Powered by WordPress