我需要读取 30 MB JSON 文件并将结果作为 JSON 数据进行流式传输。我找到了以下代码,但该代码序列化了一个大对象,这会产生内存峰值。如何更改代码以从 JSON 文本文件流式传输部分数据,然后将数据作为 JSON 流式传输回调用应用程序?
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new PushStreamContent((stream, content, context) =>
{
using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
using (JsonTextWriter jtw = new JsonTextWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jtw, hugeObject); //don't serialize the entire object.
//Read in some of the file and stream it back out until the entire text file is consumed.
}
}, "application/json");
编辑-巨大对象是一个json数据数组:
[
{
"agent": "name1",
"entityCode": "010002",
"state": "AL",
"memberType": "U",
"producerCode": "572714"
},
{
"agent": "name2",
"entityCode": "010002",
"state": "AL",
"memberType": "U",
"producerCode": "572714"
}
]
更新 - 我有以下工作,但代码创建了一个大字符串。我想改为流式传输数据。
[HttpGet, Route("/api/GetAllData/{callingApp}")]
public IActionResult GetAllData(
[RegularExpression(@"^[a-zA-Z0-9_-]*$"), Required, MaxLength(50)] string callingApp)
{
string fileName = _config.GetSection("AllAgentsPath").Value;
StreamReader reader = new StreamReader(fileName);
var buffer = new char[4096];
int numberRead;
var stringBuilder = new StringBuilder();
while ((numberRead = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
{
stringBuilder.Append(buffer[..numberRead]);
}
return Ok(stringBuilder.ToString());
}
感谢 dbc,解决方案贴在下面。非常简单。
[HttpGet, Route("/api/GetAllAgents/{callingApp}")]
public IActionResult GetAllAgents(
[RegularExpression(@"^[a-zA-Z0-9_-]*$"), Required, MaxLength(50)] string callingApp)
{
string fileName = _config.GetSection("AllAgentsPath").Value;
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
return File(fileStream, "application/json");
}