Skip to content

Commit

Permalink
Add predicate to filter the properties when serializing to JSON strin…
Browse files Browse the repository at this point in the history
…g/data.
  • Loading branch information
Xenofex committed Dec 20, 2013
1 parent 13eaec4 commit fe976d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
10 changes: 7 additions & 3 deletions YAJL-Entity/NSObject+YAJLizable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@

extern int EWPropNameUpperCaseFirstChar;

typedef enum __EWPropDictOption {
typedef NS_OPTIONS(NSUInteger, EWPropDictOption) {
EWPropDictOptionNone = 0,
EWPropDictSnakecase = 1
} EWPropDictOption;
EWPropDictSnakecase = 1 << 1
};

@interface NSObject(YAJLizable)

- (NSDictionary *) newDictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate option:(EWPropDictOption)option;
- (NSDictionary *) dictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate option:(EWPropDictOption)option;


- (NSDictionary *) newDictionaryOfPropertiesWithOption:(EWPropDictOption)option;
- (NSDictionary *) dictionaryOfPropertiesWithOption:(EWPropDictOption)option;

Expand Down
34 changes: 25 additions & 9 deletions YAJL-Entity/NSObject+YAJLizable.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@

@interface NSObject(YAJLizablePrivate)

- (NSMutableDictionary *)newDictionaryOfPropertiesAsClass:(Class) aClass option:(EWPropDictOption)option;
- (NSMutableDictionary *)newDictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate asClass:(Class) aClass option:(EWPropDictOption)option;

@end

@implementation NSObject(YAJLizable)

- (NSDictionary *)newDictionaryOfPropertiesWithOption:(EWPropDictOption)option

- (NSDictionary *)newDictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate option:(EWPropDictOption)option
{
Class superClass = [self class];
NSMutableDictionary *dict = [self newDictionaryOfPropertiesAsClass:[self class] option:option];

NSMutableDictionary *dict = [self newDictionaryOfPropertiesWithPredicate:predicate asClass:[self class] option:option];

while ((superClass = [superClass superclass])) {
// TEST if the current superClass is NSObject. If it's true, break;
if ([superClass superclass] != nil) {
NSMutableDictionary *superDict = [self newDictionaryOfPropertiesAsClass:superClass option:option];
NSMutableDictionary *superDict = [self newDictionaryOfPropertiesWithPredicate:predicate asClass:superClass option:option];
[dict addEntriesFromDictionary:superDict];
[superDict release];
} else {
Expand All @@ -57,6 +57,16 @@ - (NSDictionary *)newDictionaryOfPropertiesWithOption:(EWPropDictOption)option
return ret;
}

- (NSDictionary *)dictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate option:(EWPropDictOption)option
{
return [[self newDictionaryOfPropertiesWithPredicate:predicate option:option] autorelease];
}

- (NSDictionary *)newDictionaryOfPropertiesWithOption:(EWPropDictOption)option
{
return [self newDictionaryOfPropertiesWithPredicate:nil option:option];
}

- (NSDictionary *)dictionaryOfPropertiesWithOption:(EWPropDictOption)option
{
return [[self newDictionaryOfPropertiesWithOption:option] autorelease];
Expand All @@ -77,19 +87,25 @@ - (id)initForYAJL
return [self init];
}

- (NSMutableDictionary *)newDictionaryOfPropertiesAsClass:(Class)aClass option:(EWPropDictOption)option
- (NSMutableDictionary *)newDictionaryOfPropertiesWithPredicate:(NSPredicate *)predicate asClass:(Class)aClass option:(EWPropDictOption)option
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSArray *propertyNames = [aClass propertyNames];
if (predicate) {
propertyNames = [propertyNames filteredArrayUsingPredicate:predicate];
}

NSString *propName;
NSString *dictKey = nil;
for (propName in propertyNames) {
if (option & EWPropDictSnakecase)
if (option & EWPropDictSnakecase) {
dictKey = [propName snakecaseString];
else if (EWPropNameUpperCaseFirstChar)
} else if (EWPropNameUpperCaseFirstChar) {
dictKey = [propName stringByUppercaseFirstChar];
else
} else {
dictKey = propName;
}

[dic setValue:[self valueForKey:propName] forKey:dictKey];
}

Expand Down

0 comments on commit fe976d3

Please sign in to comment.