使用JSON.NET将对象序列化为HttpClient的响应流 [英] Using JSON.NET to serialize object into HttpClient's response stream

查看:105
本文介绍了使用JSON.NET将对象序列化为HttpClient的响应流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在一个项目中,需要通过HttpClient发送某个对象的潜在巨大json,典型大小是10到20 mb的JSON.为了有效地做到这一点,我想使用流,两者都通过Json.Net来序列化一个对象,再加上使用HttpClient发布数据的流.

Hi, I'm working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In order do that efficiently I want to use streams, both with Json.Net to serialize an object plus streams for posting data with HttpClient.

以下是使用Json.net进行序列化的代码段,为了使用流,Json.net希望它将写入的流:

Here is the snippet for serialization with Json.net, in order to work with streams, Json.net expects a stream that it will write into:

public static void Serialize( object value, Stream writeOnlyStream )
{
    StreamWriter writer = new StreamWriter(writeOnlyStream);  <-- Here Json.net expects the stream to be already created
    JsonTextWriter jsonWriter = new JsonTextWriter(writer);
    JsonSerializer ser = new JsonSerializer();
    ser.Serialize(jsonWriter, value );
    jsonWriter.Flush();
}

虽然HttpClient希望从中读取流:

While HttpClient expects a stream that it will read from:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:54359/");

    var response = await client.PostAsync("/api/snapshot", new StreamContent(readOnlyStream)); <-- The same thing here, HttpClient expects the stream already to exist

    ... 
}

因此最终这意味着这两个类都希望Stream由其他人创建,但是Json.Net和HttpClient都没有流.因此,似乎可以通过实现一个流来解决该问题,该流将拦截对只读流发出的读取请求,并根据来自只写流的请求发出写操作.

So eventually this means that both classes expecting the Stream to be created by someone else, but there are no streams both for Json.Net, neither for HttpClient. So the problem seems that can be solved by implementing a stream that would intercept a read requests made to read-only stream, and issue writes upon request from write-only stream.

也许有人已经迷失了这种情况,并且可能已经找到解决该问题的方法.如果是这样,请与我分享,

Maybe someone has stumbled on such situation already, and probably found already implemented solution to this problem. If so, please share it with me,

提前谢谢!

推荐答案

使用PushStreamContent.与其让Web API从流中拉",不如让它更直观地推入"其中.

Use PushStreamContent. Rather than have Web API "pull" from a stream, it lets you more intuitively "push" into one.

object value = ...;

PushStreamContent content = new PushStreamContent((stream, httpContent, transportContext) =>
{
    using (var tw = new StreamWriter(stream))
    {
        JsonSerializer ser = new JsonSerializer();
        ser.Serialize(tw, value);
    }
});

请注意,JSON.NET在序列化过程中不支持异步,因此虽然这可能会提高内存效率,但扩展性却不高.

Note that JSON.NET doesn't support async during serialization so while this may be more memory efficient, it won't be very scalable.

尽管如此,我还是建议避免使用这么大的JSON对象.例如,如果您要通过一个大集合发送邮件,请尝试对其进行分块.许多客户/服务器会拒绝大量的东西而无需特殊处理.

I'd recommend trying to avoid such large JSON objects, though. Try to chunk it up, for instance, if you're sending over a large collection. Many clients/servers will flat out reject something so big without special handling.

这篇关于使用JSON.NET将对象序列化为HttpClient的响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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