Skip to content

Commit

Permalink
fix: stringifying data types not supported for serialization by NSJSO…
Browse files Browse the repository at this point in the history
…NSerialization
  • Loading branch information
Desu Sai Venkat committed Jan 24, 2024
1 parent 9aad33e commit 56d13ab
Show file tree
Hide file tree
Showing 14 changed files with 526 additions and 269 deletions.
222 changes: 111 additions & 111 deletions Rudder.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Sources/Classes/Headers/Public/RSUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RSMessage.h"
#import "RSDBMessage.h"
#import "RSServerConfigSource.h"

Expand All @@ -26,17 +27,15 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString*) getLocale;
+ (NSString*) getDateString: (NSDate*) date;
+ (NSMutableArray<NSNumber *> *) sortArray:(NSMutableArray<NSNumber *>*) mutableArrayOfNumbers inOrder:(ORDER) order;
+ (NSString*) getStringFromDict:(NSDictionary *) dict;
+ (unsigned int) getUTF8LengthForDict:(NSDictionary *)message;
+ (unsigned int) getUTF8Length: (NSString*) message;
+ (NSDictionary<NSString*, id>*) serializeDict: (NSDictionary<NSString*, id>* _Nullable) dict;
+ (NSArray*) serializeArray: (NSArray*) array;
+ (int) getNumberOfBatches:(RSDBMessage*) dbMessage withFlushQueueSize: (int) queueSize;
+ (NSMutableArray<NSString *>*) getBatch:(NSMutableArray<NSString *>*) messageDetails withQueueSize: (int) queueSize;
+(NSArray<NSString*>*) getArrayFromCSVString: (NSString *) csvString;
+ (NSString*) getCSVString:(NSArray*) inputStrings;
+ (NSString*) getJSONCSVString:(NSArray*) inputStrings;
+ (id _Nullable) deSerializeJSONString:(NSString*) jsonString;
+ (NSString*) getCSVStringFromArray:(NSArray*) inputStrings;
+ (id) sanitizeObject: (id) val;
+ (NSString*) serialize:(id) object;
+ (id _Nullable) deserialize:(NSString*) jsonString;
+ (BOOL) isValidURL:(NSURL*) url;
+ (NSString*) appendSlashToUrl:(NSString*) url;
+ (NSString* _Nullable) getBase64EncodedString:(NSString* __nonnull) inputString;
Expand All @@ -46,6 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (BOOL)removeFile:(NSString *)fileName;
+ (BOOL) isDBMessageEmpty:(RSDBMessage*)dbMessage;
+ (BOOL) isEmptyString:(NSString *)value;
+ (BOOL) isInvalidNumber:(NSNumber *)number;

