Skip to content
This repository was archived by the owner on Aug 26, 2021. It is now read-only.

vimeo/VIMObjectMapper

Repository files navigation

⚠️⚠️⚠️ This library has been deprecated and will be removed in the future. ⚠️⚠️⚠️

Attention: This tool will be merged into VimeoNetworking shortly. Please refer to that repository for future development. If you're only interested in object mapping, you can use the Models subspec of the VimeoNetworking CocoaPod

VIMObjectMapper

VIMObjectMapper converts JSON into model objects.

Setup

Add VIMObjectMapper to your project. Do this by including it as a git submodule or by using cocoapods:

# Add this to your podfile
target 'MyTarget' do
    pod 'VIMObjectMapper', '{CURRENT_POD_VERSION}'
end

Usage

Subclass VIMModelObject

Make your custom model object a subclass of VIMModelObject and optionally implement the VIMMappable protocol methods:

#import "VIMModelObject.h"

@class VIMPictureCollection;

@interface VIMUser : VIMModelObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) VIMPictureCollection *pictureCollection;
@property (nonatomic, strong) NSDictionary *uploadQuota;
@property (nonatomic, strong) NSArray *websites;

@end
#import "VIMUser.h"
#import "VIMPictureCollection.h"
#import "VIMObjectMapper.h"

@implementation VIMUser

#pragma mark - VIMMappable // All methods are optional, implement to specify how the object should be "inflated"

- (NSDictionary *)getObjectMapping
{
	return @{@"pictures": @"pictureCollection"};
}

- (Class)getClassForCollectionKey:(NSString *)key
{
    if ([key isEqualToString:@"uploadQuota"])
    {
        return [NSDictionary class];
    }
    
    if ([key isEqualToString:@"websites"])
    {
        return [NSArray class];
    }

    return nil;
}

- (Class)getClassForObjectKey:(NSString *)key
{
    if ([key isEqualToString:@"pictures"])
    {
        return [VIMPictureCollection class];
    }
    
    return nil;
}

- (void)didFinishMapping
{
    // Do any post-parsing work you might want to do
}

Get some JSON

{
    user = {
        name = "Homer Simpson";
        pictures = {
            uri = "...";
            sizes = (...);
        };
        "upload_quota" = { ... };
        websites = ( ... );
    };
}

Let VIMObjectMapper go to work

NSDictionary *JSON = ...;

VIMObjectMapper *mapper = [[VIMObjectMapper alloc] init];

[mapper addMappingClass:[VIMUser class] forKeypath:@"user"];

VIMUser *user = [mapper applyMappingToJSON:JSON];

Found an Issue?

Please file it in the git issue tracker.

Want to Contribute?

If you'd like to contribute, please follow our guidelines found in CONTRIBUTING.md.

License

VIMObjectMapper is available under the MIT license. See the LICENSE file for more info.

Questions?

Tweet at us here: @vimeoapi.

Post on Stackoverflow with the tag vimeo-ios.

Get in touch here.

Interested in working at Vimeo? We're hiring!

About

An automatic JSON to model object converter

Resources

License

Contributing

Stars

9 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors