-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSDictionary+SSYJsonFile.m
49 lines (41 loc) · 1.55 KB
/
NSDictionary+SSYJsonFile.m
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
#import "NSDictionary+SSYJsonFile.h"
#import "NSError+InfoAccess.h"
#import "NSDictionary+BSJSONAdditions.h"
#import "NSError+MyDomain.h"
@implementation NSDictionary (SSYJsonFile)
+ (NSDictionary*)dictionaryFromJsonAtPath:(NSString*)path
error_p:(NSError**)error_p {
BOOL ok = YES ;
NSError* error = nil ;
NSDictionary* dic = nil ;
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSString* prefsString = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error] ;
if (!prefsString) {
ok = NO ;
error = [SSYMakeError(252391, @"Could not read file") errorByAddingUnderlyingError:error] ;
}
if (ok) {
dic = [NSDictionary dictionaryWithJSONString:prefsString
accurately:NO] ;
if (![dic isKindOfClass:[NSDictionary class]]) {
ok = NO ;
error = [SSYMakeError(252392, @"Could not decode JSON in file") errorByAddingUnderlyingError:error] ;
}
}
}
else {
dic = [NSDictionary dictionary] ;
}
if (!ok) {
if (error && error_p) {
error = [error errorByAddingUserInfoObject:path
forKey:@"Path"] ;
*error_p = error ;
dic = nil ;
}
}
return dic ;
}
@end