Work on "better" json serialization of data objects
This commit is contained in:
parent
31b9d45dfe
commit
870bea043c
|
@ -3,7 +3,6 @@ package com.pahimar.ee3.exchange;
|
|||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.util.LogHelper;
|
||||
import com.pahimar.ee3.util.SerializationHelper;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
|
@ -27,20 +26,12 @@ public class NewEnergyValueRegistry {
|
|||
public static File energyValuesDataFile;
|
||||
|
||||
private NewEnergyValueRegistry() {
|
||||
|
||||
preCalculationMappings = new TreeMap<>();
|
||||
// Loading up some dummy values for testing serialization
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.apple), new EnergyValue(1));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.arrow), new EnergyValue(2));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.baked_potato), new EnergyValue(3));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.bed), new EnergyValue(4));
|
||||
preCalculationMappings.put(WrappedStack.wrap(new OreStack("oreIron")), new EnergyValue(5));
|
||||
|
||||
|
||||
postCalculationMappings = new TreeMap<>();
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
LogHelper.info(SerializationHelper.GSON.toJson(this));
|
||||
return SerializationHelper.GSON.toJson(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,16 +6,10 @@ import com.google.gson.GsonBuilder;
|
|||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
||||
import com.pahimar.ee3.exchange.OreStack;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.exchange.*;
|
||||
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
|
||||
import com.pahimar.ee3.reference.Reference;
|
||||
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 com.pahimar.ee3.util.serialize.*;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.ModContainer;
|
||||
|
@ -35,6 +29,7 @@ public class SerializationHelper {
|
|||
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
|
||||
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
|
||||
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
|
||||
.registerTypeAdapter(NewEnergyValueRegistry.class, new NewEnergyValueRegistrySerializer())
|
||||
.create();
|
||||
|
||||
private static File instanceDataDirectory;
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package com.pahimar.ee3.util.serialize;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
public class NewEnergyValueRegistrySerializer implements JsonSerializer<NewEnergyValueRegistry>, JsonDeserializer<NewEnergyValueRegistry> {
|
||||
|
||||
private static final String PRE_CALCULATION_ASSIGNMENTS = "pre_calculation_assignments";
|
||||
private static final String POST_CALCULATION_ASSIGNMENTS = "post_calculation_assignments";
|
||||
private static final String ENERGY_VALUE = "energyValue";
|
||||
|
||||
@Override
|
||||
public NewEnergyValueRegistry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(NewEnergyValueRegistry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
if (src != null) {
|
||||
JsonObject registryObject = new JsonObject();
|
||||
|
||||
registryObject.add(PRE_CALCULATION_ASSIGNMENTS, test(src.preCalculationMappings, context));
|
||||
registryObject.add(POST_CALCULATION_ASSIGNMENTS, test(src.postCalculationMappings, context));
|
||||
|
||||
return registryObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static JsonArray test(Map<WrappedStack, EnergyValue> valueMap, JsonSerializationContext context) {
|
||||
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
for (WrappedStack wrappedStack : valueMap.keySet()) {
|
||||
JsonObject jsonMapping = new JsonObject();
|
||||
jsonMapping.add(wrappedStack.getWrappedObject().getClass().getSimpleName().toLowerCase(), context.serialize(wrappedStack.getWrappedObject()));
|
||||
jsonMapping.addProperty(ENERGY_VALUE, valueMap.get(wrappedStack).getValue());
|
||||
jsonArray.add(jsonMapping);
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue