Skip to content

Instantly share code, notes, and snippets.

@pashields
Created August 22, 2011 15:38
Show Gist options
  • Save pashields/1162690 to your computer and use it in GitHub Desktop.
Save pashields/1162690 to your computer and use it in GitHub Desktop.
Deserialize json obj-c
//
// RIP.m
// RIP
//
// Created by Patrick Shields on 8/21/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "Resty.h"
#import "RestyConfig.h"
@implementation Resty
+ (id)dict:(NSDictionary*)dict toClass:(Class)classType {
NSObject *obj = [classType new];
for (NSString *key in [dict allKeys]) {
id val = [dict objectForKey:key];
/* Key is an array, recursive search for nested objects */
if ([val isKindOfClass:[NSMutableArray class]]) {
for (int i=0; i<[val count]; i++) {
if ([[val objectAtIndex:i] isKindOfClass:[NSMutableDictionary class]]) {
[val replaceObjectAtIndex:i withObject:[Resty dict:[val objectAtIndex:i] toMappedName:key]];
}
}
} else if ([val isKindOfClass:[NSMutableDictionary class]]) {
val = [Resty dict:val toMappedName:key];
}
[obj setValue:val forKey:key];
}
return obj;
}
+ (id)dict:(NSDictionary *)dict toMappedName:(NSString*)key {
RestyConfig *config = [RestyConfig sharedInstance];
Class subClass = [config.mappings objectForKey:key];
return [Resty dict:dict toClass:subClass];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment