我在将一个非常复杂的类序列化为存储在手机LocalFolder中的文件时遇到了很多问题。我用newtonSoft软件包和xmlserializer尝试了Json seriliazitaion。
这是我尝试从文件存储和加载的类:
public class Gear
{
public int LastUsedGroup { get; set; }
public List<GearGroupName> GearTree { get; set; }
}
public class GearItem
{
public string Description { get; set; }
public bool ToTake { get; set; }
public bool Done { get; set; }
public bool DoneEnabled { get; set; }
public SolidColorBrush Background { get; set; }
public double Opacity { get; set; }
public int Index { get; set; }
}
public class GearGroupName
{
public string GroupName { get; set; }
public List<GearItem> GearGroup = new List<GearItem>();
}
我在课堂上有一些硬编码信息。但是当尝试使用这个序列化到文件中时(见下文)我得到错误(JullSerialization的NullReference和Json的StackOverFlow错误)。
命名空间ReadWriteUniversal {
class XmlIO
{
/// <summary>
/// Reads XML content from a file ("filename") and returns an object from the XML
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filename"></param>
/// <returns></returns>
public static async Task<T> ReadObjectFromXmlFileAsync<T>(string filename)
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(filename);
Stream stream = await file.OpenStreamForReadAsync();
T objectFromXml = default(T);
var serializer = new XmlSerializer(typeof(T));
objectFromXml = (T)serializer.Deserialize(stream);
stream.Dispose();
return objectFromXml;
}
/// <summary>
/// Stores an object in XML format in file called 'filename'
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objectToSave"></param>
/// <param name="filename"></param>
/// <returns></returns>
public static async Task SaveObjectToXml<T>(T objectToSave, string filename)
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
Stream stream = await file.OpenStreamForWriteAsync();
Type t = typeof(T);
XmlSerializer serializer = new XmlSerializer(t);
using (stream)
{
serializer.Serialize(stream, objectToSave);
}
}
}
class JsonIO
{
public static async Task<T> ReadObjectFromJsonFileAsync<T>(string filename)
{
try
{
// this reads XML content from a file ("filename") and returns an object from the XML
//T objectFromXml = default(T);
//var serializer = new XmlSerializer(typeof(T));
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(filename);
//Stream stream = await file.OpenStreamForReadAsync();
Debug.WriteLine("Starting Json reading");
string strJson = await Windows.Storage.FileIO.ReadTextAsync(file);
Debug.WriteLine("Read" + strJson);
return (T)Newtonsoft.Json.JsonConvert.DeserializeObject(strJson);
//objectFromXml = (T)serializer.Deserialize(stream);
//stream.Dispose();
//return objectFromXml;
}
catch (Exception)
{
return default(T);
}
}
public static async Task SaveObjectToJson<T>(T objectToSave, string filename)
{
// stores an object in XML format in file called 'filename'
//XmlSerializer serializer = new XmlSerializer(typeof(T));
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
//Stream stream = await file.OpenStreamForWriteAsync();
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(objectToSave);
Debug.WriteLine("Write- " + strJson);
await Windows.Storage.FileIO.WriteTextAsync(file, strJson);
//using (stream)
//{
// serializer.Serialize(stream, objectToSave);
//}
}
}
}
知道为什么会这样吗?
如果有帮助,这里是用于测试目的的硬编码部分。
private void staticData()
{
// TODO:
// LinkGroupName-ak sartu. Pivot-etan agertuko direnak
// MENDIA ELURRAREKIN
GearGroupName ggn = new GearGroupName();
ggn.GroupName = "Mendiko materiala elurrarekin";
// Group bakoitzari elementuak gehitu
GearItem gear;
gear = new GearItem();
gear.Description = "Toaila";
gear.Index = 0;
gear.DoneEnabled = gear.ToTake;
if (gear.ToTake)
gear.Opacity = 1.0;
else
gear.Opacity = 0.5;
ggn.GearGroup.Add(gear);
gear = new GearItem();
gear.Description = "Kamara estalkia";
gear.Index = 1;
gear.ToTake = true;
gear.DoneEnabled = gear.ToTake;
if (gear.ToTake)
gear.Opacity = 1.0;
else
gear.Opacity = 0.5;
ggn.GearGroup.Add(gear);
gear = new GearItem();
gear.Description = "Eskularruak";
gear.Index = 2;
gear.ToTake = true;
gear.DoneEnabled = gear.ToTake;
if (gear.ToTake)
gear.Opacity = 1.0;
else
gear.Opacity = 0.5;
ggn.GearGroup.Add(gear);
myGear.GearTree.Add(ggn);
// KOSTA
ggn = new GearGroupName();
ggn.GroupName = "Kosta";
// Group bakoitzari elementuak gehitu
gear = new GearItem();
gear.Description = "Katiuskak";
ggn.GearGroup.Add(gear);
gear = new GearItem();
gear.Description = "Uretako oinetakoak";
ggn.GearGroup.Add(gear);
gear = new GearItem();
gear.Description = "Badeadorea";
ggn.GearGroup.Add(gear);
myGear.GearTree.Add(ggn);
// BIDAIATZEKO
ggn = new GearGroupName();
ggn.GroupName = "Bidai materiala";
gear = new GearItem();
gear.Description = "Tripodea";
ggn.GearGroup.Add(gear);
myGear.GearTree.Add(ggn);
}