Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR] Introduce MapObjectReader #3215

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
554 changes: 445 additions & 109 deletions sentry/api/sentry.api

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions sentry/src/main/java/io/sentry/Breadcrumb.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public static Breadcrumb fromMap(
switch (entry.getKey()) {
case JsonKeys.TIMESTAMP:
if (value instanceof String) {
Date deserializedDate =
JsonObjectReader.dateOrNull((String) value, options.getLogger());
Date deserializedDate = ObjectReader.dateOrNull((String) value, options.getLogger());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Date deserializedDate = ObjectReader.dateOrNull((String) value, options.getLogger());
final Date deserializedDate = ObjectReader.dateOrNull((String) value, options.getLogger());

if (deserializedDate != null) {
timestamp = deserializedDate;
}
Expand Down Expand Up @@ -700,8 +699,8 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
public static final class Deserializer implements JsonDeserializer<Breadcrumb> {
@SuppressWarnings("unchecked")
@Override
public @NotNull Breadcrumb deserialize(
@NotNull JsonObjectReader reader, @NotNull ILogger logger) throws Exception {
public @NotNull Breadcrumb deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger)
throws Exception {
reader.beginObject();
@NotNull Date timestamp = DateUtils.getCurrentDateTime();
String message = null;
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/CheckIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger

public static final class Deserializer implements JsonDeserializer<CheckIn> {
@Override
public @NotNull CheckIn deserialize(@NotNull JsonObjectReader reader, @NotNull ILogger logger)
public @NotNull CheckIn deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger)
throws Exception {
SentryId sentryId = null;
MonitorConfig monitorConfig = null;
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/JsonDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
@ApiStatus.Internal
public interface JsonDeserializer<T> {
@NotNull
T deserialize(@NotNull JsonObjectReader reader, @NotNull ILogger logger) throws Exception;
T deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger) throws Exception;
}
196 changes: 138 additions & 58 deletions sentry/src/main/java/io/sentry/JsonObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,74 @@
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class JsonObjectReader extends JsonReader {
public final class JsonObjectReader implements ObjectReader {

private final @NotNull JsonReader jsonReader;

public JsonObjectReader(Reader in) {
super(in);
this.jsonReader = new JsonReader(in);
}

@Override
public @Nullable String nextStringOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextString();
return jsonReader.nextString();
}

@Override
public @Nullable Double nextDoubleOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextDouble();
return jsonReader.nextDouble();
}

@Override
public @Nullable Float nextFloatOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextFloat();
}

public @NotNull Float nextFloat() throws IOException {
return (float) nextDouble();
@Override
public float nextFloat() throws IOException {
return (float) jsonReader.nextDouble();
}

@Override
public @Nullable Long nextLongOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextLong();
return jsonReader.nextLong();
}

@Override
public @Nullable Integer nextIntegerOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextInt();
return jsonReader.nextInt();
}

@Override
public @Nullable Boolean nextBooleanOrNull() throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return nextBoolean();
return jsonReader.nextBoolean();
}

@Override
public void nextUnknown(ILogger logger, Map<String, Object> unknown, String name) {
try {
unknown.put(name, nextObjectOrNull());
Expand All @@ -81,90 +91,79 @@ public void nextUnknown(ILogger logger, Map<String, Object> unknown, String name
}
}

@Override
public <T> @Nullable List<T> nextListOrNull(
@NotNull ILogger logger, @NotNull JsonDeserializer<T> deserializer) throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
beginArray();
jsonReader.beginArray();
List<T> list = new ArrayList<>();
if (hasNext()) {
if (jsonReader.hasNext()) {
do {
try {
list.add(deserializer.deserialize(this, logger));
} catch (Exception e) {
logger.log(SentryLevel.WARNING, "Failed to deserialize object in list.", e);
}
} while (peek() == JsonToken.BEGIN_OBJECT);
} while (jsonReader.peek() == JsonToken.BEGIN_OBJECT);
}
endArray();
jsonReader.endArray();
return list;
}

@Override
public <T> @Nullable Map<String, T> nextMapOrNull(
@NotNull ILogger logger, @NotNull JsonDeserializer<T> deserializer) throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
beginObject();
jsonReader.beginObject();
Map<String, T> map = new HashMap<>();
if (hasNext()) {
if (jsonReader.hasNext()) {
do {
try {
String key = nextName();
String key = jsonReader.nextName();
map.put(key, deserializer.deserialize(this, logger));
} catch (Exception e) {
logger.log(SentryLevel.WARNING, "Failed to deserialize object in map.", e);
}
} while (peek() == JsonToken.BEGIN_OBJECT || peek() == JsonToken.NAME);
} while (jsonReader.peek() == JsonToken.BEGIN_OBJECT || jsonReader.peek() == JsonToken.NAME);
}

endObject();
jsonReader.endObject();
return map;
}

@Override
public <T> @Nullable T nextOrNull(
@NotNull ILogger logger, @NotNull JsonDeserializer<T> deserializer) throws Exception {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
return deserializer.deserialize(this, logger);
}

@Override
public @Nullable Date nextDateOrNull(ILogger logger) throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
return null;
}
return JsonObjectReader.dateOrNull(nextString(), logger);
}

