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
0 commit comments