Quantcast
Channel: Latest Questions by Ricewind1
Viewing all articles
Browse latest Browse all 16

JsonFX and Polymorphism?

$
0
0
I'm trying to store (readable) data in a text-file as an asset since the class I'm serializing contains many things that Unity itself can't handle. A problem is though, that even though everything saves and loads, only the base-type is returned. I did a lot of searches on the internet to find a solution and I've tried everything recommended but alas, the problem still resides. I'm trying to serialize a class with 2 fields, one int and one Dictionary; Item is the base class with a large variety of children I've had to convert int to string to allow jsonfx to de-serialize the dictionary. The following code is used to serialize and de-serialize the data. public static string FiletoJSON(object T) { try { StringBuilder sb = new StringBuilder(); JsonWriterSettings settings = JsonDataWriter.CreateSettings(false); settings.TypeHintName = "__type"; JsonWriter writer = new JsonWriter(sb, settings); writer.Write(T); return sb.ToString(); } catch(Exception e) { Debug.LogWarning(e.InnerException); return null; } } public static T JSONtoFile(string JSONString) { try { JsonReaderSettings settings = JsonDataReader.CreateSettings(true); settings.TypeHintName = "__type"; using (TextReader textReader = new StringReader(JSONString)) { var jsonReader = new JsonReader(textReader, settings); return jsonReader.Deserialize(); } } catch (Exception e) { Debug.LogWarning(e); return default(T); } } The following is the output of the string. { "__type": "ItemDatabase, Assembly-CSharp", "DatabaseID": 1, "ItemList": { "1": { "__type": "Sniper, Assembly-CSharp", "MaxAmountOfItem": 1, "RangedAttack": 950, "WeaponObjectID": 3, "weaponType": "Sniper", "Accuracy": 100, "MaxEnergy": 437, "SkillSlots": null, "StatusEffects": null, "EnergyRestorePerTick": 8, "EnergyRestoreOverTime": true, "Variance": 30, "Cooldown": 1, "Name": "TestSniper", "Description": "", "ID": 1, "Points": 1, "Tier": 8 } } } How can I make sure that JsonFX properly de-serializes my sub-types too? Note: The purpose of this, is storing several dictionaries filled with supertypes. I've managed to accomplish this with a binaryformatter, but there is a noticeable performance reduction compared to using JSON. On top of this, the data in the text file is unreadable.

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>