I am trying to serialize and de-serialize objects to Json files to a Dictionary using Json.Net. The serialization works great and I can see all the data in the file. But when I try and de-serialize it fails on populating a System.Collections.BitArray. Are BitArrays not properly supported?
The Json file appears to have the correct values and in the correct form. I've also stepped through the code and it builds the object correctly only failing to set the value of the BitArray. It has been working correctly so far for all objcets, only failing once I introduced an object with a BitArray.
The Failing Object
[DataContract]
public class Chip
{
[DataMember]
public Guid ID { get; set; }
[DataMember]
public BitArray Input { get; set; } //Failing on setting this value
[DataMember]
public BitArray Output { get; set; }
[DataMember]
public List<Gate> Gates { get; set; }
[DataMember]
public List<Chip> Chips { get; set; }
[DataMember]
public Dictionary<Guid, List<Wire>> WireDict { get; set; }
[DataMember]
protected BitArray Dirty { get; set; }
protected Chip(int inputs, int outputs)
{
ID = Guid.NewGuid();
Input = new BitArray(inputs, false);
Output = new BitArray(outputs, false);
Dirty = new BitArray(outputs, false);
Gates = new List<Gate>();
Chips = new List<Chip>();
WireDict = new Dictionary<Guid, List<Wire>>();
}
}
The Code I'm using to serialize
using(StreamWriter file = File.CreateText(filePath))
{
JsonSerializer serializer = new JsonSerializer
{
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented
};
serializer.Serialize(file, componentsDict);
}
The Code I'm using to de-serialize
using (StreamReader file = File.OpenText(filePath))
{
JsonSerializer serializer = new JsonSerializer();
serializer.TypeNameHandling = TypeNameHandling.Auto;
Dictionary<Guid, ChipWrapper> componentsDict = (Dictionary<Guid, ChipWrapper>)serializer.Deserialize(file, typeof(Dictionary<Guid, ChipWrapper>));
}
I get the error
JsonSerializationException: Cannot populate list type System.Collections.BitArray. Path 'a77af562-0e5e-4471-86c5-06857610ae6d.Chip.Input', line 612, position 16.
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) (at <97722d3abc9f4cf69f9e21e6770081b3>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType,
Etc...
The Dictionary holds a class which a lot of other classes are derived from but only the classes with bit arrays are failing.