Using Boolean Values with TouchJSON

When creating an NSDictionary to serialize as JSON with TouchJSON you can’t set a BOOL value in the dictionary because it requires an object.

Use an NSNumber, which will be converted to true or false by the TouchJSON library.

Eg:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber numberWithBool:YES forKey:@"shouldBeOn"];
[dictionary setObject:[NSNumber numberWithBool:NO forKey:@"shouldBeOff"];
NSError *error = NULL;
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dictionary error:&error];

Produces:

{
    "shouldBeOn": true,
    "shouldBeOff": false
}