Work on "better" json serialization of data objects

This commit is contained in:
Pahimar 2016-05-11 22:56:39 -04:00
parent 3a29be6d9a
commit 3c28db961c
7 changed files with 339 additions and 16 deletions

View file

@ -1,10 +1,10 @@
package com.pahimar.ee3;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pahimar.ee3.array.AlchemyArrayRegistry;
import com.pahimar.ee3.command.CommandEE;
import com.pahimar.ee3.exchange.CachedOreDictionary;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
import com.pahimar.ee3.exchange.*;
import com.pahimar.ee3.handler.*;
import com.pahimar.ee3.init.*;
import com.pahimar.ee3.knowledge.AbilityRegistry;
@ -22,6 +22,10 @@ import com.pahimar.ee3.util.FluidHelper;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper;
import com.pahimar.ee3.util.TileEntityDataHelper;
import com.pahimar.ee3.util.serialize.FluidStackSerializer;
import com.pahimar.ee3.util.serialize.ItemStackSerializer;
import com.pahimar.ee3.util.serialize.OreStackSerializer;
import com.pahimar.ee3.util.serialize.WrappedStackSerializer;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
@ -29,6 +33,8 @@ import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.*;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.io.File;
import java.io.IOException;
@ -42,6 +48,13 @@ public class EquivalentExchange3
@SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
public static IProxy proxy;
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(ItemStack.class, new ItemStackSerializer())
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
.create();
@EventHandler
public void invalidFingerprint(FMLFingerprintViolationEvent event)
{
@ -72,6 +85,7 @@ public class EquivalentExchange3
{
ConfigurationHandler.init(event.getSuggestedConfigurationFile());
Files.init(event);
Files.Global.init(event);
PacketHandler.init();

View file

@ -2,8 +2,8 @@ package com.pahimar.ee3.exchange;
import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import com.pahimar.ee3.EquivalentExchange3;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.reference.Files;
import com.pahimar.ee3.util.LogHelper;
import net.minecraft.init.Items;
import net.minecraft.nbt.NBTTagCompound;
@ -22,11 +22,14 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER);
public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry();
private static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken<Map<WrappedStack, EnergyValue>>(){}.getType();
public static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken<Map<WrappedStack, EnergyValue>>(){}.getType();
private final Map<WrappedStack, EnergyValue> preCalculationMappings;
private final Map<WrappedStack, EnergyValue> postCalculationMappings;
public static File energyValuesDataDirectory;
public static File energyValuesDataFile;
private NewEnergyValueRegistry() {
preCalculationMappings = new TreeMap<>();
// Loading up some dummy values for testing serialization
@ -39,8 +42,7 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
}
public String toJson() {
// FIXME This shouldn't be a new object all the time - we should be using an instance of a single serializer
return new GsonBuilder().setPrettyPrinting().registerTypeAdapter(NewEnergyValueRegistry.class, new NewEnergyValueRegistry()).create().toJson(this);
return EquivalentExchange3.GSON.toJson(this);
}
/**
@ -48,8 +50,7 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
*/
public void save() throws IOException {
// TODO We should probably keep the references to the energy value data dir and file in this class and not in Files. You know, anti-patterns and stuff.
File file = Files.energyValuesDataFile;
File file = energyValuesDataFile;
File tempFile = new File(file.getAbsolutePath() + "_tmp");
if (tempFile.exists()) {
@ -96,8 +97,8 @@ public class NewEnergyValueRegistry implements JsonSerializer<NewEnergyValueRegi
public JsonElement serialize(NewEnergyValueRegistry src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonRegistry = new JsonObject();
jsonRegistry.add("pre_calculation_value_mappings", context.serialize(src.preCalculationMappings, ENERGY_VALUE_MAP_TYPE)); // TODO String constant for property name
jsonRegistry.add("post_calculation_value_mappings", context.serialize(src.postCalculationMappings, ENERGY_VALUE_MAP_TYPE)); // TODO String constant for property name
jsonRegistry.add("pre_calculation_value_mappings", context.serialize(src.preCalculationMappings)); // TODO String constant for property name
jsonRegistry.add("post_calculation_value_mappings", context.serialize(src.postCalculationMappings)); // TODO String constant for property name
return jsonRegistry;
}
}

View file

@ -1,5 +1,6 @@
package com.pahimar.ee3.reference;
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import java.io.File;
@ -12,11 +13,10 @@ public class Files {
public static final String ENERGY_VALUES_JSON = "energy-values.json";
public static File dataDirectory;
public static File energyValuesDataDirectory;
public static File abilitiesDataDirectory;
public static File knowledgeDataDirectory;
public static File energyValuesDataFile;
public static File abilitiesDataFile;
public static File knowledgeDataFile;
@ -24,9 +24,9 @@ public class Files {
dataDirectory = new File(event.getModConfigurationDirectory().getParentFile(), "data" + File.separator + Reference.LOWERCASE_MOD_ID);
energyValuesDataDirectory = new File(dataDirectory, "energy-values");
energyValuesDataDirectory.mkdirs();
energyValuesDataFile = new File(energyValuesDataDirectory, ENERGY_VALUES_JSON);
NewEnergyValueRegistry.energyValuesDataDirectory = new File(dataDirectory, "energy-values");
NewEnergyValueRegistry.energyValuesDataDirectory.mkdirs();
NewEnergyValueRegistry.energyValuesDataFile = new File(NewEnergyValueRegistry.energyValuesDataDirectory, ENERGY_VALUES_JSON);
abilitiesDataDirectory = new File(dataDirectory, "abilities");
abilitiesDataDirectory.mkdirs();

View file

@ -0,0 +1,92 @@
package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import java.lang.reflect.Type;
public class FluidStackSerializer implements JsonSerializer<FluidStack>, JsonDeserializer<FluidStack> {
// TODO String constants for property names
@Override
public FluidStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonObject()) {
JsonObject jsonObject = (JsonObject) json;
String fluidName = null;
int fluidAmount = Integer.MIN_VALUE;
NBTTagCompound nbtTagCompound = null;
try {
if (jsonObject.get("fluidName").getAsJsonPrimitive().isString()) {
fluidName = jsonObject.get("fluidName").getAsString();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("fluidAmount").getAsJsonPrimitive().isNumber()) {
fluidAmount = jsonObject.get("fluidAmount").getAsInt();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("fluidTagCompound").getAsJsonPrimitive().isString()) {
NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("fluidTagCompound").getAsString());
if (nbtBase instanceof NBTTagCompound) {
nbtTagCompound = (NBTTagCompound) nbtBase;
}
}
}
catch (IllegalStateException exception) {
}
catch (NBTException e) {
}
if (fluidName != null) {
Fluid fluid = FluidRegistry.getFluid(fluidName);
if (fluid != null && fluidAmount >= 0) {
FluidStack fluidStack = new FluidStack(fluid, fluidAmount);
if (nbtTagCompound != null) {
fluidStack.tag = nbtTagCompound;
}
return fluidStack;
}
}
}
return null;
}
@Override
public JsonElement serialize(FluidStack src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("fluidName", src.getFluid().getName());
jsonObject.addProperty("fluidAmount", src.amount);
if (src.tag != null) {
jsonObject.addProperty("fluidTagCompound", src.tag.toString());
}
return jsonObject;
}
return null;
}
}

