Skip to content

Commit 0bf0521

Browse files
author
kai
committed
reuse MSIDJsonSerializer
1 parent 5c38240 commit 0bf0521

File tree

3 files changed

+17
-98
lines changed

3 files changed

+17
-98
lines changed

IdentityCore/src/telemetry/execution_flow/MSIDExecutionFlowBlob.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (void)setObject:(id)obj forKey:(NSString *)key
6363
{
6464
if ([NSString msidIsStringNilOrBlank:key])
6565
{
66-
MSID_LOG_WITH_CTX_PII(MSIDLogLevelWarning, nil, @"Key cannot be nil", nil);
66+
MSID_LOG_WITH_CTX_PII(MSIDLogLevelWarning, nil, @"Key cannot be nil or blank", nil);
6767
return;
6868
}
6969

IdentityCore/src/telemetry/execution_flow/MSIDExecutionFlowUtils.m

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "MSIDExecutionFlowUtils.h"
2626
#import "MSIDExecutionFlowBlob.h"
2727
#import "MSIDExecutionFlowConstants.h"
28+
#import "MSIDJsonSerializer.h"
2829

2930
@implementation MSIDExecutionFlowUtils
3031

@@ -41,85 +42,28 @@ + (instancetype)sharedInstance
4142
- (NSString *)convertDictionary:(NSDictionary *)dictionary
4243
toJsonStringWithKeys:(NSSet<NSString *> *)queryKeys
4344
{
44-
NSMutableString *result = [NSMutableString stringWithString:@"{"];
45+
MSIDJsonSerializer *serializer = [MSIDJsonSerializer new];
4546

46-
// Always include required fields in specific order: t, tid, ts
47-
[result appendFormat:@"\"t\":\"%@\",\"ts\":%@,\"tid\":%@", [self escapeJSONString:dictionary[MSID_EXECUTION_FLOW_TAG]], dictionary[MSID_EXECUTION_FLOW_TIME_SPENT], dictionary[MSID_EXECUTION_FLOW_THREAD_ID]];
48-
49-
// Add all other fields
50-
NSSet *reservedKeys = [NSSet setWithArray:@[MSID_EXECUTION_FLOW_TAG, MSID_EXECUTION_FLOW_TIME_SPENT, MSID_EXECUTION_FLOW_THREAD_ID]];
51-
for (NSString *key in dictionary)
47+
if ([queryKeys count] == 0)
5248
{
53-
if ([reservedKeys containsObject:key] || (![queryKeys containsObject:key] && queryKeys.count != 0))
54-
{
55-
continue;
56-
}
49+
return [serializer serializeToJsonString:dictionary error:nil];
5750

58-
id value = dictionary[key];
59-
if ([value isKindOfClass:NSString.class])
60-
{
61-
[result appendFormat:@",\"%@\":\"%@\"", [self escapeJSONString:key], [self escapeJSONString:value]];
62-
}
63-
else if ([value isKindOfClass:NSNumber.class])
64-
{
65-
[result appendFormat:@",\"%@\":%@", [self escapeJSONString:key], value];
66-
}
67-
}
68-
69-
[result appendString:@"}"];
70-
71-
return result;
72-
}
73-
74-
// Escapes special characters in a string for safe JSON inclusion
75-
- (NSString *)escapeJSONString:(NSString *)string
76-
{
77-
if (!string)
78-
{
79-
return @"";
8051
}
81-
82-
NSMutableString *escaped = [NSMutableString stringWithCapacity:string.length];
83-
for (NSUInteger i = 0; i < string.length; i++)
52+
else
8453
{
85-
unichar c = [string characterAtIndex:i];
86-
switch (c)
54+
NSSet *reservedKeys = [NSSet setWithArray:@[MSID_EXECUTION_FLOW_TAG, MSID_EXECUTION_FLOW_TIME_SPENT, MSID_EXECUTION_FLOW_THREAD_ID]];
55+
NSMutableDictionary *resultDict = [NSMutableDictionary new];
56+
for (NSString *key in dictionary)
8757
{
88-
case '\\':
89-
[escaped appendString:@"\\\\"];
90-
break;
91-
case '"':
92-
[escaped appendString:@"\\\""];
93-
break;
94-
case '\n':
95-
[escaped appendString:@"\\n"];
96-
break;
97-
case '\r':
98-
[escaped appendString:@"\\r"];
99-
break;
100-
case '\t':
101-
[escaped appendString:@"\\t"];
102-
break;
103-
case '\b':
104-
[escaped appendString:@"\\b"];
105-
break;
106-
case '\f':
107-
[escaped appendString:@"\\f"];
108-
break;
109-
default:
110-
if (c < 0x20)
111-
{
112-
// Escape other control characters as \u00XX
113-
[escaped appendFormat:@"\\u%04x", c];
114-
}
115-
else
116-
{
117-
[escaped appendFormat:@"%C", c];
118-
}
119-
break;
58+
if ([reservedKeys containsObject:key] || [queryKeys containsObject:key])
59+
{
60+
id value = dictionary[key];
61+
resultDict[key] = value;
62+
}
12063
}
64+
65+
return [serializer serializeToJsonString:resultDict error:nil];
12166
}
122-
return escaped;
12367
}
12468

12569
@end

IdentityCore/tests/MSIDExecutionFlowBlobTests.m

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ - (void)testSetObjectWithEmptyStringKey_shouldWork
347347
NSString *jsonString = [blob blobToStringWithKeys:[NSSet setWithArray:@[@""]]];
348348
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
349349
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
350-
XCTAssertEqualObjects(result[@""], @"value");
350+
XCTAssertNil(result[@""]);
351351
}
352352

