c#反序列化时出错:集合的大小固定

时间:2017-11-23 16:03:24

标签: c# json.net json-deserialization

我试图对一个对象进行序列化和反序列化,在第二部分(full json),它不适用于它的某个特定部分:

"Players": {
   "$type": "Player[], Classes",
    "$values": [
      {
        "$type": "Human, Classes",
        "Color": 1,
        "NbAction": 0,
        "Name": "toto"
      },
      {
        "$type": "IA, Classes",
        "Level": {
          "$type": "NoobLevel, Classes"
        },
        "Color": 0,
        "NbAction": 0,
        "Name": "tata"
      }
    ]
},

我明白了一个相当普通的问题:Collection was of fixed size但是因为没有堆栈痕迹或者我不知道该去哪里,因为我不知道不习惯c#它不是很容易

我首先得到此错误,因为某个元素是在构造函数中创建的,所以我为JSON创建了另一个具有良好参数的元素,但对于这个...我将Console.write()放入每个构造函数都帮助我,但不是

存储的相关类:

public class MementoGame
{
    private Board _plateau;
    private List<Player> _players;
    private int _numTurn;
    private int indexCurrentPlayer;

    //properties

    public MementoGame(Board plateau, Player[] players, int numTurn, int index)
    {
        Console.WriteLine("CONSTRUC MEMENTO BASE");
        _plateau = plateau; Players = players;  _numTurn = numTurn; IndexCurrentPlayer = index;
    }

    [JsonConstructor]
    public MementoGame(Board plateau, int numTurn, int indexCurrentPlayer, params Player[] players)
    {
        Console.WriteLine("CONSTRUC MEMENTO JSON");
        _plateau = plateau;
        for (int i = 0; i < players.Length; i++)
        {
            Console.Write(players[i] + " / ");                
        }
        _players = new List<Player>(players);
        _numTurn = numTurn;
        IndexCurrentPlayer = indexCurrentPlayer;
    }
}

序列化的方法:

public void saveToFile()
{
    string json = JsonConvert.SerializeObject(getMementoGame(), Formatting.Indented ,
           new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
    File.WriteAllText(@"C:\Users\...\care\" + link, json);
}

public void loadFromFile()
{
    string json = File.ReadAllText(@"C:\Users\...\care\" + link);
    MementoGame mem = JsonConvert.DeserializeObject<MementoGame>(json,
        new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All});
}
  • MementoGame无法工作的无参数构造函数
  • List<Player> players而不是params Player[] players无法正常工作

完整追踪:

System.NotSupportedException: La collection était d'une taille fixe.
à System.Array.System.Collections.IList.Add(Object value)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
à Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
à Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
à Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
à Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
à CaretakerGame.loadFromFile() dans C:\Users\...\memento\game\CaretakerGame.cs:ligne 52
à TestCaretakerGame.loadFromFileTest() dans C:\Users\...\memento\game\TestCaretakerGame.cs:ligne 75

0 个答案:

没有答案