File tree Expand file tree Collapse file tree 3 files changed +40
-10
lines changed
Expand file tree Collapse file tree 3 files changed +40
-10
lines changed Original file line number Diff line number Diff line change @@ -61,12 +61,8 @@ + (instancetype)controllerWithFileManager:(PFFileManager *)fileManager {
6161- (BFTask *)getCurrentConfigAsync {
6262 return [BFTask taskFromExecutor: _dataExecutor withBlock: ^id {
6363 if (!_currentConfig) {
64- NSError *error = nil ;
65- NSData *jsonData = [NSData dataWithContentsOfFile: self .configFilePath
66- options: NSDataReadingMappedIfSafe
67- error: &error];
68- if (error == nil && [jsonData length ] != 0 ) {
69- NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromData: jsonData];
64+ NSDictionary *dictionary = [PFJSONSerialization JSONObjectFromFileAtPath: self .configFilePath];
65+ if (dictionary) {
7066 NSDictionary *decodedDictionary = [[PFDecoder objectDecoder ] decodeObject: dictionary];
7167 _currentConfig = [[PFConfig alloc ] initWithFetchedConfig: decodedDictionary];
7268 } else {
Original file line number Diff line number Diff line change 99
1010#import < Foundation/Foundation.h>
1111
12+ NS_ASSUME_NONNULL_BEGIN
13+
1214@interface PFJSONSerialization : NSObject
1315
1416/* !
2123
2224 @returns NSData of JSON representing the passed in object.
2325 */
24- + (NSData *)dataFromJSONObject : (id )object ;
26+ + (nullable NSData *)dataFromJSONObject : (id )object ;
2527
2628/* !
2729 The object passed in must be one of:
3335
3436 @returns NSString of JSON representing the passed in object.
3537 */
36- + (NSString *)stringFromJSONObject : (id )object ;
38+ + (nullable NSString *)stringFromJSONObject : (id )object ;
3739
3840/* !
3941 Takes a JSON string and returns the NSDictionaries and NSArrays in it.
4042 You should still call decodeObject if you want Parse types.
4143 */
42- + (id )JSONObjectFromData : (NSData *)data ;
44+ + (nullable id )JSONObjectFromData : (NSData *)data ;
4345
4446/* !
4547 Takes a JSON string and returns the NSDictionaries and NSArrays in it.
4648 You should still call decodeObject if you want Parse types.
4749 */
48- + (id )JSONObjectFromString : (NSString *)string ;
50+ + (nullable id )JSONObjectFromString : (NSString *)string ;
51+
52+ /* !
53+ @abstract Takes a file path to json file and returns the NSDictionaries and NSArrays in it.
54+
55+ @description You should still call decodeObject if you want Parse types.
56+
57+ @param filePath File path to a file.
58+
59+ @return Decoded object.
60+ */
61+ + (nullable id )JSONObjectFromFileAtPath : (NSString *)filePath ;
4962
5063@end
64+
65+ NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change @@ -43,4 +43,23 @@ + (id)JSONObjectFromString:(NSString *)string {
4343 return [self JSONObjectFromData: [string dataUsingEncoding: NSUTF8StringEncoding]];
4444}
4545
46+ + (id )JSONObjectFromFileAtPath : (NSString *)filePath {
47+ NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath: filePath];
48+ if (!stream) {
49+ return nil ;
50+ }
51+
52+ [stream open ];
53+
54+ NSError *error = nil ;
55+ id object = [NSJSONSerialization JSONObjectWithStream: stream options: 0 error: &error];
56+ if (!object || error) {
57+ PFLogError (PFLoggingTagCommon, @" JSON deserialization failed with error: %@ " , error.description );
58+ }
59+
60+ [stream close ];
61+
62+ return object;
63+ }
64+
4665@end
You can’t perform that action at this time.
0 commit comments