Using JSON.NET to serialize object into HttpClient's response stream

前端 未结 2 695
温柔的废话
温柔的废话 2020-12-20 00:48

Abstract

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 orde

2条回答
  •  佛祖请我去吃肉
    2020-12-20 01:07

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

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

    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.

提交回复
热议问题