Skip to content

Commit

Permalink
migrate util functions to Basics
Browse files Browse the repository at this point in the history
Summary: As part of the FB & AN SDK Consolidation work, this diff is to migrate util function to Basics

Reviewed By: KylinChang

Differential Revision: D15216479

fbshipit-source-id: 4cdf623948e39526f8d9a13157bc8e55cd616b16
  • Loading branch information
tianqibt authored and facebook-github-bot committed May 7, 2019
1 parent e299c1a commit 6d0b5aa
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 39 deletions.
Expand Up @@ -312,7 +312,7 @@ + (NSString *)retrievePersistedAnonymousID
NSString *content = [[NSString alloc] initWithContentsOfFile:file
encoding:NSASCIIStringEncoding
error:nil];
NSDictionary *results = [FBSDKInternalUtility objectForJSONString:content error:NULL];
NSDictionary<id, id> *results = [FBSDKBasicUtility objectForJSONString:content error:NULL];
return results[FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY];
}

Expand Down
Expand Up @@ -203,7 +203,7 @@ - (void)instanceRestore:(BOOL)calledFromActivateApp

} else {

NSDictionary *results = [FBSDKInternalUtility objectForJSONString:content error:NULL];
NSDictionary<id, id> *results = [FBSDKBasicUtility objectForJSONString:content error:NULL];

_lastSuspendTime = [results[FBSDKTimeSpentPersistKeyLastSuspendTime] longValue];

Expand Down
2 changes: 1 addition & 1 deletion FBSDKCoreKit/FBSDKCoreKit/AppLink/FBSDKAppLinkUtility.m
Expand Up @@ -89,7 +89,7 @@ + (NSString *)appInvitePromotionCodeFromURL:(NSURL *)url;
// Parse deeplinkContext and extract promo code
if (deeplinkContextString.length > 0) {
NSError *error = nil;
NSDictionary *deeplinkContextData = [FBSDKInternalUtility objectForJSONString:deeplinkContextString error:&error];
NSDictionary<id, id> *deeplinkContextData = [FBSDKBasicUtility objectForJSONString:deeplinkContextString error:&error];
if (!error && [deeplinkContextData isKindOfClass:[NSDictionary class]]) {
return deeplinkContextData[@"promo_code"];
}
Expand Down
Expand Up @@ -72,6 +72,15 @@ setJSONStringForObject:(id)object
*/
+ (void)array:(NSMutableArray *)array addObject:(id)object;

/**
Converts a JSON string into an object
@param string The JSON string to convert.
@param errorRef If an error occurs, upon return contains an NSError object that describes the problem.
@return An NSDictionary, NSArray, NSString or NSNumber containing the object representation, or nil if the string
cannot be converted.
*/
+ (id)objectForJSONString:(NSString *)string error:(NSError *__autoreleasing *)errorRef;

@end

NS_ASSUME_NONNULL_END
Expand Up @@ -121,4 +121,16 @@ + (void)array:(NSMutableArray *)array addObject:(id)object
}
}

+ (id)objectForJSONString:(NSString *)string error:(NSError *__autoreleasing *)errorRef
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
if (!data) {
if (errorRef != NULL) {
*errorRef = nil;
}
return nil;
}
return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:errorRef];
}

@end
2 changes: 1 addition & 1 deletion FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.m
Expand Up @@ -290,7 +290,7 @@ - (void)_logIfAppLinkEvent:(NSURL *)url
return;
}