public static @Nullable Date dateOrNull(@Nullable String dateString, ILogger logger) {
if (dateString == null) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
try {
return DateUtils.getDateTime(dateString);
} catch (Exception ignored) {
try {
return DateUtils.getDateTimeWithMillisPrecision(dateString);
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when deserializing millis timestamp format.", e);
}
}
return null;
return ObjectReader.dateOrNull(jsonReader.nextString(), logger);
}

@Override
public @Nullable TimeZone nextTimeZoneOrNull(ILogger logger) throws IOException {
if (peek() == JsonToken.NULL) {
nextNull();
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
try {
return TimeZone.getTimeZone(nextString());
return TimeZone.getTimeZone(jsonReader.nextString());
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when deserializing TimeZone", e);
}
Expand All @@ -177,7 +176,88 @@ public void nextUnknown(ILogger logger, Map<String, Object> unknown, String name
*
* @return The deserialized object from json.
*/
@Override
public @Nullable Object nextObjectOrNull() throws IOException {
return new JsonObjectDeserializer().deserialize(this);
}

@Override
public @NotNull JsonToken peek() throws IOException {
return jsonReader.peek();
}

@Override
public @NotNull String nextName() throws IOException {
return jsonReader.nextName();
}

@Override
public void beginObject() throws IOException {
jsonReader.beginObject();
}

@Override
public void endObject() throws IOException {
jsonReader.endObject();
}

@Override
public void beginArray() throws IOException {
jsonReader.beginArray();
}

@Override
public void endArray() throws IOException {
jsonReader.endArray();
}

@Override
public boolean hasNext() throws IOException {
return jsonReader.hasNext();
}

@Override
public int nextInt() throws IOException {
return jsonReader.nextInt();
}

@Override
public long nextLong() throws IOException {
return jsonReader.nextLong();
}

@Override
public String nextString() throws IOException {
return jsonReader.nextString();
}

@Override
public boolean nextBoolean() throws IOException {
return jsonReader.nextBoolean();
}

@Override
public double nextDouble() throws IOException {
return jsonReader.nextDouble();
}

@Override
public void nextNull() throws IOException {
jsonReader.nextNull();
}

@Override
public void setLenient(boolean lenient) {
jsonReader.setLenient(lenient);
}

@Override
public void skipValue() throws IOException {
jsonReader.skipValue();
}

@Override
public void close() throws IOException {
jsonReader.close();
}
}
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/MonitorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger

public static final class Deserializer implements JsonDeserializer<MonitorConfig> {
@Override
public @NotNull MonitorConfig deserialize(
@NotNull JsonObjectReader reader, @NotNull ILogger logger) throws Exception {
public @NotNull MonitorConfig deserialize(@NotNull ObjectReader reader, @NotNull ILogger logger)
throws Exception {
MonitorSchedule schedule = null;
Long checkinMargin = null;
Long maxRuntime = null;
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/MonitorContexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static final class Deserializer implements JsonDeserializer<MonitorContex

@Override
public @NotNull MonitorContexts deserialize(
final @NotNull JsonObjectReader reader, final @NotNull ILogger logger) throws Exception {
final @NotNull ObjectReader reader, final @NotNull ILogger logger) throws Exception {
final MonitorContexts contexts = new MonitorContexts();
reader.beginObject();
while (reader.peek() == JsonToken.NAME) {
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/MonitorSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
public static final class Deserializer implements JsonDeserializer<MonitorSchedule> {
@Override
public @NotNull MonitorSchedule deserialize(
@NotNull JsonObjectReader reader, @NotNull ILogger logger) throws Exception {
@NotNull ObjectReader reader, @NotNull ILogger logger) throws Exception {
String type = null;
String value = null;
String unit = null;
Expand Down
Loading
Loading