353353
- (void)testSetObjectWithEmptyStringValue_shouldWork
@@ -411,11 +411,6 @@ - (void)testBlobToString_shouldReturnExpectedJSONFormat
411411

412412
NSString *jsonString = [blob blobToStringWithKeys:nil];
413413

414-
// Verify exact output format
415-
NSString *expectedJSON = @"{\"t\":\"TestTag\",\"ts\":1234,\"tid\":5678}";
416-
XCTAssertEqualObjects(jsonString, expectedJSON, @"JSON string should match expected format exactly");
417-
418-
419414
// Verify it's valid JSON by parsing it
420415
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
421416
NSError *error = nil;
@@ -511,26 +506,6 @@ - (void)testBlobToString_withZeroValues_shouldIncludeZeros
511506
XCTAssertTrue([jsonString containsString:@"\"e\":0"]);
512507
}
513508

514-
- (void)testBlobToString_requiredFieldsFirst_shouldBeInCorrectOrder
515-
{
516-
MSIDExecutionFlowBlob *blob = [[MSIDExecutionFlowBlob alloc] initWithTag:@"MyTag"
517-
timeStep:@(100)
518-
threadId:@(200)];
519-
520-
[blob setObject:@(999) forKey:@"custom"];
521-
NSString *jsonString = [blob blobToStringWithKeys:nil];
522-
523-
// Check that required fields appear first
524-
NSRange tRange = [jsonString rangeOfString:@"\"t\":"];
525-
NSRange tidRange = [jsonString rangeOfString:@"\"tid\":"];
526-
NSRange tsRange = [jsonString rangeOfString:@"\"ts\":"];
527-
NSRange customRange = [jsonString rangeOfString:@"\"custom\":"];
528-
529-
XCTAssertTrue(tRange.location < customRange.location, @"Tag should appear before custom fields");
530-
XCTAssertTrue(tidRange.location < customRange.location, @"Thread ID should appear before custom fields");
531-
XCTAssertTrue(tsRange.location < customRange.location, @"Timestamp should appear before custom fields");
532-
}
533-
534509
- (void)testBlobToStringWithKeys_withSpecificKeys_shouldOnlyIncludeRequestedFields
535510
{
536511
MSIDExecutionFlowBlob *blob = [[MSIDExecutionFlowBlob alloc] initWithTag:@"TestTag"

0 commit comments

Comments
 (0)