NSDictionary *applinkData = [FBSDKInternalUtility objectForJSONString:applinkDataString error:NULL];
NSDictionary<id, id> *applinkData = [FBSDKBasicUtility objectForJSONString:applinkDataString error:NULL];
if (!applinkData) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions FBSDKCoreKit/FBSDKCoreKit/FBSDKGraphRequestConnection.m
Expand Up @@ -724,7 +724,7 @@ - (id)parseJSONOrOtherwise:(NSString *)utf8
{
id parsed = nil;
if (!(*error) && [utf8 isKindOfClass:[NSString class]]) {
parsed = [FBSDKInternalUtility objectForJSONString:utf8 error:error];
parsed = [FBSDKBasicUtility objectForJSONString:utf8 error:error];
// if we fail parse we attempt a re-parse of a modified input to support results in the form "foo=bar", "true", etc.
// which is shouldn't be necessary since Graph API v2.1.
if (*error) {
Expand All @@ -734,7 +734,7 @@ - (id)parseJSONOrOtherwise:(NSString *)utf8
NSDictionary *original = @{ FBSDKNonJSONResponseProperty : utf8 };
NSString *jsonrep = [FBSDKBasicUtility JSONStringForObject:original error:NULL invalidObjectHandler:NULL];
NSError *reparseError = nil;
parsed = [FBSDKInternalUtility objectForJSONString:jsonrep error:&reparseError];
parsed = [FBSDKBasicUtility objectForJSONString:jsonrep error:&reparseError];
if (!reparseError) {
*error = nil;
}
Expand Down
Expand Up @@ -187,7 +187,7 @@ - (NSDictionary *)responseParametersForActionID:(NSString *)actionID
}
NSError *error;
NSString *bridgeParametersJSON = queryParameters[FBSDKBridgeAPIProtocolNativeV1InputKeys.bridgeArgs];
NSDictionary *bridgeParameters = [FBSDKInternalUtility objectForJSONString:bridgeParametersJSON error:&error];
NSDictionary<id, id> *bridgeParameters = [FBSDKBasicUtility objectForJSONString:bridgeParametersJSON error:&error];
bridgeParameters = [FBSDKTypeUtility dictionaryValue:bridgeParameters];
if (!bridgeParameters) {
if (error && (errorRef != NULL)) {
Expand All @@ -213,7 +213,7 @@ - (NSDictionary *)responseParametersForActionID:(NSString *)actionID
return nil;
}
NSString *resultParametersJSON = queryParameters[FBSDKBridgeAPIProtocolNativeV1InputKeys.methodResults];
NSDictionary *resultParameters = [FBSDKInternalUtility objectForJSONString:resultParametersJSON error:&error];
NSDictionary<id, id> *resultParameters = [FBSDKBasicUtility objectForJSONString:resultParametersJSON error:&error];
if (!resultParameters) {
if (errorRef != NULL) {
*errorRef = [NSError fbInvalidArgumentErrorWithName:FBSDKBridgeAPIProtocolNativeV1InputKeys.methodResults
Expand Down
Expand Up @@ -91,7 +91,7 @@ - (NSDictionary *)responseParametersForActionID:(NSString *)actionID

NSError *error;
NSString *bridgeParametersJSON = [FBSDKTypeUtility stringValue:queryParameters[FBSDK_BRIDGE_API_PROTOCOL_WEB_V1_BRIDGE_ARGS_KEY]];
NSDictionary *bridgeParameters = [FBSDKInternalUtility objectForJSONString:bridgeParametersJSON error:&error];
NSDictionary<id, id> *bridgeParameters = [FBSDKBasicUtility objectForJSONString:bridgeParametersJSON error:&error];
if (!bridgeParameters) {
if (error && (errorRef != NULL)) {
*errorRef = [NSError fbInvalidArgumentErrorWithName:FBSDK_BRIDGE_API_PROTOCOL_WEB_V1_BRIDGE_ARGS_KEY
Expand Down
9 changes: 0 additions & 9 deletions FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKInternalUtility.h
Expand Up @@ -220,15 +220,6 @@ NS_SWIFT_NAME(InternalUtility)
*/
+ (NSDictionary<NSString *, NSString *> *)dictionaryWithQueryString:(NSString *)queryString;

/**
Converts a JSON string into an object
@param string The JSON string to convert.
@param errorRef If an error occurs, upon return contains an NSError object that describes the problem.
@return An NSDictionary, NSArray, NSString or NSNumber containing the object representation, or nil if the string
cannot be converted.
*/
+ (id)objectForJSONString:(NSString *)string error:(NSError *__autoreleasing *)errorRef;

/**
Constructs a query string from a dictionary.
@param dictionary The dictionary with key/value pairs for the query string.
Expand Down
12 changes: 0 additions & 12 deletions FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKInternalUtility.m
Expand Up @@ -340,18 +340,6 @@ + (BOOL)object:(id)object isEqualToObject:(id)other;
return [object isEqual:other];
}

+ (id)objectForJSONString:(NSString *)string error:(NSError *__autoreleasing *)errorRef
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
if (!data) {
if (errorRef != NULL) {
*errorRef = nil;
}
return nil;
}
return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:errorRef];
}

+ (NSOperatingSystemVersion)operatingSystemVersion
{
static NSOperatingSystemVersion operatingSystemVersion = {
Expand Down
Expand Up @@ -69,7 +69,7 @@ - (void)testRequestURL
NSDictionary *queryParameters = [FBSDKUtility dictionaryWithQueryString:requestURL.query];
NSSet *expectedKeys = [NSSet setWithObjects:@"bridge_args", @"method_args", @"version", nil];
XCTAssertEqualObjects([NSSet setWithArray:[queryParameters allKeys]], expectedKeys);
XCTAssertEqualObjects([FBSDKInternalUtility objectForJSONString:queryParameters[@"method_args"] error:NULL], parameters);
XCTAssertEqualObjects([FBSDKBasicUtility objectForJSONString:queryParameters[@"method_args"] error:NULL], parameters);
}

- (void)testNilResponseParameters
Expand Down Expand Up @@ -309,7 +309,7 @@ - (void)testRequestParametersWithDataJSON
XCTAssertEqualObjects([NSSet setWithArray:[queryParameters allKeys]], expectedKeys);
NSMutableDictionary *expectedMethodArgs = [parameters mutableCopy];
expectedMethodArgs[@"data"] = [self _testDataSerialized:(NSData *)parameters[@"data"]];
NSDictionary *methodArgs = [FBSDKInternalUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
NSDictionary<id, id> *methodArgs = [FBSDKBasicUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
XCTAssertEqualObjects(methodArgs, expectedMethodArgs);
NSData *decodedData = [FBSDKBase64 decodeAsData:methodArgs[@"data"][@"fbAppBridgeType_jsonReadyValue"]];
XCTAssertEqualObjects(decodedData, parameters[@"data"]);
Expand Down Expand Up @@ -342,7 +342,7 @@ - (void)testRequestParametersWithImageJSON
XCTAssertEqualObjects([NSSet setWithArray:[queryParameters allKeys]], expectedKeys);
NSMutableDictionary *expectedMethodArgs = [parameters mutableCopy];
expectedMethodArgs[@"image"] = [self _testImageSerialized:(UIImage *)parameters[@"image"]];
NSDictionary *methodArgs = [FBSDKInternalUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
NSDictionary<id, id> *methodArgs = [FBSDKBasicUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
XCTAssertEqualObjects(methodArgs, expectedMethodArgs);
NSData *decodedData = [FBSDKBase64 decodeAsData:methodArgs[@"image"][@"fbAppBridgeType_jsonReadyValue"]];
XCTAssertNotNil([UIImage imageWithData:decodedData]);
Expand Down Expand Up @@ -381,7 +381,7 @@ - (void)testRequestParametersWithDataPasteboard
XCTAssertEqualObjects([NSSet setWithArray:[queryParameters allKeys]], expectedKeys);
NSMutableDictionary *expectedMethodArgs = [parameters mutableCopy];
expectedMethodArgs[@"data"] = [self _testDataContainerWithPasteboardName:pasteboardName tag:@"data"];
NSDictionary *methodArgs = [FBSDKInternalUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
NSDictionary<id, id> *methodArgs = [FBSDKBasicUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
XCTAssertEqualObjects(methodArgs, expectedMethodArgs);
}

Expand Down Expand Up @@ -419,7 +419,7 @@ - (void)testRequestParametersWithImagePasteboard
XCTAssertEqualObjects([NSSet setWithArray:[queryParameters allKeys]], expectedKeys);
NSMutableDictionary *expectedMethodArgs = [parameters mutableCopy];
expectedMethodArgs[@"image"] = [self _testDataContainerWithPasteboardName:pasteboardName tag:@"png"];
NSDictionary *methodArgs = [FBSDKInternalUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
NSDictionary<id, id> *methodArgs = [FBSDKBasicUtility objectForJSONString:queryParameters[@"method_args"] error:NULL];
XCTAssertEqualObjects(methodArgs, expectedMethodArgs);
}

Expand Down
Expand Up @@ -566,7 +566,7 @@ + (NSArray *)formDataForRequestBody:(NSString *)body
NSArray *lines = [body componentsSeparatedByString:@"\n"];
for (int i = 0; i < lines.count; i++) {
if ([lines[i] rangeOfString:@"Content-Disposition: form-data; name=\"custom_events_file\";"].location != NSNotFound) {
return [FBSDKInternalUtility objectForJSONString:lines[i+3] error:NULL];
return [FBSDKBasicUtility objectForJSONString:lines[i+3] error:NULL];
}
}
return nil;
Expand Down
Expand Up @@ -183,7 +183,7 @@ - (void)setParametersWithDictionary:(NSDictionary *)parameters appID:(NSString *
_parameters.dataAccessExpirationDate = dataAccessExpirationDate;

NSError *error = nil;
NSDictionary *state = [FBSDKInternalUtility objectForJSONString:parameters[@"state"] error:&error];
NSDictionary<id, id> *state = [FBSDKBasicUtility objectForJSONString:parameters[@"state"] error:&error];
_parameters.challenge = [FBSDKUtility URLDecode:state[@"challenge"]];
}

Expand Down
Expand Up @@ -64,7 +64,7 @@ @implementation FBSDKLoginManagerLogger

+ (FBSDKLoginManagerLogger *)loggerFromParameters:(NSDictionary *)parameters
{
NSDictionary *clientState = [FBSDKInternalUtility objectForJSONString:parameters[FBSDKLoginManagerLoggingClientStateKey] error:NULL];
NSDictionary<id, id> *clientState = [FBSDKBasicUtility objectForJSONString:parameters[FBSDKLoginManagerLoggingClientStateKey] error:NULL];

id isClientState = clientState[FBSDKLoginManagerLoggingClientStateIsClientState];
if ([isClientState isKindOfClass:[NSNumber class]] && [isClientState boolValue]) {
Expand Down Expand Up @@ -160,7 +160,7 @@ - (NSDictionary *)parametersWithTimeStampAndClientState:(NSDictionary *)loginPar
invalidObjectHandler:NULL];
params[@"e2e"] = e2eTimestampString;

NSDictionary *existingState = [FBSDKInternalUtility objectForJSONString:params[FBSDKLoginManagerLoggingClientStateKey] error:NULL];
NSDictionary<id, id> *existingState = [FBSDKBasicUtility objectForJSONString:params[FBSDKLoginManagerLoggingClientStateKey] error:NULL];
params[FBSDKLoginManagerLoggingClientStateKey] = [self clientStateForAuthMethod:authMethod andExistingState:existingState];

return params;
Expand Down

0 comments on commit 6d0b5aa

Please sign in to comment.