Skip to content

Commit

Permalink
reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Jan 15, 2023
1 parent 4809638 commit de06338
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Style read(final JsonReader in) throws IOException {
if (hoverEventObject.has(HOVER_EVENT_CONTENTS)) {
final @Nullable JsonElement rawValue = hoverEventObject.get(HOVER_EVENT_CONTENTS);
final Class<?> actionType = action.type();
if (rawValue.isJsonNull() || (rawValue.isJsonArray() && rawValue.getAsJsonArray().size() == 0) || (rawValue.isJsonObject() && rawValue.getAsJsonObject().size() == 0)) {
if (isNullOrEmpty(rawValue)) {
value = null;
} else if (SerializerFactory.COMPONENT_TYPE.isAssignableFrom(actionType)) {
value = this.gson.fromJson(rawValue, SerializerFactory.COMPONENT_TYPE);
Expand All @@ -160,7 +160,7 @@ public Style read(final JsonReader in) throws IOException {
}
} else if (hoverEventObject.has(HOVER_EVENT_VALUE)) {
final JsonElement element = hoverEventObject.get(HOVER_EVENT_VALUE);
if (element.isJsonNull() || (element.isJsonArray() && element.getAsJsonArray().size() == 0) || (element.isJsonObject() && element.getAsJsonObject().size() == 0)) {
if (isNullOrEmpty(element)) {
value = null;
} else {
final Component rawValue = this.gson.fromJson(element, SerializerFactory.COMPONENT_TYPE);
Expand All @@ -184,6 +184,10 @@ public Style read(final JsonReader in) throws IOException {
return style.build();
}

private static boolean isNullOrEmpty(final @Nullable JsonElement element) {
return element == null || element.isJsonNull() || (element.isJsonArray() && element.getAsJsonArray().size() == 0) || (element.isJsonObject() && element.getAsJsonObject().size() == 0);
}

private boolean readBoolean(final JsonReader in) throws IOException {
final JsonToken peek = in.peek();
if (peek == JsonToken.BOOLEAN) {
Expand Down

0 comments on commit de06338

Please sign in to comment.