Skip to content

Commit a7e7bab

Browse files
authored
ref: Convert more data serialization to Swift (#6196)
* ref: Convert more data serialization to Swift * Add comment
1 parent 062ca20 commit a7e7bab

37 files changed

+160
-162
lines changed

Sources/Sentry/Processors/SentryWatchdogTerminationBreadcrumbProcessor.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import "SentryInternalDefines.h"
44
#import "SentryLogC.h"
55
#import "SentrySerialization.h"
6+
#import "SentrySwift.h"
67

78
@interface SentryWatchdogTerminationBreadcrumbProcessor ()
89

@@ -40,7 +41,7 @@ - (void)dealloc
4041
- (void)addSerializedBreadcrumb:(NSDictionary *)crumb
4142
{
4243
SENTRY_LOG_DEBUG(@"Adding breadcrumb: %@", crumb);
43-
NSData *_Nullable jsonData = [SentrySerialization dataWithJSONObject:crumb];
44+
NSData *_Nullable jsonData = [SentrySerializationSwift dataWithJSONObject:crumb];
4445
if (jsonData == nil) {
4546
SENTRY_LOG_ERROR(@"Error serializing breadcrumb to JSON");
4647
return;

Sources/Sentry/Profiling/SentryProfilerSerialization.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
return nil;
332332
}
333333

334-
NSData *JSONData = [SentrySerialization dataWithJSONObject:payload];
334+
NSData *JSONData = [SentrySerializationSwift dataWithJSONObject:payload];
335335
if (JSONData == nil) {
336336
SENTRY_LOG_DEBUG(@"Failed to encode profile to JSON.");
337337
return nil;
@@ -396,7 +396,7 @@
396396
payload[@"transaction"] = transactionDict;
397397
payload[@"timestamp"] = sentry_toIso8601String(startTimestamp);
398398

399-
NSData *JSONData = [SentrySerialization dataWithJSONObject:payload];
399+
NSData *JSONData = [SentrySerializationSwift dataWithJSONObject:payload];
400400
if (JSONData == nil) {
401401
SENTRY_LOG_DEBUG(@"Failed to encode profile to JSON.");
402402
return nil;

Sources/Sentry/SentryBaseIntegration.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ - (BOOL)shouldBeEnabledWithOptions:(SentryOptions *)options
187187
= !options.enableAutoPerformanceTracing || !options.isTracingEnabled;
188188
BOOL appHangsV2Disabled = options.isAppHangTrackingV2Disabled;
189189
# if SDK_V9
190+
// The V9 watchdog tracker uses the frames tracker, so frame tracking
191+
// must be enabled if watchdog tracking is enabled.
190192
BOOL watchdogDisabled = !options.enableWatchdogTerminationTracking;
191193
# else
192194
// Before V9 this should have no effect so set it to YES

Sources/Sentry/SentryFileManager.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ - (NSURL *)getSentryPathAsURL
218218

219219
- (nullable NSString *)storeEnvelope:(SentryEnvelope *)envelope
220220
{
221-
NSData *envelopeData = [SentrySerialization dataWithEnvelope:envelope];
221+
NSData *envelopeData = [SentrySerializationSwift dataWithEnvelope:envelope];
222222

223223
if (envelopeData == nil) {
224224
SENTRY_LOG_ERROR(@"Serialization of envelope failed. Can't store envelope.");
@@ -414,7 +414,7 @@ - (NSDate *_Nullable)readTimestampLastInForeground
414414

415415
- (void)storeAppState:(SentryAppState *)appState
416416
{
417-
NSData *data = [SentrySerialization dataWithJSONObject:[appState serialize]];
417+
NSData *data = [SentrySerializationSwift dataWithJSONObject:[appState serialize]];
418418

419419
if (data == nil) {
420420
SENTRY_LOG_ERROR(@"Failed to store app state, because of an error in serialization");
@@ -582,7 +582,7 @@ - (void)deleteTimezoneOffset
582582

583583
- (void)storeAppHangEvent:(SentryEvent *)appHangEvent
584584
{
585-
NSData *jsonData = [SentrySerialization dataWithJSONObject:[appHangEvent serialize]];
585+
NSData *jsonData = [SentrySerializationSwift dataWithJSONObject:[appHangEvent serialize]];
586586
if (jsonData == nil) {
587587
SENTRY_LOG_ERROR(@"Failed to store app hang event, because of an error in serialization.");
588588
return;
@@ -943,7 +943,7 @@ - (BOOL)isDirectory:(NSString *)path
943943

944944
- (void)storeSession:(SentrySession *)session sessionFilePath:(NSString *)sessionFilePath
945945
{
946-
NSData *sessionData = [SentrySerialization dataWithSession:session];
946+
NSData *sessionData = [SentrySerializationSwift dataWithSession:session];
947947
SENTRY_LOG_DEBUG(@"Writing session: %@", sessionFilePath);
948948
@synchronized(self.currentSessionFilePath) {
949949
if (![self writeData:sessionData toPath:sessionFilePath]) {

Sources/Sentry/SentryMigrateSessionInit.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ + (void)storeSessionInit:(SentryEnvelope *)originalEnvelope
109109
items:envelopeItemsWithUpdatedSession];
110110

111111
NSError *error;
112-
NSData *envelopeWithInitFlagData = [SentrySerialization dataWithEnvelope:envelopeWithInitFlag];
112+
NSData *envelopeWithInitFlagData =
113+
[SentrySerializationSwift dataWithEnvelope:envelopeWithInitFlag];
113114
[envelopeWithInitFlagData writeToFile:envelopeFilePath
114115
options:NSDataWritingAtomic
115116
error:&error];

Sources/Sentry/SentryNSURLRequestBuilder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ - (nullable NSURLRequest *)createEnvelopeRequest:(SentryEnvelope *)envelope
1212
dsn:(SentryDsn *)dsn
1313
didFailWithError:(NSError *_Nullable *_Nullable)error
1414
{
15-
NSData *data = [SentrySerialization dataWithEnvelope:envelope];
15+
NSData *data = [SentrySerializationSwift dataWithEnvelope:envelope];
1616
if (nil == data) {
1717
SENTRY_LOG_ERROR(@"Envelope cannot be converted to data");
1818
return nil;
@@ -24,7 +24,7 @@ - (nullable NSURLRequest *)createEnvelopeRequest:(SentryEnvelope *)envelope
2424
url:(NSURL *)url
2525
didFailWithError:(NSError *_Nullable *_Nullable)error
2626
{
27-
NSData *data = [SentrySerialization dataWithEnvelope:envelope];
27+
NSData *data = [SentrySerializationSwift dataWithEnvelope:envelope];
2828
if (nil == data) {
2929
SENTRY_LOG_ERROR(@"Envelope cannot be converted to data");
3030
return nil;

Sources/Sentry/SentrySerialization.m

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,6 @@
1313

1414
@implementation SentrySerialization
1515

16-
+ (NSData *_Nullable)dataWithJSONObject:(id)jsonObject
17-
{
18-
if (![NSJSONSerialization isValidJSONObject:jsonObject]) {
19-
SENTRY_LOG_ERROR(@"Dictionary is not a valid JSON object.");
20-
return nil;
21-
}
22-
23-
NSError *error = nil;
24-
NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:&error];
25-
if (error) {
26-
SENTRY_LOG_ERROR(@"Internal error while serializing JSON: %@", error);
27-
}
28-
29-
return data;
30-
}
31-
32-
+ (NSData *_Nullable)dataWithEnvelope:(SentryEnvelope *)envelope
33-
{
34-
NSMutableData *envelopeData = [[NSMutableData alloc] init];
35-
NSMutableDictionary *serializedData = [NSMutableDictionary new];
36-
if (nil != envelope.header.eventId) {
37-
[serializedData setValue:[envelope.header.eventId sentryIdString] forKey:@"event_id"];
38-
}
39-
40-
SentrySdkInfo *sdkInfo = envelope.header.sdkInfo;
41-
if (nil != sdkInfo) {
42-
[serializedData setValue:[sdkInfo serialize] forKey:@"sdk"];
43-
}
44-
45-
SentryTraceContext *traceContext = envelope.header.traceContext;
46-
if (traceContext != nil) {
47-
[serializedData setValue:[traceContext serialize] forKey:@"trace"];
48-
}
49-
50-
NSDate *sentAt = envelope.header.sentAt;
51-
if (sentAt != nil) {
52-
[serializedData setValue:sentry_toIso8601String(sentAt) forKey:@"sent_at"];
53-
}
54-
NSData *header = [SentrySerialization dataWithJSONObject:serializedData];
55-
if (nil == header) {
56-
SENTRY_LOG_ERROR(@"Envelope header cannot be converted to JSON.");
57-
return nil;
58-
}
59-
[envelopeData appendData:header];
60-
61-
NSData *_Nonnull const newLineData = [NSData dataWithBytes:"\n" length:1];
62-
for (int i = 0; i < envelope.items.count; ++i) {
63-
[envelopeData appendData:newLineData];
64-
NSDictionary *serializedItemHeaderData = [envelope.items[i].header serialize];
65-
66-
NSData *itemHeader = [SentrySerialization dataWithJSONObject:serializedItemHeaderData];
67-
if (nil == itemHeader) {
68-
SENTRY_LOG_ERROR(@"Envelope item header cannot be converted to JSON.");
69-
return nil;
70-
}
71-
[envelopeData appendData:itemHeader];
72-
[envelopeData appendData:newLineData];
73-
[envelopeData appendData:envelope.items[i].data];
74-
}
75-
76-
return envelopeData;
77-
}
78-
79-
+ (NSData *_Nullable)dataWithSession:(SentrySession *)session
80-
{
81-
return [self dataWithJSONObject:[session serialize]];
82-
}
83-
8416
+ (NSDictionary *_Nullable)deserializeDictionaryFromJsonData:(NSData *)data
8517
{
8618
NSError *error = nil;

Sources/Sentry/SentrySessionReplayIntegration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ - (void)saveCurrentSessionInfo:(SentryId *)sessionId
451451
[[NSDictionary alloc] initWithObjectsAndKeys:sessionId.sentryIdString, @"replayId",
452452
path.lastPathComponent, @"path", @(options.onErrorSampleRate), @"errorSampleRate", nil];
453453

454-
NSData *data = [SentrySerialization dataWithJSONObject:info];
454+
NSData *data = [SentrySerializationSwift dataWithJSONObject:info];
455455

456456
NSString *infoPath = [[path stringByDeletingLastPathComponent]
457457
stringByAppendingPathComponent:SENTRY_CURRENT_REPLAY];

Sources/Sentry/include/SentryModels+Serializable.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# import "SentryTraceContext.h"
1919
# import "SentryUser.h"
2020

21+
NS_ASSUME_NONNULL_BEGIN
22+
2123
@interface SentryScope () <SentryInternalSerializable>
2224

2325
@end
@@ -56,6 +58,8 @@
5658

5759
@interface SentryEnvelopeItemHeader () <SentryInternalSerializable>
5860

61+
- (NSDictionary<NSString *, id> *)serialize;
62+
5963
@end
6064

6165
@interface SentryFrame () <SentryInternalSerializable>
@@ -72,6 +76,8 @@
7276

7377
@interface SentryTraceContext () <SentryInternalSerializable>
7478

79+
- (NSDictionary<NSString *, id> *)serialize;
80+
7581
@end
7682

7783
@interface SentryMessage () <SentryInternalSerializable>
@@ -90,4 +96,6 @@
9096

9197
@end
9298

99+
NS_ASSUME_NONNULL_END
100+
93101
#endif // SDK_V9

Sources/Sentry/include/SentryPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "SentryFileManager.h"
3636
#import "SentryLevelHelper.h"
3737
#import "SentryMeta.h"
38+
#import "SentryModels+Serializable.h"
3839
#import "SentryMsgPackSerializer.h"
3940
#import "SentryNSDictionarySanitize.h"
4041
#import "SentryOptions+Private.h"

0 commit comments

Comments
 (0)