我发现了两个如何序列化数据的不同示例。一个使用流,另一个不使用?
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.Write("JSON form of Person object: ");
Console.WriteLine(sr.ReadToEnd());
public static string ToJson(this object obj, int recursionDepth = 100)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
return serializer.Serialize(obj);
}
public static List<T> ToListObject<T>(this string obj, int recursionDepth = 100)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RecursionLimit = recursionDepth;
List<T> returnList = serializer.Deserialize<List<T>>(obj);
return returnList;
}
为什么要使用 Stream 作为附加步骤?