-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathJSONDeeplyMutable.m
More file actions
58 lines (47 loc) · 1.45 KB
/
Copy pathJSONDeeplyMutable.m
File metadata and controls
58 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// 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 throwsExceptions:(BOOL)throwsExceptions
{
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 && throwsExceptions)
{
[NSException raise:NSDestinationInvalidException format:@"Object is not mutable or copyable: %@", oldValue];
}
return newCopy;
}
+ (id)copyAsDeeplyImmutableValue:(id)oldValue throwsExceptions:(BOOL)throwsExceptions
{
id newCopy = nil;
if ([oldValue respondsToSelector: @selector(copyAsDeeplyImmutableJSONWithExceptions:)])
{
newCopy = [oldValue copyAsDeeplyImmutableJSONWithExceptions:throwsExceptions];
}
else if ([oldValue conformsToProtocol:@protocol(NSCopying)])
{
newCopy = [oldValue copy];
}
if (!newCopy && throwsExceptions)
{
[NSException raise:NSDestinationInvalidException format:@"Object is not copyable: %@", oldValue];
}
return newCopy;
}
@end