创建 RawPreservingProperty 接口 (#4464)

This commit is contained in:
Glavo
2025-09-14 21:06:32 +08:00
committed by GitHub
parent 19072bd617
commit 51954163d4
5 changed files with 141 additions and 26 deletions

View File

@@ -17,10 +17,7 @@
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.*;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Type;
@@ -57,7 +54,14 @@ public final class EnumOrdinalDeserializer<T extends Enum<T>> implements JsonDes
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return mapping.get(json.getAsString());
if (json == null || json.isJsonNull())
return null;
String name = json.getAsString();
T value = mapping.get(name);
if (value == null)
throw new JsonParseException("No enum constant with name " + name);
return value;
}
}

View File

@@ -17,10 +17,7 @@
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.*;
import com.google.gson.annotations.SerializedName;
import javafx.beans.Observable;
import javafx.beans.property.ListProperty;
@@ -120,7 +117,7 @@ public abstract class ObservableField<T> {
return (Observable) varHandle.get(value);
}
public abstract JsonElement serialize(T value, JsonSerializationContext context);
public abstract void serialize(JsonObject result, T value, JsonSerializationContext context);
public abstract void deserialize(T value, JsonElement element, JsonDeserializationContext context);
@@ -133,14 +130,36 @@ public abstract class ObservableField<T> {
}
@Override
public JsonElement serialize(T value, JsonSerializationContext context) {
return context.serialize(((Property<?>) get(value)).getValue(), elementType);
public void serialize(JsonObject result, T value, JsonSerializationContext context) {
Property<?> property = (Property<?>) get(value);
if (property instanceof RawPreservingProperty<?> rawPreserving) {
JsonElement rawJson = rawPreserving.getRawJson();
if (rawJson != null) {
result.add(getSerializedName(), rawJson);
return;
}
}
JsonElement serialized = context.serialize(property.getValue(), elementType);
if (serialized != null && !serialized.isJsonNull())
result.add(getSerializedName(), serialized);
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void deserialize(T value, JsonElement element, JsonDeserializationContext context) {
((Property) get(value)).setValue(context.deserialize(element, elementType));
Property property = (Property) get(value);
try {
property.setValue(context.deserialize(element, elementType));
} catch (Throwable e) {
if (property instanceof RawPreservingProperty<?>) {
((RawPreservingProperty<?>) property).setRawJson(element);
} else {
throw e;
}
}
}
}
@@ -158,8 +177,8 @@ public abstract class ObservableField<T> {
}
@Override
public JsonElement serialize(T value, JsonSerializationContext context) {
return context.serialize(get(value), collectionType);
public void serialize(JsonObject result, T value, JsonSerializationContext context) {
result.add(getSerializedName(), context.serialize(get(value), collectionType));
}
@SuppressWarnings({"unchecked"})
@@ -193,8 +212,8 @@ public abstract class ObservableField<T> {
}
@Override
public JsonElement serialize(T value, JsonSerializationContext context) {
return context.serialize(get(value), mapType);
public void serialize(JsonObject result, T value, JsonSerializationContext context) {
result.add(getSerializedName(), context.serialize(get(value), mapType));
}
@SuppressWarnings({"unchecked"})

View File

@@ -0,0 +1,57 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonElement;
import javafx.beans.property.SimpleObjectProperty;
import org.jetbrains.annotations.Nullable;
/// @author Glavo
public class RawPreservingObjectProperty<T> extends SimpleObjectProperty<T> implements RawPreservingProperty<T> {
private JsonElement rawJson;
public RawPreservingObjectProperty() {
}
public RawPreservingObjectProperty(T initialValue) {
super(initialValue);
}
public RawPreservingObjectProperty(Object bean, String name) {
super(bean, name);
}
public RawPreservingObjectProperty(Object bean, String name, T initialValue) {
super(bean, name, initialValue);
}
@Override
public void setRawJson(JsonElement value) {
this.rawJson = value;
}
@Override
public @Nullable JsonElement getRawJson() {
return rawJson;
}
@Override
protected void invalidated() {
this.rawJson = null;
}
}

View File

@@ -0,0 +1,32 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonElement;
import javafx.beans.property.Property;
import org.jetbrains.annotations.Nullable;
/// If a Property implementing this interface fails to deserialize a json object into a value, it will store the original JsonElement internally.
/// If the value does not change at runtime, the original JsonElement will be written back during serialization.
///
/// @author Glavo
public interface RawPreservingProperty<T> extends Property<T> {
void setRawJson(JsonElement value);
@Nullable JsonElement getRawJson();
}