-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added convenience categories to create deeply mutable copies of colle…
…ctions. This is very useful when applying patches to (non-mutable) collections.
- Loading branch information
Showing
8 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// JSONDeeplyMutable.h | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
@import Foundation; | ||
#import "NSArray+JSONDeepMutable.h" | ||
#import "NSDictionary+JSONDeepMutable.h" | ||
|
||
@interface JSONDeepMutable : NSObject | ||
|
||
/** | ||
* @private | ||
* Use the NSArray+JSONDeepMutable and NSDictionary+JSONDeepMutable | ||
* categories instead. This is an intern implementation that only operates | ||
* on one instance of a container's content. Throws an exception if any | ||
* interior value objects aren't copyable in some way. This method | ||
* prefers NSMutableCopying over NSCopying whenever possible. | ||
* | ||
* @param oldValue A value object to (mutably) copy. | ||
* | ||
* @return A (mutable) copy of the object. | ||
*/ | ||
+ (id)copyAsDeeplyMutableValue:(id)oldValue; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// JSONDeeplyMutable.m | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
#import "JSONDeeplyMutable.h" | ||
|
||
@implementation JSONDeepMutable | ||
|
||
+ (id)copyAsDeeplyMutableValue:(id)oldValue | ||
{ | ||
id newCopy = nil; | ||
|
||
if ([oldValue respondsToSelector: @selector(copyAsDeeplyMutableJSON)]) | ||
{ | ||
newCopy = [oldValue copyAsDeeplyMutableJSON]; | ||
} | ||
else if ([oldValue conformsToProtocol:@protocol(NSMutableCopying)]) | ||
{ | ||
newCopy = [oldValue mutableCopy]; | ||
} | ||
else if ([oldValue conformsToProtocol:@protocol(NSCopying)]) | ||
{ | ||
newCopy = [oldValue copy]; | ||
} | ||
|
||
if (!newCopy) | ||
{ | ||
[NSException raise:NSDestinationInvalidException format:@"Object is not mutable or copyable: %@", oldValue]; | ||
} | ||
|
||
return newCopy; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// NSArray+JSONDeepMutable.h | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
@import Foundation; | ||
|
||
@interface NSArray (JSONDeepMutable) | ||
|
||
/** | ||
* Recurses into the receiver's contents and makes a (mutable) copy of each value it encounters. | ||
* Throws an exception if any interior value objects aren't copyable in some way. This method | ||
* prefers NSMutableCopying over NSCopying whenever possible. | ||
* | ||
* @return A deeply mutable copy of the receiver's contents. | ||
*/ | ||
- (NSMutableArray *)copyAsDeeplyMutableJSON; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// NSArray+JSONDeepMutable.m | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
#import "NSArray+JSONDeepMutable.h" | ||
#import "JSONDeeplyMutable.h" | ||
|
||
@implementation NSArray (JSONDeepMutable) | ||
|
||
- (NSMutableArray *)copyAsDeeplyMutableJSON | ||
{ | ||
NSMutableArray* ret = [[NSMutableArray alloc] initWithCapacity: [self count]]; | ||
for (id oldValue in self) | ||
{ | ||
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:oldValue]; | ||
if (newCopy) | ||
{ | ||
[ret addObject:newCopy]; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// NSDictionary+JSONDeepMutable.h | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
@import Foundation; | ||
|
||
@interface NSDictionary (JSONDeepMutable) | ||
|
||
/** | ||
* Recurses into the receiver's contents and makes a (mutable) copy of each value it encounters. | ||
* Throws an exception if any interior value objects aren't copyable in some way. This method | ||
* prefers NSMutableCopying over NSCopying whenever possible. | ||
* | ||
* @return A deeply mutable copy of the receiver's contents. | ||
*/ | ||
- (NSMutableDictionary *)copyAsDeeplyMutableJSON; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// NSDictionary+JSONDeepMutable.m | ||
// JSONTools | ||
// | ||
// Copyright (C) 2014 Gregory Combs [gcombs at gmail] | ||
// See LICENSE.txt for details. | ||
|
||
#import "NSDictionary+JSONDeepMutable.h" | ||
#import "JSONDeeplyMutable.h" | ||
|
||
@implementation NSDictionary (JSONDeepMutable) | ||
|
||
- (NSMutableDictionary *)copyAsDeeplyMutableJSON | ||
{ | ||
NSMutableDictionary * ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]]; | ||
|
||
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | ||
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:obj]; | ||
if (newCopy) | ||
{ | ||
ret[key] = newCopy; | ||
} | ||
}]; | ||
|
||
return ret; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters