mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-21 16:23:20 +08:00
* Introduce RKObjectMappable#relationshipsToSerialize to define nested relationships to post * New helper methods for working with relationship serializations. * Extended Rails router to serialize nested objects according to Rails idioms
70 lines
2.2 KiB
Objective-C
70 lines
2.2 KiB
Objective-C
//
|
|
// NSDictionary+RKRequestSerialization.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 7/28/09.
|
|
// Copyright 2009 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "NSDictionary+RKRequestSerialization.h"
|
|
|
|
// private helper function to convert any object to its string representation
|
|
static NSString *toString(id object) {
|
|
return [NSString stringWithFormat: @"%@", object];
|
|
}
|
|
|
|
// private helper function to convert string to UTF-8 and URL encode it
|
|
static NSString *urlEncode(id object) {
|
|
NSString *string = toString(object);
|
|
NSString *encodedString = (NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
|
|
(CFStringRef)string,
|
|
NULL,
|
|
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
|
|
kCFStringEncodingUTF8);
|
|
return [encodedString autorelease];
|
|
}
|
|
|
|
|
|
@implementation NSDictionary (RKRequestSerialization)
|
|
|
|
- (NSString*)URLEncodedString {
|
|
NSMutableArray *parts = [NSMutableArray array];
|
|
for (id key in self) {
|
|
id value = [self objectForKey:key];
|
|
if ([value isKindOfClass:[NSArray class]]) {
|
|
for (id item in value) {
|
|
if ([item isKindOfClass:[NSDictionary class]]) {
|
|
// Handle nested object one level deep
|
|
for( NSString *nKey in [item allKeys] ) {
|
|
id nValue = [item objectForKey:nKey];
|
|
NSString *part = [NSString stringWithFormat: @"%@[][%@]=%@",
|
|
urlEncode(key), urlEncode(nKey), urlEncode(nValue)];
|
|
[parts addObject:part];
|
|
}
|
|
} else {
|
|
// Stringify
|
|
NSString *part = [NSString stringWithFormat: @"%@[]=%@",
|
|
urlEncode(key), urlEncode(item)];
|
|
[parts addObject:part];
|
|
}
|
|
}
|
|
} else {
|
|
NSString *part = [NSString stringWithFormat: @"%@=%@",
|
|
urlEncode(key), urlEncode(value)];
|
|
[parts addObject:part];
|
|
}
|
|
}
|
|
|
|
return [parts componentsJoinedByString: @"&"];
|
|
}
|
|
|
|
- (NSString*)HTTPHeaderValueForContentType {
|
|
return @"application/x-www-form-urlencoded";
|
|
}
|
|
|
|
- (NSData*)HTTPBody {
|
|
return [[self URLEncodedString] dataUsingEncoding:NSUTF8StringEncoding];
|
|
}
|
|
|
|
@end
|