View file

@ -0,0 +1,96 @@
package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import java.lang.reflect.Type;
public class ItemStackSerializer implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
// TODO String constants for property names
@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonObject()) {
JsonObject jsonObject = (JsonObject) json;
String itemName = null;
int itemDamage = Integer.MIN_VALUE;
NBTTagCompound nbtTagCompound = null;
try {
if (jsonObject.get("itemName").getAsJsonPrimitive().isString()) {
itemName = jsonObject.get("itemName").getAsString();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("itemDamage").getAsJsonPrimitive().isNumber()) {
itemDamage = jsonObject.get("itemDamage").getAsInt();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("itemTagCompound").getAsJsonPrimitive().isString()) {
NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get("itemTagCompound").getAsString());
if (nbtBase instanceof NBTTagCompound) {
nbtTagCompound = (NBTTagCompound) nbtBase;
}
}
}
catch (IllegalStateException exception) {
}
catch (NBTException e) {
}
if (itemName != null) {
Item item = (Item) Item.itemRegistry.getObject(itemName);
if (item != null) {
ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(itemName));
if (itemDamage >= 0) {
itemStack.setItemDamage(itemDamage);
}
if (nbtTagCompound != null) {
itemStack.setTagCompound(nbtTagCompound);
}
return itemStack;
}
}
}
return null;
}
@Override
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("itemName", Item.itemRegistry.getNameForObject(src.getItem()));
jsonObject.addProperty("itemDamage", src.getItemDamage());
if (src.getTagCompound() != null) {
jsonObject.addProperty("itemTagCompound", src.getTagCompound().toString());
}
return jsonObject;
}
return null;
}
}

