forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSerializer.java
More file actions
215 lines (192 loc) · 8.89 KB
/
JsonSerializer.java
File metadata and controls
215 lines (192 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package io.sentry;
import io.sentry.clientreport.ClientReport;
import io.sentry.protocol.App;
import io.sentry.protocol.Browser;
import io.sentry.protocol.Contexts;
import io.sentry.protocol.DebugImage;
import io.sentry.protocol.DebugMeta;
import io.sentry.protocol.Device;
import io.sentry.protocol.Gpu;
import io.sentry.protocol.MeasurementValue;
import io.sentry.protocol.Mechanism;
import io.sentry.protocol.Message;
import io.sentry.protocol.OperatingSystem;
import io.sentry.protocol.Request;
import io.sentry.protocol.SdkInfo;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryPackage;
import io.sentry.protocol.SentryRuntime;
import io.sentry.protocol.SentrySpan;
import io.sentry.protocol.SentryStackFrame;
import io.sentry.protocol.SentryStackTrace;
import io.sentry.protocol.SentryThread;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.util.Objects;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The serializer class that uses manual JSON parsing with the help of vendored GSON reader/writer
* classes.
*/
public final class JsonSerializer implements ISerializer {
/** the UTF-8 Charset */
@SuppressWarnings("CharsetObjectCanBeUsed")
private static final Charset UTF_8 = Charset.forName("UTF-8");
/** the SentryOptions */
private final @NotNull SentryOptions options;
private final @NotNull Map<Class<?>, JsonDeserializer<?>> deserializersByClass;
/**
* All our custom deserializers need to be registered to be used with the deserializer instance. *
*/
public JsonSerializer(@NotNull SentryOptions options) {
this.options = options;
deserializersByClass = new HashMap<>();
deserializersByClass.put(App.class, new App.Deserializer());
deserializersByClass.put(Breadcrumb.class, new Breadcrumb.Deserializer());
deserializersByClass.put(Browser.class, new Browser.Deserializer());
deserializersByClass.put(Contexts.class, new Contexts.Deserializer());
deserializersByClass.put(DebugImage.class, new DebugImage.Deserializer());
deserializersByClass.put(DebugMeta.class, new DebugMeta.Deserializer());
deserializersByClass.put(Device.class, new Device.Deserializer());
deserializersByClass.put(
Device.DeviceOrientation.class, new Device.DeviceOrientation.Deserializer());
deserializersByClass.put(Gpu.class, new Gpu.Deserializer());
deserializersByClass.put(MeasurementValue.class, new MeasurementValue.Deserializer());
deserializersByClass.put(Mechanism.class, new Mechanism.Deserializer());
deserializersByClass.put(Message.class, new Message.Deserializer());
deserializersByClass.put(OperatingSystem.class, new OperatingSystem.Deserializer());
deserializersByClass.put(ProfilingTraceData.class, new ProfilingTraceData.Deserializer());
deserializersByClass.put(Request.class, new Request.Deserializer());
deserializersByClass.put(SdkInfo.class, new SdkInfo.Deserializer());
deserializersByClass.put(SdkVersion.class, new SdkVersion.Deserializer());
deserializersByClass.put(SentryEnvelopeHeader.class, new SentryEnvelopeHeader.Deserializer());
deserializersByClass.put(
SentryEnvelopeItemHeader.class, new SentryEnvelopeItemHeader.Deserializer());
deserializersByClass.put(SentryEvent.class, new SentryEvent.Deserializer());
deserializersByClass.put(SentryException.class, new SentryException.Deserializer());
deserializersByClass.put(SentryItemType.class, new SentryItemType.Deserializer());
deserializersByClass.put(SentryLevel.class, new SentryLevel.Deserializer());
deserializersByClass.put(SentryPackage.class, new SentryPackage.Deserializer());
deserializersByClass.put(SentryRuntime.class, new SentryRuntime.Deserializer());
deserializersByClass.put(SentrySpan.class, new SentrySpan.Deserializer());
deserializersByClass.put(SentryStackFrame.class, new SentryStackFrame.Deserializer());
deserializersByClass.put(SentryStackTrace.class, new SentryStackTrace.Deserializer());
deserializersByClass.put(SentryThread.class, new SentryThread.Deserializer());
deserializersByClass.put(SentryTransaction.class, new SentryTransaction.Deserializer());
deserializersByClass.put(Session.class, new Session.Deserializer());
deserializersByClass.put(SpanContext.class, new SpanContext.Deserializer());
deserializersByClass.put(SpanId.class, new SpanId.Deserializer());
deserializersByClass.put(SpanStatus.class, new SpanStatus.Deserializer());
deserializersByClass.put(User.class, new User.Deserializer());
deserializersByClass.put(UserFeedback.class, new UserFeedback.Deserializer());
deserializersByClass.put(ClientReport.class, new ClientReport.Deserializer());
}
// Deserialize
@Override
public <T> @Nullable T deserialize(@NotNull Reader reader, @NotNull Class<T> clazz) {
try {
JsonObjectReader jsonObjectReader = new JsonObjectReader(reader);
JsonDeserializer<?> deserializer = deserializersByClass.get(clazz);
if (deserializer != null) {
Object object = deserializer.deserialize(jsonObjectReader, options.getLogger());
return clazz.cast(object);
} else {
return null; // No way to deserialize objects we don't know about.
}
} catch (Exception e) {
options.getLogger().log(SentryLevel.ERROR, "Error when deserializing", e);
return null;
}
}
@Override
public @Nullable SentryEnvelope deserializeEnvelope(@NotNull InputStream inputStream) {
Objects.requireNonNull(inputStream, "The InputStream object is required.");
try {
return options.getEnvelopeReader().read(inputStream);
} catch (IOException e) {
options.getLogger().log(SentryLevel.ERROR, "Error deserializing envelope.", e);
return null;
}
}
// Serialize
@Override
public <T> void serialize(@NotNull T entity, @NotNull Writer writer) throws IOException {
Objects.requireNonNull(entity, "The entity is required.");
Objects.requireNonNull(writer, "The Writer object is required.");
if (options.getLogger().isEnabled(SentryLevel.DEBUG)) {
String serialized = serializeToString(entity, true);
options.getLogger().log(SentryLevel.DEBUG, "Serializing object: %s", serialized);
}
JsonObjectWriter jsonObjectWriter = new JsonObjectWriter(writer, options.getMaxDepth());
jsonObjectWriter.value(options.getLogger(), entity);
writer.flush();
}
/**
* Serializes an envelope to an OutputStream
*
* @param envelope the envelope
* @param outputStream will not be closed automatically
* @throws Exception an exception
*/
@Override
public void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream outputStream)
throws Exception {
Objects.requireNonNull(envelope, "The SentryEnvelope object is required.");
Objects.requireNonNull(outputStream, "The Stream object is required.");
// we do not want to close these as we would also close the stream that was passed in
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
final Writer writer = new BufferedWriter(new OutputStreamWriter(bufferedOutputStream, UTF_8));
try {
envelope
.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");
for (final SentryEnvelopeItem item : envelope.getItems()) {
try {
// When this throws we don't write anything and continue with the next item.
final byte[] data = item.getData();
item.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
writer.write("\n");
writer.flush();
outputStream.write(data);
writer.write("\n");
} catch (Exception exception) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
}
}
} finally {
writer.flush();
}
}
@Override
public @NotNull String serialize(@NotNull Map<String, Object> data) throws Exception {
return serializeToString(data, false);
}
// Helper
private @NotNull String serializeToString(Object object, boolean pretty) throws IOException {
StringWriter stringWriter = new StringWriter();
JsonObjectWriter jsonObjectWriter = new JsonObjectWriter(stringWriter, options.getMaxDepth());
if (pretty) {
jsonObjectWriter.setIndent("\t");
}
jsonObjectWriter.value(options.getLogger(), object);
return stringWriter.toString();
}
}