Skip to content

Commit

Permalink
Merge pull request #351 from shaneomack91/patch-1
Browse files Browse the repository at this point in the history
Prevent a memory leak when converting to JSON
  • Loading branch information
purplecabbage authored Jan 18, 2018
2 parents 4f873f7 + e4c162b commit c88fbbf
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions CordovaLib/Classes/Private/CDVJSON_private.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ @implementation NSArray (CDVJSONSerializingPrivate)

- (NSString*)cdv_JSONString
{
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:0
error:&error];

if (error != nil) {
NSLog(@"NSArray JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
@autoreleasepool {
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:0
error:&error];

if (error != nil) {
NSLog(@"NSArray JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
}

Expand All @@ -43,16 +45,18 @@ @implementation NSDictionary (CDVJSONSerializingPrivate)

- (NSString*)cdv_JSONString
{
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:&error];

if (error != nil) {
NSLog(@"NSDictionary JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
@autoreleasepool {
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:self
options:NSJSONWritingPrettyPrinted
error:&error];

if (error != nil) {
NSLog(@"NSDictionary JSONString error: %@", [error localizedDescription]);
return nil;
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
}
}

Expand All @@ -62,30 +66,34 @@ @implementation NSString (CDVJSONSerializingPrivate)

- (id)cdv_JSONObject
{
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@, Malformed Data: %@", [error localizedDescription], self);
}
@autoreleasepool {
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@, Malformed Data: %@", [error localizedDescription], self);
}

return object;
return object;
}
}

- (id)cdv_JSONFragment
{
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingAllowFragments
error:&error];
@autoreleasepool {
NSError* error = nil;
id object = [NSJSONSerialization JSONObjectWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingAllowFragments
error:&error];

if (error != nil) {
NSLog(@"NSString JSONObject error: %@", [error localizedDescription]);
}
if (error != nil) {
NSLog(@"NSString JSONObject error: %@", [error localizedDescription]);
}

return object;
return object;
}
}

@end

0 comments on commit c88fbbf

Please sign in to comment.