├── .gitmodules ├── AFMsgPackSerialization ├── AFMsgPackRequestSerializer.h ├── AFMsgPackRequestSerializer.m ├── AFMsgPackResponseSerializer.h └── AFMsgPackResponseSerializer.m ├── AFOnoResponseSerializer.podspec ├── LICENSE └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "MsgPackSerialization"] 2 | path = MsgPackSerialization 3 | url = https://github.com/mattt/MsgPackSerialization.git 4 | [submodule "AFNetworking"] 5 | path = AFNetworking 6 | url = https://github.com/AFNetworking/AFNetworking.git 7 | -------------------------------------------------------------------------------- /AFMsgPackSerialization/AFMsgPackRequestSerializer.h: -------------------------------------------------------------------------------- 1 | // AFMsgPackRequestSerializer.h 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "MsgPackSerialization.h" 24 | #import "AFURLRequestSerialization.h" 25 | 26 | /** 27 | `AFMsgPackRequestSerializer` is a subclass of `AFHTTPRequestSerializer` encodes parameters as a MsgPack form body. 28 | */ 29 | @interface AFMsgPackRequestSerializer : AFHTTPRequestSerializer 30 | 31 | /** 32 | Options for writing the request MsgPack data from Foundation objects. For possible values, see the `MsgPackSerialization` documentation section "MsgPackWritingOptions". `0` by default. 33 | */ 34 | @property (nonatomic, assign) MsgPackWritingOptions writingOptions; 35 | 36 | /** 37 | Creates and returns a MsgPack request serializer with specified writing options. 38 | 39 | @param writingOptions The specified MsgPack writing options. 40 | */ 41 | + (instancetype)serializerWithWritingOptions:(MsgPackWritingOptions)writingOptions; 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /AFMsgPackSerialization/AFMsgPackRequestSerializer.m: -------------------------------------------------------------------------------- 1 | // AFMsgPackRequestSerializer.m 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFMsgPackRequestSerializer.h" 24 | 25 | @implementation AFMsgPackRequestSerializer 26 | 27 | + (instancetype)serializer { 28 | return [self serializerWithWritingOptions:0]; 29 | } 30 | 31 | + (instancetype)serializerWithWritingOptions:(MsgPackWritingOptions)writingOptions { 32 | AFMsgPackRequestSerializer *serializer = [[self alloc] init]; 33 | serializer.writingOptions = writingOptions; 34 | 35 | return serializer; 36 | } 37 | 38 | #pragma mark - AFURLRequestSerialization 39 | 40 | - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request 41 | withParameters:(id)parameters 42 | error:(NSError *__autoreleasing *)error 43 | { 44 | NSParameterAssert(request); 45 | 46 | if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { 47 | return [super requestBySerializingRequest:request withParameters:parameters error:error]; 48 | } 49 | 50 | NSMutableURLRequest *mutableRequest = [request mutableCopy]; 51 | 52 | [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) { 53 | if (![request valueForHTTPHeaderField:field]) { 54 | [mutableRequest setValue:value forHTTPHeaderField:field]; 55 | } 56 | }]; 57 | 58 | if (!parameters) { 59 | return mutableRequest; 60 | } 61 | 62 | [mutableRequest setValue:@"application/x-msgpack" forHTTPHeaderField:@"Content-Type"]; 63 | [mutableRequest setHTTPBody:[MsgPackSerialization dataWithMsgPackObject:parameters options:self.writingOptions error:error]]; 64 | 65 | return mutableRequest; 66 | } 67 | 68 | #pragma mark - NSSecureCoding 69 | 70 | - (id)initWithCoder:(NSCoder *)decoder { 71 | self = [super initWithCoder:decoder]; 72 | if (!self) { 73 | return nil; 74 | } 75 | 76 | self.writingOptions = (MsgPackWritingOptions)[[decoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(writingOptions))] unsignedIntegerValue]; 77 | 78 | return self; 79 | } 80 | 81 | - (void)encodeWithCoder:(NSCoder *)coder { 82 | [super encodeWithCoder:coder]; 83 | 84 | [coder encodeObject:@(self.writingOptions) forKey:NSStringFromSelector(@selector(writingOptions))]; 85 | } 86 | 87 | #pragma mark - NSCopying 88 | 89 | - (id)copyWithZone:(NSZone *)zone { 90 | AFMsgPackRequestSerializer *serializer = [[[self class] allocWithZone:zone] init]; 91 | serializer.writingOptions = self.writingOptions; 92 | 93 | return serializer; 94 | } 95 | 96 | @end 97 | -------------------------------------------------------------------------------- /AFMsgPackSerialization/AFMsgPackResponseSerializer.h: -------------------------------------------------------------------------------- 1 | // AFMsgPackResponseSerializer.h 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "MsgPackSerialization.h" 24 | #import "AFURLResponseSerialization.h" 25 | 26 | /** 27 | `AFMsgPackResponseSerializer` is a subclass of `AFHTTPResponseSerializer` that validates and decodes MsgPack responses. 28 | 29 | By default, `AFMsgPackSerializer` accepts the following MIME types: 30 | 31 | - `application/x-msgpack` 32 | */ 33 | @interface AFMsgPackResponseSerializer : AFHTTPResponseSerializer 34 | 35 | /** 36 | Options for reading the response MsgPack data and creating the Foundation objects. For possible values, see the `MsgPackSerialization` documentation section "MsgPackReadingOptions". `0` by default. 37 | */ 38 | @property (nonatomic, assign) MsgPackReadingOptions readingOptions; 39 | 40 | /** 41 | Creates and returns a MsgPack response serializer with specified reading options. 42 | 43 | @param readingOptions The specified MsgPack reading options. 44 | */ 45 | + (instancetype)serializerWithReadingOptions:(MsgPackReadingOptions)readingOptions; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /AFMsgPackSerialization/AFMsgPackResponseSerializer.m: -------------------------------------------------------------------------------- 1 | // AFMsgPackResponseSerializer.m 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFMsgPackResponseSerializer.h" 24 | 25 | @implementation AFMsgPackResponseSerializer 26 | 27 | + (instancetype)serializer { 28 | return [self serializerWithReadingOptions:0]; 29 | } 30 | 31 | + (instancetype)serializerWithReadingOptions:(MsgPackReadingOptions)readingOptions { 32 | AFMsgPackResponseSerializer *serializer = [[self alloc] init]; 33 | serializer.readingOptions = readingOptions; 34 | 35 | return serializer; 36 | } 37 | 38 | - (instancetype)init { 39 | self = [super init]; 40 | if (!self) { 41 | return nil; 42 | } 43 | 44 | self.acceptableContentTypes = [NSSet setWithObjects:@"application/x-msgpack", nil]; 45 | 46 | return self; 47 | } 48 | 49 | #pragma mark - AFURLResponseSerializer 50 | 51 | - (id)responseObjectForResponse:(NSURLResponse *)response 52 | data:(NSData *)data 53 | error:(NSError *__autoreleasing *)error 54 | { 55 | if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) { 56 | return nil; 57 | } 58 | 59 | return [MsgPackSerialization MsgPackObjectWithData:data options:self.readingOptions error:error]; 60 | } 61 | 62 | #pragma mark - NSCoding 63 | 64 | - (id)initWithCoder:(NSCoder *)decoder { 65 | self = [super initWithCoder:decoder]; 66 | if (!self) { 67 | return nil; 68 | } 69 | 70 | self.readingOptions = (MsgPackReadingOptions)[[decoder decodeObjectForKey:NSStringFromSelector(@selector(readingOptions))] unsignedIntegerValue]; 71 | 72 | return self; 73 | } 74 | 75 | - (void)encodeWithCoder:(NSCoder *)coder { 76 | [super encodeWithCoder:coder]; 77 | 78 | [coder encodeObject:@(self.readingOptions) forKey:NSStringFromSelector(@selector(readingOptions))]; 79 | } 80 | 81 | #pragma mark - NSCopying 82 | 83 | - (id)copyWithZone:(NSZone *)zone { 84 | AFMsgPackResponseSerializer *serializer = [[[self class] allocWithZone:zone] init]; 85 | serializer.readingOptions = self.readingOptions; 86 | 87 | return serializer; 88 | } 89 | 90 | @end 91 | 92 | -------------------------------------------------------------------------------- /AFOnoResponseSerializer.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'AFMsgPackSerialization' 3 | s.version = '0.0.1' 4 | s.license = 'MIT' 5 | s.summary = 'A MsgPack request and response serializer for AFNetworking 2.0.' 6 | s.homepage = 'https://github.com/AFNetworking/AFMsgPackSerialization' 7 | s.social_media_url = 'https://twitter.com/afnetworking' 8 | s.authors = { 'Mattt Thompson' => 'm@mattt.me' } 9 | s.source = { :git => 'https://github.com/AFNetworking/AFMsgPackSerialization.git', :tag => '0.0.1' } 10 | s.source_files = 'AFMsgPackSerialization' 11 | s.requires_arc = true 12 | 13 | s.ios.deployment_target = '6.0' 14 | s.osx.deployment_target = '10.8' 15 | 16 | s.dependency 'AFNetworking', '~> 2.2' 17 | s.dependency 'MsgPackSerialization' 18 | end 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 AFNetworking (http://afnetworking.com) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AFMsgPackSerialization 2 | ====================== 3 | 4 | `AFMsgPackSerialization` is an AFNetworking 2 extension that provides request and response serializers to automatically encodes and decodes objects to and from the [MsgPack](http://msgpack.org) format. 5 | 6 | ## Usage 7 | 8 | ```objective-c 9 | // AFHTTPClient Configuration 10 | AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 11 | manager.responseSerializer = [AFMsgPackResponseSerializer serializer]; 12 | [manager GET:@"http://example.com/foo.msgpack" 13 | parameters:nil 14 | success:^(NSHTTPURLResponse *response, id responseObject) { 15 | // ... 16 | } 17 | failure:nil]; 18 | ``` 19 | 20 | --- 21 | 22 | ## Contact 23 | 24 | Mattt Thompson 25 | 26 | - http://github.com/mattt 27 | - http://twitter.com/mattt 28 | - m@mattt.me 29 | 30 | ## License 31 | 32 | AFMsgPackSerialization is available under the MIT license. See the LICENSE file for more info. 33 | --------------------------------------------------------------------------------