Object with Dictionary<object, int> Serialization

Feb 24, 2013 at 9:17 AM
Edited Feb 24, 2013 at 9:18 AM
I am having a pretty difficult time trying to get this to serialize properly. Any help in solving this would be great, thanks! I am trying to serialize the Army object Units so it saves the UnitData as well. Any ideas? Thanks again.

Here is what I get
{
  "$type": "Army, Assembly-CSharp",
  "ArmyName": null,
  "TotalPointCost": 0,
  "Units": {
    "$type": "System.Collections.Generic.Dictionary`2[[UnitModel, Assembly-CSharp],[System.Int32, mscorlib]], mscorlib",
    "UnitModel": 2,
    "UnitModel": 1
  }
}
public class Army 
{
    public string ArmyName
    {
        get;
        private set;
    } // eo ArmyName
    
    public int TotalPointCost
    {
        get;
        private set;
    } // eo TotalPointCost

    public Dictionary<UnitModel, int> Units
    {
        get;
        private set;
    } // eo Units
    
    public Army()
    {
        Units = new Dictionary<UnitModel, int>();
    } // eo Army

public void Serialize()
    {
        KeyValuePairConverter[] converter = { new KeyValuePairConverter( ) };
        DefaultContractResolver contractResolver = new DefaultContractResolver();
           contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
        JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings() {
                       Formatting = Formatting.Indented,
                       TypeNameHandling = TypeNameHandling.All,
                       ContractResolver = contractResolver
                        
         };
        jsonSerializerSettings.Converters.Add(new KeyValuePairConverter());
        jsonSerializerSettings.Converters.Add(new StringEnumConverter());
        string json = JsonConvert.SerializeObject(this, Formatting.Indented, jsonSerializerSettings);
        Debug.Log(json);
    }
}

public class UnitModel 
{   
    
    public int AttackDamage
    {
        get;
        private set;
    } // eo Attack
    
    public int AttackRange
    {
        get;
        private set;
    } // eo AttackRange
    
    public int Defense
    {
        get;
        private set;
    } // eo Defense

    public bool DoesFly
    {
        get;
        private set;
    }// eo DoesFly
}