例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象,或者和其它应用程序共享使用。.
一般来说序列化有以下几种
string strobj = "serialization";
FileStream stream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Create, FileAccess.Write,FileShare.None);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, strobj);
stream.Close();
FileStream readstream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Open, FileAccess.Read,
FileShare.Read);
string readdata = (string)formatter.Deserialize(readstream);
readstream.Close();
Console.WriteLine(readdata);
Console.ReadLine();
string strobj = "serialization";
FileStream stream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Create, FileAccess.Write ,FileShare.None);
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, strobj);
stream.Close();
FileStream readstream = new FileStream("C:\\Users\\JingWang\\Desktop\\11.txt", FileMode.Open , FileAccess.Read ,
FileShare.Read );
string readdata = (string)formatter.Deserialize(readstream);
readstream.Close();
Console.WriteLine(readdata);
Console.ReadLine();
string strobj = "test string for serialization";
FileStream stream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Create, FileAccess.Write ,FileShare.None);
XmlSerializer xmlserializer = new XmlSerializer(typeof(string));
xmlserializer.Serialize(stream, strobj);
stream.Close();
FileStream readstream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Open , FileAccess.Read ,FileShare.Read );
string readdata = (string)xmlserializer.Deserialize(readstream);
Console.WriteLine(readdata);
Console.ReadLine();
string strobj = "test string for serialization";
FileStream stream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Create, FileAccess.Write,FileShare.None);
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(string));
formatter.WriteObject(stream, strobj);
stream.Close();
FileStream readstream = new FileStream("C:\\Users\\Jing\\Desktop\\11.txt", FileMode.Open, FileAccess.Read,FileShare.Read);
string readdata = (string)formatter.ReadObject(readstream);
readstream.Close();
Console.WriteLine(readdata);
Console.ReadLine();
在C#中,如果想要进行自定义类的序列化,只需要简单地在定义类的时候添加Serializable标签即可。除非类的成员标记了[NonSerializable],序列化会将类中的所有成员都序列化。
[Serializable]
public class Person
{
public string name;
public int age;
}
在游戏开发中,我们需要保存玩家的数据,而玩家是一个类的实例,也就是对象。
我们需要把这个对象存储起来,下次加载游戏的时候,把存储起来的数据重新装载成对象
例如Unity,我们可以使用JsonUtility,把对象转换为Json字符串,然后存到本地文件里
这里我是用的是Unity自带的PlayerPrefs存储到本地,大家也可以使用File文件流存储
///
/// 保存数据
///
/// 需要存储的对象
/// 键
private void SaveData(Object data, string key)
{
PlayerPrefs.SetString(key, JsonUtility.ToJson(data));//先使用JsonUtility把对象序列化为Json字符串,然后使用PlayerPrefs存储到本地
PlayerPrefs.Save();//Set完之后一定要调用Save保存,不然只是写入到内存中,没有写入磁盘
}
///
/// 加载数据
///
/// 需要加载的对象
/// 键
/// 是否加载成功
private bool LoadData(Object data, string key)
{
if (PlayerPrefs.HasKey(key))
{
JsonUtility.FromJsonOverwrite(PlayerPrefs.GetString(key), data);//先拿到对应的Json数据,然后使用JsonUtility反序列化为对象
return true;
}
return false;
}