Streaming large list of data as JSON format using Json.net

后端 未结 1 628
醉梦人生
醉梦人生 2020-12-05 21:06

Using the MVC model, I would like to write a JsonResult that would stream the Json string to the client rather than converting all the data into Json string at once and then

相关标签:
1条回答
  • 2020-12-05 21:56

    Assuming your final output is a JSON array and each "chunk" is one item in that array, you could try something like the following JsonStreamingResult class. It uses a JsonTextWriter to write the JSON to the output stream, and uses a JObject as a means to serialize each item individually before writing it to the writer. You could pass the JsonStreamingResult an IEnumerable implementation which can read items individually from your data source so that you don't have them all in memory at once. I haven't tested this extensively, but it should get you going in the right direction.

    public class JsonStreamingResult : ActionResult
    {
        private IEnumerable itemsToSerialize;
    
        public JsonStreamingResult(IEnumerable itemsToSerialize)
        {
            this.itemsToSerialize = itemsToSerialize;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.ContentType = "application/json";
            response.ContentEncoding = Encoding.UTF8;
    
            JsonSerializer serializer = new JsonSerializer();
    
            using (StreamWriter sw = new StreamWriter(response.OutputStream))
            using (JsonTextWriter writer = new JsonTextWriter(sw))
            {
                writer.WriteStartArray();
                foreach (object item in itemsToSerialize)
                {
                    JObject obj = JObject.FromObject(item, serializer);
                    obj.WriteTo(writer);
                    writer.Flush();
                }
                writer.WriteEndArray();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题