1

So i have WeaponStats class that contains one of classes that inherit from IDamageType. Fire is empty bacause i didn't receive anything back from game designer and its still empty. But i can add anything to code. When i try to Serialize it, it doesn't get any thing back to deserialize to. And my question is what is a better way to Serialize and Deserialize Fire and any similar class? I Edited to add new information. JsonCalculator calculates needed stats and returns them. And Staff.json is an exemplar of json i used for deserelialization. Last code sample is what i get when i serialize WeaponStats with Fire.

public class WeaponStats : IEquipmentStats { public int Damage; public IDamageType DamageType; public int Range; public int ActionPointsCost; public float CritDamageChance; public float CritDamage; public int SkillCooldown; public int ElementalDamage; } 
public interface IDamageType { } 
public class Fire : IDamageType { } 
public static class JSONCalculator<T> where T : class, IEquipmentStats { static string path = @"Assets\_Project\Scripts\Units\Systems\Equipment\Exemplars\"; public static T GetEquipmentStats(string fileName,int level) { string newPath = Path.Combine(Environment.CurrentDirectory, (path + fileName+".json")); string jsonString = File.ReadAllText(newPath); Dictionary<string, T> results = JsonConvert.DeserializeObject<Dictionary<string,T>>(jsonString);// bug string levelStr = "level"; switch (level) { case 0: levelStr += "Zero"; break; case 1: levelStr += "One"; break; case 2: levelStr += "Two"; break; case 3: levelStr += "Three"; break; } return results[levelStr]; } } 

Staff.json

{ "levelZero": { "Damage": 10, "DamageType": {}, "Range": 4, "ActionPointsCost": 2, "CritDamageChance": 5, "CritDamage": 150, "SkillCooldown": 2, "ElementalDamage": 2 }, "levelOne": { "Damage": 20, "DamageType": {}, "Range": 4, "ActionPointsCost": 2, "CritDamageChance": 5, "CritDamage": 150, "SkillCooldown": 2, "ElementalDamage": 2 }, "levelTwo": { "Damage": 30, "DamageType": {}, "Range": 4, "ActionPointsCost": 2, "CritDamageChance": 5, "CritDamage": 150, "SkillCooldown": 2, "ElementalDamage": 2 }, "levelThree": { "Damage": 40, "DamageType": {}, "Range": 4, "ActionPointsCost": 2, "CritDamageChance": 5, "CritDamage": 150, "SkillCooldown": 2, "ElementalDamage": 2 } } 
{\"Damage\":0,\"DamageType\":{},\"Range\":0,\"ActionPointsCost\":0,\"CritDamageChance\":0.0,\"CritDamage\":0.0,\"SkillCooldown\":0,\"ElementalDamage\":0}" 
public interface IEquipmentStats { } 
2
  • What exactly do you want it to serialize? There are no properties to serialize. You'd need a discriminator property at the least to tell it which type to deserialize to. Commented Sep 28 at 15:35
  • 1
    Possible duplicate: stackoverflow.com/questions/2254872/… Commented Sep 28 at 15:37

1 Answer 1

4

You want to deserialize a structure that does not correspond to your data.

You write :

Dictionary<string, T> results = JsonConvert.DeserializeObject<Dictionary<string,T>>(jsonString); 

This line said that you want to deserialize a json like this (consider T is int) :

{ "a": 1, "b": 2, "c": 3, "d": 4, } 

This will works for the line described : https://dotnetfiddle.net/6l3J9Q

But in you case, you have an interface that can't be solved without a little help.

You can see in this sample what it is different : https://dotnetfiddle.net/XbmKeO

When you deserialize object with interface property, you need to have the indication of whihc type the converter should deserialize to.

Please read this article that explained that very well: Using Json.NET converters to deserialize properties

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i used this one to fix it. ``` public class Foo { public int Number { get; set; } // Add "$type" property containing type info of concrete class. [JsonProperty( TypeNameHandling = TypeNameHandling.Objects )] public ISomething { get; set; } } ```

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.