More info: http://developing-ios-apps-with-andrew-shmig.blogspot.ru/2013/03/asadeepdictionary.html
Deep dictionary using KVC (+JSON)
Example #0:
JSONDataExample.json: http://paste.ubuntu.com/5609872/
NSString *pathToJSON = [[NSBundle mainBundle] pathForResource:@"JSONDataExample"
ofType:@"json"];
NSLog(@"pathToJSON: %@", pathToJSON);
NSData *json = [[NSString stringWithContentsOfFile:pathToJSON
encoding:NSUTF8StringEncoding
error:nil] dataUsingEncoding:NSUTF8StringEncoding];
ASADeepDictionary *dd = [[ASADeepDictionary alloc] initWithJSON:json];
[dd setAlias:@"mainServlet" forKey:@"web-app.servlet"];
[dd setAlias:@"logFile" forKey:@"init-param.dataLogLocation"];
NSLog(@"dataLogLocation: %@", [dd valueForKey:@"$mainServlet.#4.$logFile"]);
[dd release];
dd = nil;
Example #1: ASADeepDictionary allocation and initialization (writing and reading values)
ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
[dd setValue:@"AndrewShmig" forKey:@"cookies.user.login"];
[dd setValue:@"[email protected]" forKey:@"cookies.user.email"];
[dd setValue:@"ru_RU" forKey:@"cookies.user.locale"];
NSLog(@"login: %@", [dd valueForKey:@"cookies.user.login"]);
NSLog(@"email: %@", [dd valueForKey:@"cookies.user.email"]);
NSLog(@"user record: %@", [dd valueForKey:@"cookies.user"]);
Output:
login: AndrewShmig
email: [email protected]
user record: {
email = "[email protected]";
locale = "ru_RU";
login = AndrewShmig;
}
Example #2: ASADeepDictionary to JSON convertion
ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
[dd setValue:@"AndrewShmig" forKey:@"cookies.user.login"];
[dd setValue:@"[email protected]" forKey:@"cookies.user.email"];
[dd setValue:@"ru_RU" forKey:@"cookies.user.locale"];
NSData *jsonData = [dd JSON];
NSLog(@"JSON: %@", [NSString stringWithUTF8String:[jsonData bytes]]);
Output:
JSON: {
"cookies" : {
"user" : {
"login" : "AndrewShmig",
"email" : "[email protected]",
"locale" : "ru_RU"
}
}
}
Example #3: Init ASADeepDictionary with another dictionary
NSDictionary *dic = @{@"window":@{
@"position": @{
@"x": @"100",
@"y": @"200",
@"orientation":@{
@"horizontalAlign": @"YES",
@"verticalAlign": @"NO"
}
},
@"size": @{
@"width": @"500",
@"height": @"780"
}
}};
ASADeepDictionary *dd = [[ASADeepDictionary alloc] initWithDictionary:dic];
NSLog(@"orientation: %@", [dd valueForKey:@"window.position.orientation"]);
Output:
orientation: {
horizontalAlign = YES;
verticalAlign = NO;
}
Example #4: Init ASADeepDictionary with JSON object
NSDictionary *dic = @{@"key":@"value",
@"key2":@"value2",
@"user":@{
@"name":@"AndrewShmig",
@"email":@"[email protected]"
}};
ASADeepDictionary *dd = [[ASADeepDictionary alloc]
initWithDictionary:dic];
ASADeepDictionary *dd2 = [[ASADeepDictionary alloc]
initWithJSON:[dd JSON]];
NSLog(@"%@", dd2);
Output:
{
key = value;
key2 = value2;
user = {
email = "[email protected]";
name = AndrewShmig;
};
}
Example #5: Using aliases
ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
[dd setValue:@{@"login":@"AndrewShmig", @"email":@"[email protected]"}
forKey:@"cookies.github.usergroup.admin"];
[dd setAlias:@"adminInfo" forKey:@"cookies.github.usergroup.admin"];
[dd setAlias:@"githubService" forKeyPath:@"cookies.github"];
NSLog(@"login: %@", [dd valueForKey:@"$adminInfo.login"]);
NSLog(@"email: %@", [dd valueForKey:@"$adminInfo.email"]);
NSLog(@"githubService: %@", [dd valueForKey:@"$githubService"]);
NSLog(@"github usergroup: %@", [dd valueForKey:@"$githubService.usergroup"]);
[dd removeAlias:@"adminInfo"];
NSLog(@"aliases: %@", [dd aliases]);
NSLog(@"%@", dd);
Output:
login: AndrewShmig
email: [email protected]
githubService: {
usergroup = {
admin = {
email = "[email protected]";
login = AndrewShmig;
};
};
}
github usergroup: {
admin = {
email = "[email protected]";
login = AndrewShmig;
};
}
aliases: {
"$githubService" = "cookies.github";
}
{
cookies = {
github = {
usergroup = {
admin = {
email = "[email protected]";
login = AndrewShmig;
};
};
};
};
}
Example #6: Array iteration with '#' placeholder
ASADeepDictionary *dd = [[ASADeepDictionary alloc] init];
NSArray *data = @[@{@"name":@"Andrew", @"age":@"21"},
@{@"name":@"Igori", @"age":@"25"},
@{@"name":@"Masha", @"age":@"19"}];
[dd setAlias:@"data" forKey:@"cookies.localstoragetype.xml.data"];
[dd setValue:data forKey:@"$data"];
NSLog(@"%@", dd);
NSUInteger count = [[dd valueForKey:@"$data"] count];
for(int i=0; i<count; i++) {
NSString *namePath = [NSString stringWithFormat:@"$data.#%d.name", i];
NSString *agePath = [NSString stringWithFormat:@"$data.#%d.age", i];
NSString *name = [dd valueForKey:namePath];
NSString *age = [dd valueForKey:agePath];
NSLog(@"%@ is %@ years old!", name, age);
}
Output:
{
cookies = {
localstoragetype = {
xml = {
data = (
{
age = 21;
name = Andrew;
},
{
age = 25;
name = Igori;
},
{
age = 19;
name = Masha;
}
);
};
};
};
}
Andrew is 21 years old!
Igori is 25 years old!
Masha is 19 years old!