extern unsigned int MAX_EVENT_SIZE;
extern unsigned int MAX_BATCH_SIZE;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/RSCloudModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ - (void) startCloudModeProcessor {
if (payload != nil) {
response = [strongSelf->networkManager sendNetworkRequest:payload toEndpoint:BATCH_ENDPOINT withRequestMethod:POST];
if (response.state == NETWORK_SUCCESS) {
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSCloudModeManager: CloudModeProcessor: Updating status as CLOUDMODEPROCESSING DONE for events (%@)",[RSUtils getCSVString:dbMessage.messageIds]]];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSCloudModeManager: CloudModeProcessor: Updating status as CLOUDMODEPROCESSING DONE for events (%@)",[RSUtils getCSVStringFromArray:dbMessage.messageIds]]];
[RSMetricsReporter report:SDKMETRICS_CM_ATTEMPT_SUCCESS forMetricType:COUNT withProperties:nil andValue:(float)dbMessage.messages.count];
[strongSelf->dbPersistentManager updateEventsWithIds:dbMessage.messageIds withStatus:CLOUD_MODE_PROCESSING_DONE];
[strongSelf->dbPersistentManager clearProcessedEventsFromDB];
Expand Down
22 changes: 9 additions & 13 deletions Sources/Classes/RSContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ - (instancetype)initWithConfig:(RSConfig *) config {
// no persisted traits, create new and persist
[self createAndPersistTraits];
} else {
NSError *error;
NSDictionary *traitsDict = [NSJSONSerialization JSONObjectWithData:[traitsJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
NSDictionary *traitsDict = [RSUtils deserialize:traitsJson];
if (traitsDict == nil) {
_traits = [traitsDict mutableCopy];
_traits[@"anonymousId"] = _anonymousId;
[self persistTraits];
Expand All @@ -64,9 +63,8 @@ - (instancetype)initWithConfig:(RSConfig *) config {
// get saved external Ids from prefs
NSString *externalIdJson = [preferenceManager getExternalIds];
if (externalIdJson != nil) {
NSError *error;
NSDictionary *externalIdDict = [NSJSONSerialization JSONObjectWithData:[externalIdJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil) {
NSDictionary *externalIdDict = [RSUtils deserialize:externalIdJson];
if (externalIdDict == nil) {
_externalIds = [externalIdDict mutableCopy];
}
}
Expand All @@ -82,11 +80,11 @@ - (instancetype) initWithDict:(NSDictionary*) dict {
_traits = dict[@"traits"];
_library = [[RSLibraryInfo alloc] initWithDict:dict[@"library"]];
_os = [[RSOSInfo alloc] initWithDict:dict[@"os"]];
_screen = [[RSScreenInfo alloc] initWithDict:dict[@"screen"]];
_screen = [[RSScreenInfo alloc] initWithDict:dict[@"screen"]];
_userAgent = dict[@"userAgent"];
_locale = dict[@"locale"];
_device = [[RSDeviceInfo alloc] initWithDict:dict[@"device"]];
_network = [[RSNetwork alloc] initWithDict:dict[@"network"]];
_network = [[RSNetwork alloc] initWithDict:dict[@"network"]];
_timezone = dict[@"timezone"];
_sessionId = dict[@"sessionId"];
if(dict[@"sessionStart"]) {
Expand Down Expand Up @@ -144,8 +142,7 @@ - (void) persistTraitsOnQueue {
}

-(void) persistTraits {
NSData *traitsJsonData = [NSJSONSerialization dataWithJSONObject:[RSUtils serializeDict:self->_traits] options:0 error:nil];
NSString *traitsString = [[NSString alloc] initWithData:traitsJsonData encoding:NSUTF8StringEncoding];
NSString* traitsString = [RSUtils serialize:[self->_traits copy]];
[self->preferenceManager saveTraits:traitsString];
}

Expand Down Expand Up @@ -215,8 +212,7 @@ - (void)persistExternalIds {
dispatch_sync(queue, ^{
if (self->_externalIds != nil) {
// update persistence storage
NSData *externalIdJsonData = [NSJSONSerialization dataWithJSONObject:[RSUtils serializeArray:[self->_externalIds copy]] options:0 error:nil];
NSString *externalIdJson = [[NSString alloc] initWithData:externalIdJsonData encoding:NSUTF8StringEncoding];
NSString *externalIdJson = [RSUtils serialize: [self->_externalIds copy]];
[self->preferenceManager saveExternalIds:externalIdJson];
}
});
Expand Down Expand Up @@ -275,7 +271,7 @@ - (void)setConsentData:(NSArray <NSString *> *)deniedConsentIds {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
dispatch_sync(queue, ^{
[tempDict setObject:[_app dict] forKey:@"app"];
[tempDict setObject:[RSUtils serializeDict:_traits] forKey:@"traits"];
[tempDict setObject:_traits forKey:@"traits"];
[tempDict setObject:[_library dict] forKey:@"library"];
[tempDict setObject:[_os dict] forKey:@"os"];
[tempDict setObject:[_screen dict] forKey:@"screen"];
Expand Down
4 changes: 2 additions & 2 deletions Sources/Classes/RSDBPersistentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ - (void) clearOldEventsWithThreshold:(int) threshold {
}

- (void)clearEventsFromDB:(NSMutableArray<NSString *> *)messageIds {
NSString *deleteSqlString = [[NSString alloc] initWithFormat:@"DELETE FROM %@ WHERE %@ IN (%@);", TABLE_EVENTS, COL_ID, [RSUtils getCSVString:messageIds]];
NSString *deleteSqlString = [[NSString alloc] initWithFormat:@"DELETE FROM %@ WHERE %@ IN (%@);", TABLE_EVENTS, COL_ID, [RSUtils getCSVStringFromArray:messageIds]];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDBPersistentManager: deleteEventSql: %@", deleteSqlString]];
@synchronized (self) {
if([self execSQL:deleteSqlString]) {
Expand Down Expand Up @@ -564,7 +564,7 @@ -(void) updateEventWithId:(NSNumber *) messageId withStatus:(EVENT_PROCESSING_ST
}

-(void) updateEventsWithIds:(NSArray*) messageIds withStatus:(EVENT_PROCESSING_STATUS) status {
NSString *messageIdsCsv = [RSUtils getCSVString:messageIds];
NSString *messageIdsCsv = [RSUtils getCSVStringFromArray:messageIds];
if(messageIdsCsv != nil) {
NSString* updateEventStatusSQL = [[NSString alloc] initWithFormat:@"UPDATE %@ SET %@ = %@ | %d WHERE %@ IN (%@);", TABLE_EVENTS, COL_STATUS, COL_STATUS, status, COL_ID, messageIdsCsv];
@synchronized (self) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/RSDeviceModeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ - (void) replayMessageQueue {
NSArray *messageIds = dbMessage.messageIds;
NSArray *messages = dbMessage.messages;
for (int i=0; i<messageIds.count; i++) {
id object = [RSUtils deSerializeJSONString:messages[i]];
id object = [RSUtils deserialize:messages[i]];
NSNumber *rowId = [NSNumber numberWithInt:[messageIds[i] intValue]];
if (object && rowId) {
RSMessage* originalMessage = [[RSMessage alloc] initWithDict:object];
Expand Down
10 changes: 5 additions & 5 deletions Sources/Classes/RSDeviceModeTransformationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void) transformationProcessor {
do {
RSDBMessage* dbMessage = [strongSelf->dbPersistentManager fetchEventsFromDB:DMT_BATCH_SIZE ForMode:DEVICEMODE];
RSTransformationRequest* request = [strongSelf __getDeviceModeTransformationRequest:dbMessage];
NSString* payload = [RSUtils getStringFromDict:[request toDict]];
NSString* payload = [RSUtils serialize:[request toDict]];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Payload: %@", payload]];
[RSLogger logInfo:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: EventCount: %lu", (unsigned long)dbMessage.messageIds.count]];
RSNetworkResponse* response = [strongSelf->networkManager sendNetworkRequest:payload toEndpoint:TRANSFORM_ENDPOINT withRequestMethod:POST];
Expand Down Expand Up @@ -96,7 +96,7 @@ - (void) transformationProcessor {
[strongSelf completeDeviceModeEventProcessing:dbMessage];
} else {
deviceModeSleepCount = 0;
id object = [RSUtils deSerializeJSONString:responsePayload];
id object = [RSUtils deserialize:responsePayload];
if(object != nil && [object isKindOfClass:[NSDictionary class]]) {
NSArray* transformedBatch = object[@"transformedBatch"];
if(transformedBatch != nil && transformedBatch.count > 0) {
Expand All @@ -112,7 +112,7 @@ - (void) transformationProcessor {
}
}
}
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Updating status as DEVICE_MODE_PROCESSING DONE for events (%@)",[RSUtils getCSVString:dbMessage.messageIds]]];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Updating status as DEVICE_MODE_PROCESSING DONE for events (%@)",[RSUtils getCSVStringFromArray:dbMessage.messageIds]]];
[strongSelf->dbPersistentManager updateEventsWithIds:dbMessage.messageIds withStatus:DEVICE_MODE_PROCESSING_DONE];
[strongSelf->dbPersistentManager clearProcessedEventsFromDB];
}
Expand All @@ -135,7 +135,7 @@ - (RSTransformationRequest *) __getDeviceModeTransformationRequest:(RSDBMessage
NSMutableArray<NSString *>* messages = dbMessage.messages;
NSMutableArray<NSString *>* messageIds = dbMessage.messageIds;
for(int i=0; i<messages.count; i++) {
id object = [RSUtils deSerializeJSONString:messages[i]];
id object = [RSUtils deserialize:messages[i]];
if (object && [object isKindOfClass:[NSDictionary class]]) {
RSMessage* message = [[RSMessage alloc] initWithDict:object];
NSArray<NSString *>* destinationIds = [self getDestinationIdsWithTransformationsForMessage:message];
Expand All @@ -157,7 +157,7 @@ - (RSTransformationRequest *) __getDeviceModeTransformationRequest:(RSDBMessage


- (void)completeDeviceModeEventProcessing:(RSDBMessage *)dbMessage {
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Updating status as DEVICE_MODE_PROCESSING DONE for events (%@)",[RSUtils getCSVString:dbMessage.messageIds]]];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"RSDeviceModeTransformationManager: TransformationProcessor: Updating status as DEVICE_MODE_PROCESSING DONE for events (%@)",[RSUtils getCSVStringFromArray:dbMessage.messageIds]]];
[self->dbPersistentManager updateEventsWithIds:dbMessage.messageIds withStatus:DEVICE_MODE_PROCESSING_DONE];
[self->dbPersistentManager clearProcessedEventsFromDB];
}
Expand Down
10 changes: 7 additions & 3 deletions Sources/Classes/RSEventRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,19 @@ - (void) dump:(RSMessage *)message {
message = [self applyConsents:message];
[self applySession:message withUserSession:userSession andRudderConfig:config];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[message dict] options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"dump: %@", jsonString]];
NSString * jsonString = [RSUtils serialize: [message dict]];
if(jsonString == nil) {
[RSLogger logError:@"dump: Event is not a valid JSON object, discarding the event"];
return;
}

unsigned int messageSize = [RSUtils getUTF8Length:jsonString];
if (messageSize > MAX_EVENT_SIZE) {
[RSLogger logError:[NSString stringWithFormat:@"dump: Event size exceeds the maximum permitted event size(%iu)", MAX_EVENT_SIZE]];
[RSMetricsReporter report:SDKMETRICS_EVENTS_DISCARDED forMetricType:COUNT withProperties:@{SDKMETRICS_TYPE: SDKMETRICS_MSG_SIZE_INVALID} andValue:1];
return;
}
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"dump: %@", jsonString]];
NSNumber* rowId = [self->dbpersistenceManager saveEvent:jsonString];
[self->deviceModeManager makeFactoryDump: message FromHistory:NO withRowId:rowId];
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Classes/RSMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ - (instancetype) initWithDict: (NSDictionary*) dict {
[tempDict setValue:_groupId forKey:@"groupId"];
}
if (_traits != nil) {
[tempDict setValue:[RSUtils serializeDict:_traits] forKey:@"traits"];
[tempDict setValue:_traits forKey:@"traits"];
}
[tempDict setValue:_anonymousId forKey:@"anonymousId"];
if (_userId != nil) {
[tempDict setValue:_userId forKey:@"userId"];
}
if (_properties != nil) {
[tempDict setValue:[RSUtils serializeDict:_properties] forKey:@"properties"];
[tempDict setValue: _properties forKey:@"properties"];
}
[tempDict setValue:_event forKey:@"event"];
if (_userProperties != nil) {
[tempDict setValue:[RSUtils serializeDict:_userProperties] forKey:@"userProperties"];
[tempDict setValue:_userProperties forKey:@"userProperties"];
}
[tempDict setValue:_integrations forKey:@"integrations"];

Expand Down
2 changes: 1 addition & 1 deletion Sources/Classes/RSNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ - (instancetype) initWithDict:(NSDictionary*) dict {
@synchronized (tempDict) {
tempDict = [[NSMutableDictionary alloc] init];
if(_carrier.count !=0) {
[tempDict setValue:[RSUtils getCSVString:_carrier] forKey:@"carrier"];
[tempDict setValue:[RSUtils getCSVStringFromArray:_carrier] forKey:@"carrier"];
}
#if !TARGET_OS_WATCH
[tempDict setValue:[NSNumber numberWithBool:_wifi] forKey:@"wifi"];
Expand Down
12 changes: 5 additions & 7 deletions Sources/Classes/RSPreferenceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ - (void)clearAnonymousId {

- (void) clearAnonymousIdFromTraits {
NSString* traitsStr = [self getTraits];
NSError *error;
NSMutableDictionary* traitsDict = [NSJSONSerialization JSONObjectWithData:[traitsStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if(error == nil && traitsDict != nil) {
NSMutableDictionary* traitsDict = [RSUtils deserialize:traitsStr];
if(traitsDict != nil) {
[traitsDict removeObjectForKey:@"anonymousId"];
NSString* finalTraitsStr = [RSUtils getStringFromDict:traitsDict];
NSString* finalTraitsStr = [RSUtils serialize:traitsDict];
[self saveTraits:finalTraitsStr];
}
}
Expand Down Expand Up @@ -286,9 +285,8 @@ - (RSServerConfigSource * __nullable)getServerSourceConfig {
if (configStr == nil) {
return nil;
}
NSError *error;
NSDictionary *configDict = [NSJSONSerialization JSONObjectWithData:[configStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
if (error == nil && configDict != nil) {
NSDictionary *configDict = [RSUtils deserialize:configStr];
if (configDict != nil) {
RSServerConfigSource *config = [[RSServerConfigSource alloc] initWithConfigDict:configDict];
return config;
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/Classes/RSServerConfigManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ - (RSServerConfigSource* _Nullable) _retrieveConfig {
}

- (RSServerConfigSource *_Nullable)_parseConfig:(NSString *)configStr {
NSError *error;
NSDictionary *configDict = [NSJSONSerialization JSONObjectWithData:[configStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSDictionary *configDict = [RSUtils deserialize:configStr];

RSServerConfigSource *source;
if (error == nil && configDict != nil) {
if (configDict != nil) {
source = [[RSServerConfigSource alloc] initWithConfigDict:configDict];
for (RSServerDestination *destination in source.destinations) {
if (destination.shouldApplyDeviceModeTransformation) {
Expand Down
Loading

0 comments on commit 56d13ab

Please sign in to comment.