Skip to content

Commit

Permalink
Catch and report unexpected exceptions when (de)serializing JSON data…
Browse files Browse the repository at this point in the history
… rather than crashing.
  • Loading branch information
kstenerud committed Oct 22, 2020
1 parent 7241891 commit 24d6b09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "BSG_KSJSONCodec.h"
#import "BSG_RFC3339DateTool.h"
#import "NSError+BSG_SimpleConstructor.h"
#import "BSG_KSLogger.h"

@interface BSG_KSJSONCodec ()

Expand Down Expand Up @@ -487,20 +488,28 @@ int bsg_ksjsoncodecobjc_i_encodeObject(BSG_KSJSONCodec *codec, id object,
+ (NSData *)encode:(id)object
options:(BSG_KSJSONEncodeOption)encodeOptions
error:(NSError *__autoreleasing *)error {
NSMutableData *data = [NSMutableData data];
BSG_KSJSONEncodeContext JSONContext;
bsg_ksjsonbeginEncode(
&JSONContext, encodeOptions & BSG_KSJSONEncodeOptionPretty,
bsg_ksjsoncodecobjc_i_addJSONData, (__bridge void *)data);
BSG_KSJSONCodec *codec =
[self codecWithEncodeOptions:encodeOptions decodeOptions:0];

int result =
bsg_ksjsoncodecobjc_i_encodeObject(codec, object, NULL, &JSONContext);
if (error != nil) {
*error = codec.error;
@try {
NSMutableData *data = [NSMutableData data];
BSG_KSJSONEncodeContext JSONContext;
bsg_ksjsonbeginEncode(
&JSONContext, encodeOptions & BSG_KSJSONEncodeOptionPretty,
bsg_ksjsoncodecobjc_i_addJSONData, (__bridge void *)data);
BSG_KSJSONCodec *codec =
[self codecWithEncodeOptions:encodeOptions decodeOptions:0];

int result =
bsg_ksjsoncodecobjc_i_encodeObject(codec, object, NULL, &JSONContext);
if (error != nil) {
*error = codec.error;
}
return result == BSG_KSJSON_OK ? data : nil;
} @catch (NSException *exception) {
BSG_KSLOG_ERROR(@"Could not encode JSON object: %@", exception.description);
if (error != nil) {
*error = [NSError bsg_errorWithDomain:@"KSJSONCodecObjC" code:0 description:exception.description];
}
return nil;
}
return result == BSG_KSJSON_OK ? data : nil;
}

+ (id)decode:(NSData *)JSONData
Expand All @@ -511,6 +520,10 @@ + (id)decode:(NSData *)JSONData
@try {
result = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:error];
} @catch (NSException *exception) {
BSG_KSLOG_ERROR(@"Could not decode JSON object: %@", exception.description);
if (error != nil) {
*error = [NSError bsg_errorWithDomain:@"KSJSONCodecObjC" code:0 description:exception.description];
}
result = @{};
}
return result;
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

## TBD

## Bug fixes

* Catch and report unexpected exceptions when (de)serializing JSON data rather
than crashing.
[856](https://github.com/bugsnag/bugsnag-cocoa/pull/856)

## 6.2.2 (2020-10-21)

## Enhancements
Expand Down

0 comments on commit 24d6b09

Please sign in to comment.