数据流的大名单为使用Json.net JSON格式 [英] Streaming large list of data as JSON format using Json.net

查看:84
本文介绍了数据流的大名单为使用Json.net JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC模式,我想编写一个JsonResult,将流JSON字符串到客户端,而不是所有的数据转换成JSON字符串一次,然后流回给客户端。
我有需要发送非常大(超过30万条记录)为JSON转让行为,我认为基本的JsonResult实现是不可扩展的。

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 streaming it back to the client. I have actions that require to send very large (over 300,000 records) as Json transfers and I think the basic JsonResult implementation is not scalable.

我使用Json.net,我想知道是否有流JSON字符串的大块,因为它正在转变的一种方式。

I am using Json.net, I am wondering if there is a way to stream the chunks of the Json string as it is being transformed.

//Current implementation:
response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(Data, formatting));
response.End();

//I know I can use the JsonSerializer instead
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Serialize(textWriter, Data);

但是我不知道我怎样才能写入的TextWriter的块,并写入响应并调用reponse.Flush(),直到所有300,00条记录转换为JSON。

However I am not sure how I can get the chunks written into textWriter and write into response and call reponse.Flush() until all 300,000 records are converted to Json.

这是可能的呢?

推荐答案

假设你的最终输出是一个JSON阵列,每个块的是,阵列中的一个项目,您可以尝试像下面的 JsonStreamingResult 类。它采用了 JsonTextWriter 来的JSON写入到输出流,并使用 JObject 作为序列的每个项目的手段独立前写的作家。你可以通过 JsonStreamingResult 的IEnumerable 的实施可以从数据源中单独读取项目,使您不必他们都在内存中一次。我没有测试过这种广泛的,但它应该让你在正确的方向前进。

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();
        }
    }
}

这篇关于数据流的大名单为使用Json.net JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