From 746d146a75bf11fe082b80b21ab945d1baac9da1 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 2 Mar 2024 01:08:24 -0500 Subject: [PATCH] Use gson for regex language deserialization --- .../richtext/syntax/RegexLanguages.java | 21 ++++++++++++------- .../ui/control/richtext/syntax/RegexRule.java | 14 ++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexLanguages.java b/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexLanguages.java index 3e609ace0..bda685553 100644 --- a/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexLanguages.java +++ b/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexLanguages.java @@ -1,7 +1,7 @@ package software.coley.recaf.ui.control.richtext.syntax; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.json.JsonMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; import software.coley.recaf.ui.LanguageStylesheets; @@ -25,7 +25,10 @@ * @see LanguageStylesheets For retrieving stylesheets of the supported languages. */ public class RegexLanguages { - private static final JsonMapper MAPPER = new JsonMapper(); + /* Don't need to use the managed 'Gson' instance from 'GsonProvider' since we only want to read data + * without any custom adapters. Using it would require some larger scale refactoring anyways. + */ + private static final Gson GSON = new GsonBuilder().create(); private static final Map NAME_TO_LANG = new HashMap<>(); private static final RegexRule LANG_JAVA; private static final RegexRule LANG_JASM; @@ -88,10 +91,14 @@ public static RegexRule addLanguage(@Nonnull String path) throws IOException { */ @Nonnull public static RegexRule addLanguage(@Nonnull String name, @Nonnull InputStream stream) throws IOException { - JsonParser parser = MAPPER.createParser(new InputStreamReader(stream)); - RegexRule lang = parser.readValueAs(RegexRule.class); - NAME_TO_LANG.put(name, lang); - return lang; + try { + RegexRule lang = GSON.fromJson(new InputStreamReader(stream), RegexRule.class); + NAME_TO_LANG.put(name, lang); + return lang; + } catch (Exception ex) { + // Gson's exceptions are unchecked, so rethrow as checked. + throw new IOException(ex.getMessage(), ex); + } } /** diff --git a/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexRule.java b/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexRule.java index 04d09c455..334adfcfb 100644 --- a/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexRule.java +++ b/recaf-ui/src/main/java/software/coley/recaf/ui/control/richtext/syntax/RegexRule.java @@ -1,6 +1,6 @@ package software.coley.recaf.ui.control.richtext.syntax; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; import java.util.List; @@ -25,10 +25,10 @@ * @author Matt Coley * @see RegexSyntaxHighlighter */ -public record RegexRule(@JsonProperty("name") String name, - @JsonProperty("regex") String regex, - @JsonProperty("classes") List classes, - @JsonProperty("sub-rules") List subRules, - @JsonProperty("backtrack-mark") String backtrackMark, - @JsonProperty("advance-mark") String advanceMark) { +public record RegexRule(@SerializedName("name") String name, + @SerializedName("regex") String regex, + @SerializedName("classes") List classes, + @SerializedName("sub-rules") List subRules, + @SerializedName("backtrack-mark") String backtrackMark, + @SerializedName("advance-mark") String advanceMark) { }