ItemStackAdapter for Json: removed backward compatibility of serialized data (was temporary during the server upgrade)
This commit is contained in:
@@ -3,6 +3,7 @@ package fr.pandacube.lib.core.json;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
|
import com.google.gson.Strictness;
|
||||||
import com.google.gson.ToNumberStrategy;
|
import com.google.gson.ToNumberStrategy;
|
||||||
import com.google.gson.TypeAdapter;
|
import com.google.gson.TypeAdapter;
|
||||||
import com.google.gson.TypeAdapterFactory;
|
import com.google.gson.TypeAdapterFactory;
|
||||||
@@ -74,21 +75,21 @@ public class Json {
|
|||||||
public static final Gson gson = build(Function.identity());
|
public static final Gson gson = build(Function.identity());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#setPrettyPrinting()} and support
|
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#setPrettyPrinting()} and support
|
||||||
* for Java records and additional {@link TypeAdapterFactory} provided with
|
* for Java records and additional {@link TypeAdapterFactory} provided with
|
||||||
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
||||||
*/
|
*/
|
||||||
public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting);
|
public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()} and support for
|
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#serializeNulls()} and support for
|
||||||
* Java records and additional {@link TypeAdapterFactory} provided with
|
* Java records and additional {@link TypeAdapterFactory} provided with
|
||||||
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
* {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
||||||
*/
|
*/
|
||||||
public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls);
|
public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Gson} instance with {@link GsonBuilder#setLenient()}, {@link GsonBuilder#serializeNulls()},
|
* {@link Gson} instance with {@link Strictness#LENIENT}, {@link GsonBuilder#serializeNulls()},
|
||||||
* {@link GsonBuilder#setPrettyPrinting()} and support for Java records and additional {@link TypeAdapterFactory}
|
* {@link GsonBuilder#setPrettyPrinting()} and support for Java records and additional {@link TypeAdapterFactory}
|
||||||
* provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
* provided with {@link #registerTypeAdapterFactory(TypeAdapterFactory)}.
|
||||||
*/
|
*/
|
||||||
@@ -105,7 +106,7 @@ public class Json {
|
|||||||
.registerTypeAdapterFactory(new CustomAdapterFactory())
|
.registerTypeAdapterFactory(new CustomAdapterFactory())
|
||||||
.disableHtmlEscaping()
|
.disableHtmlEscaping()
|
||||||
.setObjectToNumberStrategy(YAML_EQUIVALENT_NUMBER_STRATEGY)
|
.setObjectToNumberStrategy(YAML_EQUIVALENT_NUMBER_STRATEGY)
|
||||||
.setLenient();
|
.setStrictness(Strictness.LENIENT);
|
||||||
return builderModifier.apply(base).create();
|
return builderModifier.apply(base).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -38,15 +38,13 @@ import java.util.Map;
|
|||||||
if (!(json instanceof JsonObject jsonObj))
|
if (!(json instanceof JsonObject jsonObj))
|
||||||
throw new JsonParseException("Unable to deserialize a ConfigurationSerializable from the provided json structure.");
|
throw new JsonParseException("Unable to deserialize a ConfigurationSerializable from the provided json structure.");
|
||||||
|
|
||||||
// the deserialized json may contain older data compatible with pre 1.21.5 but not compatible after.
|
|
||||||
// if it contains both old and new data, delete the old one introduced for compatibility
|
// if it contains both old and new data, delete the old one introduced for compatibility
|
||||||
if (jsonObj.has("DataVersion")) { // it uses the new DataVersion data
|
if (jsonObj.has("DataVersion") || jsonObj.has("id")) {
|
||||||
jsonObj.remove("v");
|
jsonObj.remove("v");
|
||||||
}
|
|
||||||
if (jsonObj.has("id")) {
|
|
||||||
jsonObj.remove("type");
|
jsonObj.remove("type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// format used when using ConfigurationSerialization
|
||||||
if (jsonObj.has(ConfigurationSerialization.SERIALIZED_TYPE_KEY))
|
if (jsonObj.has(ConfigurationSerialization.SERIALIZED_TYPE_KEY))
|
||||||
return context.deserialize(jsonObj, ConfigurationSerializable.class);
|
return context.deserialize(jsonObj, ConfigurationSerializable.class);
|
||||||
|
|
||||||
@@ -79,17 +77,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
Map<String, Object> serialized = src.serialize();
|
return context.serialize(src.serialize(), MAP_STR_OBJ_TYPE.getType());
|
||||||
|
|
||||||
// make the generated json compatible with pre 1.21.5 deserializer (temporary fix during the upgrade of the server)
|
|
||||||
if (serialized.containsKey("DataVersion")) {
|
|
||||||
serialized.put("v", serialized.get("DataVersion"));
|
|
||||||
}
|
|
||||||
if (serialized.containsKey("id")) {
|
|
||||||
serialized.put("type", Registry.MATERIAL.getOrThrow(Key.key((String)serialized.get("id"))).name());
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.serialize(serialized, MAP_STR_OBJ_TYPE.getType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user