|
13 | 13 |
|
14 | 14 | @implementation SentrySerialization |
15 | 15 |
|
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 | | - |
84 | 16 | + (NSDictionary *_Nullable)deserializeDictionaryFromJsonData:(NSData *)data |
85 | 17 | { |
86 | 18 | NSError *error = nil; |
|
0 commit comments