[ReactNative] allowLossyConversion on NSString->NSData conversion

This commit is contained in:
Andrew Rasmussen 2015-03-09 16:58:39 -07:00
parent b31fd38bb6
commit 320429e355
1 changed files with 19 additions and 8 deletions

View File

@ -2,11 +2,15 @@
#import "RCTUtils.h"
#import <CommonCrypto/CommonCrypto.h>
#import <mach/mach_time.h>
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import <CommonCrypto/CommonCrypto.h>
#import "RCTLog.h"
NSString *RCTJSONStringify(id jsonObject, NSError **error)
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:error];
@ -15,7 +19,14 @@ NSString *RCTJSONStringify(id jsonObject, NSError **error)
id RCTJSONParse(NSString *jsonString, NSError **error)
{
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
if (!jsonString) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
if (!jsonData) {
RCTLog(@"RCTJSONParse received the following string, which could not be losslessly converted to UTF8 data: '%@'", jsonString);
jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
}
return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:error];
}