-
Notifications
You must be signed in to change notification settings - Fork 130
/
BSGJSONSerialization.m
95 lines (83 loc) · 3.07 KB
/
BSGJSONSerialization.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// BSGJSONSerialization.m
// Bugsnag
//
// Created by Karl Stenerud on 03.09.20.
// Copyright © 2020 Bugsnag Inc. All rights reserved.
//
#import "BSGJSONSerialization.h"
#import "BugsnagLogger.h"
@implementation BSGJSONSerialization
static NSError* wrapException(NSException* exception) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: exception.description,
NSUnderlyingErrorKey: exception,
};
return [NSError errorWithDomain:@"BSGJSONSerializationErrorDomain" code:1 userInfo:userInfo];
}
+ (BOOL)isValidJSONObject:(id)obj {
@try {
return [NSJSONSerialization isValidJSONObject:obj];
} @catch (NSException *exception) {
return NO;
}
}
+ (nullable NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error {
@try {
return [NSJSONSerialization dataWithJSONObject:obj options:opt error:error];
} @catch (NSException *exception) {
if (error) {
*error = wrapException(exception);
}
return nil;
}
}
+ (nullable id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error {
@try {
return [NSJSONSerialization JSONObjectWithData:data options:opt error:error];
} @catch (NSException *exception) {
if (error) {
*error = wrapException(exception);
}
return nil;
}
}
+ (NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(NSJSONWritingOptions)opt error:(NSError **)error {
@try {
return [NSJSONSerialization writeJSONObject:obj toStream:stream options:opt error:error];
} @catch (NSException *exception) {
if (error) {
*error = wrapException(exception);
}
return 0;
}
}
+ (nullable id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error {
@try {
return [NSJSONSerialization JSONObjectWithStream:stream options:opt error:error];
} @catch (NSException *exception) {
if (error) {
*error = wrapException(exception);
}
return nil;
}
}
+ (BOOL)writeJSONObject:(id)JSONObject toFile:(NSString *)file options:(NSJSONWritingOptions)options error:(NSError **)errorPtr {
if (![BSGJSONSerialization isValidJSONObject:JSONObject]) {
if (errorPtr) {
*errorPtr = [NSError errorWithDomain:@"BSGJSONSerializationErrorDomain" code:0 userInfo:@{
NSLocalizedDescriptionKey: @"Not a valid JSON object"}];
}
return NO;
}
NSData *data = [BSGJSONSerialization dataWithJSONObject:JSONObject options:options error:errorPtr];
return [data writeToFile:file options:NSDataWritingAtomic error:errorPtr];
}
+ (nullable id)JSONObjectWithContentsOfFile:(NSString *)file options:(NSJSONReadingOptions)options error:(NSError **)errorPtr {
NSData *data = [NSData dataWithContentsOfFile:file options:0 error:errorPtr];
if (!data) {
return nil;
}
return [BSGJSONSerialization JSONObjectWithData:data options:options error:errorPtr];
}
@end