Skip to content

Commit

Permalink
Use gson for regex language deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Mar 2, 2024
1 parent 5712341 commit 746d146
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, RegexRule> NAME_TO_LANG = new HashMap<>();
private static final RegexRule LANG_JAVA;
private static final RegexRule LANG_JASM;
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,10 +25,10 @@
* @author Matt Coley
* @see RegexSyntaxHighlighter
*/
public record RegexRule(@JsonProperty("name") String name,
@JsonProperty("regex") String regex,
@JsonProperty("classes") List<String> classes,
@JsonProperty("sub-rules") List<RegexRule> 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<String> classes,
@SerializedName("sub-rules") List<RegexRule> subRules,
@SerializedName("backtrack-mark") String backtrackMark,
@SerializedName("advance-mark") String advanceMark) {
}

0 comments on commit 746d146

Please sign in to comment.