I was trying to do my own custom serialization/deserialization of various objects and using the SpellbornHunter repo and the JSON tutorial to guide me. I caught the bit about the Vector3 conversion and how the Newtownsoft Utility can’t handle things like structs.
Then in the sample code from Spellborn Hunter I see attempts to serialize and deserialize structs and dictionaries. The code in SpellbornHunter is much shorter and cleaner than what I’ve been trying to do.
(De)Serializing an array of structs looks very easy but this looks like a contradiction of what’s in the tutorial. It’s so easy. It’s just one line of code that handles the conversion process to/from a JToken to an array of structs.
Question: What important nuance am I misunderstanding to allow for such simple code in Spellborn Hunter but more complex code in the tutorial?
Inventory.cs
[System.Serializable]
private struct InventorySlotRecord
{
public string itemID;
public int number;
public JToken state;
}
public virtual JToken CaptureState()
{
var slotStrings = new InventorySlotRecord[inventorySize];
//code continues with no access to JSON utility
return JToken.FromObject(slotStrings);
}
public void RestoreFromJToken(JToken state)
{
var slotStrings = state.ToObject<InventorySlotRecord[]>();
//code continues with no additional use of JSON utility
}
StatsEquipableItem.cs
public override JToken CaptureAsJToken()
{
JObject state = new JObject();
IDictionary<string, JToken> stateDict = state;
stateDict["level"] = JToken.FromObject(GetLevel());
stateDict["additive"] = JToken.FromObject(actualAdditiveModifiers);
stateDict["percentage"] = JToken.FromObject(actualPercentageModifiers);
return state;
}
public override void RestoreFromJToken(JToken state)
{
IDictionary<string, JToken> stateDict = (JObject)state;
SetLevel(stateDict["level"].ToObject<int>());
actualAdditiveModifiers = stateDict["additive"].ToObject<Dictionary<EStat, float>>();
actualPercentageModifiers = stateDict["percentage"].ToObject<Dictionary<EStat, float>>();
}