Skip to content

Commit

Permalink
Optionally ignore exceptions with copyAsDeeplyMutable
Browse files Browse the repository at this point in the history
  • Loading branch information
grgcombs committed Oct 1, 2014
1 parent 4796edf commit a0d835a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
4 changes: 2 additions & 2 deletions JSONTools.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "JSONTools"
s.version = "1.0.1"
s.version = "1.0.2"
s.summary = "JSON Patch, JSON Pointer, and JSON Schema Validation in Objective-C"
s.description = <<-DESC
This Objective-C library is a collection of classes and categories that implement
Expand All @@ -15,7 +15,7 @@ Pod::Spec.new do |s|

s.source = { :git => "https://github.com/grgcombs/JSONTools.git", :tag => "v#{s.version}" }
s.source_files = "JSONTools/*.{h,m}"
s.dependency "KiteJSONValidator", '~> 0.1.1-Pod'
s.dependency "KiteJSONValidator", '~> 0.1.2-Pod'

s.xcconfig = {
'ONLY_ACTIVE_ARCH' => 'NO'
Expand Down
6 changes: 5 additions & 1 deletion JSONTools/JSONDeeplyMutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
*
* @param oldValue A value object to (mutably) copy.
*
* @param throwsExceptions Conditionally throw exceptions if an interior
* object isn't mutable or copyable, otherwise it
* merely omits that object from the new collection.
*
* @return A (mutable) copy of the object.
*/
+ (id)copyAsDeeplyMutableValue:(id)oldValue;
+ (id)copyAsDeeplyMutableValue:(id)oldValue throwsExceptions:(BOOL)throwsExceptions;

@end
4 changes: 2 additions & 2 deletions JSONTools/JSONDeeplyMutable.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@implementation JSONDeepMutable

+ (id)copyAsDeeplyMutableValue:(id)oldValue
+ (id)copyAsDeeplyMutableValue:(id)oldValue throwsExceptions:(BOOL)throwsExceptions
{
id newCopy = nil;

Expand All @@ -26,7 +26,7 @@ + (id)copyAsDeeplyMutableValue:(id)oldValue
newCopy = [oldValue copy];
}

if (!newCopy)
if (!newCopy && throwsExceptions)
{
[NSException raise:NSDestinationInvalidException format:@"Object is not mutable or copyable: %@", oldValue];
}
Expand Down
12 changes: 12 additions & 0 deletions JSONTools/NSArray+JSONDeepMutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
*/
- (NSMutableArray *)copyAsDeeplyMutableJSON;

/**
* 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.
*
* @param throwsExceptions Conditionally throw exceptions if an interior object isn't mutable or copyable,
* otherwise it merely omits that object from the new collection.
*
* @return A deeply mutable copy of the receiver's contents.
*/
- (NSMutableArray *)copyAsDeeplyMutableJSONWithExceptions:(BOOL)throwsExceptions;

@end
9 changes: 7 additions & 2 deletions JSONTools/NSArray+JSONDeepMutable.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ @implementation NSArray (JSONDeepMutable)

- (NSMutableArray *)copyAsDeeplyMutableJSON
{
NSMutableArray* ret = [[NSMutableArray alloc] initWithCapacity: [self count]];
return [self copyAsDeeplyMutableJSONWithExceptions:YES];
}

- (NSMutableArray *)copyAsDeeplyMutableJSONWithExceptions:(BOOL)throwsExceptions
{
NSMutableArray* ret = [[NSMutableArray alloc] init];
for (id oldValue in self)
{
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:oldValue];
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:oldValue throwsExceptions:throwsExceptions];
if (newCopy)
{
[ret addObject:newCopy];
Expand Down
12 changes: 12 additions & 0 deletions JSONTools/NSDictionary+JSONDeepMutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@
*/
- (NSMutableDictionary *)copyAsDeeplyMutableJSON;

/**
* 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.
*
* @param throwsExceptions Conditionally throw exceptions if an interior object isn't mutable or copyable,
* otherwise it merely omits that object from the new collection.
*
* @return A deeply mutable copy of the receiver's contents.
*/
- (NSMutableDictionary *)copyAsDeeplyMutableJSONWithExceptions:(BOOL)throwsExceptions;

@end
9 changes: 7 additions & 2 deletions JSONTools/NSDictionary+JSONDeepMutable.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ @implementation NSDictionary (JSONDeepMutable)

- (NSMutableDictionary *)copyAsDeeplyMutableJSON
{
NSMutableDictionary * ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]];
return [self copyAsDeeplyMutableJSONWithExceptions:YES];
}

- (NSMutableDictionary *)copyAsDeeplyMutableJSONWithExceptions:(BOOL)throwsExceptions
{
NSMutableDictionary * ret = [[NSMutableDictionary alloc] init];

[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:obj];
id newCopy = [JSONDeepMutable copyAsDeeplyMutableValue:obj throwsExceptions:throwsExceptions];
if (newCopy)
{
ret[key] = newCopy;
Expand Down
30 changes: 23 additions & 7 deletions JSONToolsTests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
153C3D9B7DE64C5291E571F5 /* Pods-JSONToolsTests.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JSONToolsTests.xcconfig"; path = "Pods/Pods-JSONToolsTests.xcconfig"; sourceTree = "<group>"; };
2A04D7DA4D577E304B768C24 /* Pods-JSONToolsTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JSONToolsTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JSONToolsTests/Pods-JSONToolsTests.debug.xcconfig"; sourceTree = "<group>"; };
37A5004E18FC3FC700A15B0F /* JSONPatchApplyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONPatchApplyTests.m; sourceTree = "<group>"; };
37A5005218FC9E0200A15B0F /* JSONPatchGenerateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONPatchGenerateTests.m; sourceTree = "<group>"; };
37B7C5F919DCB7CD00395110 /* Pods.release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
37B7C5FA19DCB7E000395110 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
37BDF4C018FAF1DD00BF9705 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
37BDF4CD18FAF1DD00BF9705 /* JSONToolsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JSONToolsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
37BDF4CE18FAF1DD00BF9705 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
Expand All @@ -32,8 +34,10 @@
37BDF4DC18FAF1DD00BF9705 /* JSONPointerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JSONPointerTests.m; sourceTree = "<group>"; };
37C8BA2C19106B74004D3E23 /* JSONSchemaTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONSchemaTests.m; sourceTree = "<group>"; };
39FDE358F51848868E4334BA /* libPods-JSONToolsTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-JSONToolsTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
83997C626DB14CA4A0659824 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = "<group>"; };
8A736BF437437DE4CA84B315 /* Pods-JSONToolsTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JSONToolsTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-JSONToolsTests/Pods-JSONToolsTests.release.xcconfig"; sourceTree = "<group>"; };
8BAA2AFED5A38753C8438FB8 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
B35914385F39418EB4099917 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
EA415B43DA52B212F752F99D /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -52,14 +56,26 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
2F23DB1222AFCB8158F59DB7 /* Pods */ = {
isa = PBXGroup;
children = (
8BAA2AFED5A38753C8438FB8 /* Pods.debug.xcconfig */,
EA415B43DA52B212F752F99D /* Pods.release.xcconfig */,
2A04D7DA4D577E304B768C24 /* Pods-JSONToolsTests.debug.xcconfig */,
8A736BF437437DE4CA84B315 /* Pods-JSONToolsTests.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
};
37BDF4B418FAF1DD00BF9705 = {
isa = PBXGroup;
children = (
37BDF4D618FAF1DD00BF9705 /* JSONToolsTests */,
37BDF4BF18FAF1DD00BF9705 /* Frameworks */,
37BDF4BE18FAF1DD00BF9705 /* Products */,
83997C626DB14CA4A0659824 /* Pods.xcconfig */,
153C3D9B7DE64C5291E571F5 /* Pods-JSONToolsTests.xcconfig */,
37B7C5F919DCB7CD00395110 /* Pods.release.xcconfig */,
37B7C5FA19DCB7E000395110 /* Pods.debug.xcconfig */,
2F23DB1222AFCB8158F59DB7 /* Pods */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -177,7 +193,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Pods-JSONToolsTests-resources.sh\"\n";
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-JSONToolsTests/Pods-JSONToolsTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
E5FDDCD39D4A4E08B2DE1D8B /* Check Pods Manifest.lock */ = {
Expand Down Expand Up @@ -299,7 +315,7 @@
};
37BDF4E418FAF1DD00BF9705 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 153C3D9B7DE64C5291E571F5 /* Pods-JSONToolsTests.xcconfig */;
baseConfigurationReference = 2A04D7DA4D577E304B768C24 /* Pods-JSONToolsTests.debug.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
Expand All @@ -317,7 +333,7 @@
};
37BDF4E518FAF1DD00BF9705 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 153C3D9B7DE64C5291E571F5 /* Pods-JSONToolsTests.xcconfig */;
baseConfigurationReference = 8A736BF437437DE4CA84B315 /* Pods-JSONToolsTests.release.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ xcodeproj 'JSONToolsTests.xcodeproj'
platform :ios, '7.0'
pod "JSONTools", path: '.'

target :JSONToolsTests, :exclusive => true do
target :JSONToolsTests, :exclusive => false do
pod 'JSON-Schema-Test-Suite'
pod 'KiteJSONValidator/KiteJSONResources'
end
Expand Down

0 comments on commit a0d835a

Please sign in to comment.