我有一个大小为50GB及以上的Json文件.以下是我所写的阅读Json的一小部分内容.我现在需要修改它来读取大文件.
internal static IEnumerable<T> ReadJson<T>(string filePath)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (StreamReader sr = new StreamReader(filePath))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
byte[] jsonBytes = Encoding.UTF8.GetBytes(line);
XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, XmlDictionaryReaderQuotas.Max);
var myPerson = ser.ReadObject(jsonReader);
jsonReader.Close();
yield return (T)myPerson;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它将一次读取一行(输入文件),可能是10个字节,可能都是50GB.所以它归结为:输入文件是如何构建的?如果输入JSON有换行符其他比干净的物体之间的断裂,这有可能会真的病.
缓冲区大小可能会影响它在查找每一行结尾时读取的数量,但最终:它需要每次都找到一个换行符(至少,当前是如何写入的).
| 归档时间: |
|
| 查看次数: |
4676 次 |
| 最近记录: |