View file

@ -0,0 +1,43 @@
package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import com.pahimar.ee3.exchange.OreStack;
import java.lang.reflect.Type;
public class OreStackSerializer implements JsonSerializer<OreStack>, JsonDeserializer<OreStack> {
// TODO String constants for property names
@Override
public OreStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonObject()) {
JsonObject jsonObject = (JsonObject) json;
try {
if (jsonObject.get("oreName").getAsJsonPrimitive().isString()) {
String oreName = jsonObject.get("oreName").getAsString();
return new OreStack(oreName);
}
}
catch (IllegalStateException exception) {
// TODO We could probably log here that an invalid piece of data was found
}
}
return null;
}
@Override
public JsonElement serialize(OreStack src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("oreName", src.oreName);
return jsonObject;
}
return null;
}
}

View file

@ -0,0 +1,77 @@
package com.pahimar.ee3.util.serialize;
import com.google.gson.*;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import java.lang.reflect.Type;
public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, JsonDeserializer<WrappedStack> {
// TODO String constants for property names
@Override
public WrappedStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonObject()) {
JsonObject jsonObject = (JsonObject) json;
String type = null;
int stackSize = Integer.MIN_VALUE;
String data = null;
try {
if (jsonObject.get("type").getAsJsonPrimitive().isString()) {
type = jsonObject.get("type").getAsString();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("stackSize").getAsJsonPrimitive().isNumber()) {
stackSize = jsonObject.get("stackSize").getAsInt();
}
}
catch (IllegalStateException exception) {
}
try {
if (jsonObject.get("data").getAsJsonPrimitive().isString()) {
data = jsonObject.get("data").getAsString();
}
}
catch (IllegalStateException exception) {
}
if ("itemstack".equalsIgnoreCase(type) || "orestack".equalsIgnoreCase(type) || "fluidstack".equalsIgnoreCase(type)) {
if (stackSize >= 1) {
// TODO PICK UP HERE
// TODO ALSO LOOK INTO MOVING THESE ALL INTO SerializationHelper
}
}
}
return null;
}
@Override
public JsonElement serialize(WrappedStack src, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject jsonObject = new JsonObject();
if (src.getWrappedObject() instanceof ItemStack || src.getWrappedObject() instanceof OreStack || src.getWrappedObject() instanceof FluidStack) {
jsonObject.addProperty("type", src.getWrappedObject().getClass().getSimpleName().toLowerCase());
jsonObject.add("data", context.serialize(src.getWrappedObject()));
jsonObject.addProperty("stackSize", src.getStackSize());
return jsonObject;
}
return null;
}
}