public void Deserialize(string FileName, ref FigList Figures, List <Type> Types) { using (Stream FileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.None)) { string str; using (StreamReader reader = new StreamReader(FileStream)) { str = reader.ReadLine(); } Regex Pattern = new Regex("\\{\"__type\":\"(.+?):"); bool AllIsReplased = false; int i = 0; while (!AllIsReplased) { MatchCollection Matches = Pattern.Matches(str); if (Matches.Count != 0) { if (!IsInTypes(Matches[i].Groups[1].Value, Types)) { DeleteType(Matches[i].Groups[0].Index, ref str); } else { if (Matches.Count == i + 1) { AllIsReplased = true; } i++; } } else { AllIsReplased = true; } } foreach (Match match in Pattern.Matches(str)) { if (!IsInTypes(match.Groups[1].Value, Types)) { DeleteType(match.Groups[0].Index, ref str); } } byte[] buff = Encoding.Default.GetBytes(str); MemoryStream stream = new MemoryStream(buff); DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(Figures.GetType(), Types); Figures = (FigList)jsonFormatter.ReadObject(stream); } }
public void Serialize(string FileName, FigList Figures, List <Type> Types) { using (Stream FileStream = new FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)) { DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(Figures.GetType(), Types); jsonFormatter.WriteObject(FileStream, Figures); } }