Skip to content

Instantly share code, notes, and snippets.

@greenisus
Created October 20, 2011 20:40
Show Gist options
  • Save greenisus/1302314 to your computer and use it in GitHub Desktop.
Save greenisus/1302314 to your computer and use it in GitHub Desktop.
- (id) serialize:(id) object{
NSData *value = nil;
id serializedObject = nil;
if ([object isKindOfClass:[CKRecord class]]) {
CKRecord *record = (CKRecord *)object;
NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];
NSDictionary *attributes = [record attributes];
for (NSString *name in attributes) {
// performSelector causes a warning, so using objc_msgSend instead
id attrValue = objc_msgSend(record, NSSelectorFromString(name));
if ([attrValue isKindOfClass:[NSString class]] || [attrValue isKindOfClass:[NSNumber class]]) {
[jsonDictionary setValue:attrValue forKey:name];
}
}
serializedObject = jsonDictionary;
} else {
serializedObject = object;
}
if([NSJSONSerialization isValidJSONObject:serializedObject]){
NSError *error = nil;
value = [NSJSONSerialization dataWithJSONObject:serializedObject options:0 error:&error];
if(error)
NSLog(@"%@", [error localizedFailureReason]);
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment