Skip to content

Commit c238c6c

Browse files
committed
直接反序列化json响应流
1 parent b5c9701 commit c238c6c

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

WebApiClientCore/ApiResponseContext.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
33
using System.Net.Http;
4+
using System.Text;
45
using System.Threading.Tasks;
56

67
namespace WebApiClientCore
@@ -71,10 +72,27 @@ public ApiResponseContext(ApiRequestContext context)
7172
return objType.DefaultValue();
7273
}
7374

74-
var serializer = this.HttpContext.ServiceProvider.GetJsonSerializer();
75-
var utf8Json = await this.HttpContext.ResponseMessage.Content.ReadAsUtf8ByteArrayAsync().ConfigureAwait(false);
75+
var content = this.HttpContext.ResponseMessage.Content;
76+
if (content == null)
77+
{
78+
return objType.DefaultValue();
79+
}
80+
81+
var encoding = content.GetEncoding();
7682
var options = this.HttpContext.HttpApiOptions.JsonDeserializeOptions;
77-
return serializer.Deserialize(utf8Json, objType, options);
83+
var serializer = this.HttpContext.ServiceProvider.GetJsonSerializer();
84+
85+
if (Encoding.UTF8.Equals(encoding) == true)
86+
{
87+
var utf8Json = await content.ReadAsStreamAsync().ConfigureAwait(false);
88+
return await serializer.DeserializeAsync(utf8Json, objType, options).ConfigureAwait(false);
89+
}
90+
else
91+
{
92+
var byteArray = await content.ReadAsByteArrayAsync().ConfigureAwait(false);
93+
var utf8Json = Encoding.Convert(encoding, Encoding.UTF8, byteArray);
94+
return serializer.Deserialize(utf8Json, objType, options);
95+
}
7896
}
7997

8098
/// <summary>

WebApiClientCore/HttpContents/BufferContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ protected override Task<Stream> CreateContentReadStreamAsync()
9090
/// <param name="stream"></param>
9191
/// <param name="context"></param>
9292
/// <returns></returns>
93-
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
93+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
9494
{
9595
var memory = this.bufferWriter.GetWrittenMemory();
96-
await stream.WriteAsync(memory).ConfigureAwait(false);
96+
return stream.WriteAsync(memory).AsTask();
9797
}
9898

9999
/// <summary>

WebApiClientCore/HttpContents/FormContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ protected override Task<Stream> CreateContentReadStreamAsync()
151151
/// <param name="stream">目标流</param>
152152
/// <param name="context">上下文</param>
153153
/// <returns></returns>
154-
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
154+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
155155
{
156156
var memory = this.bufferWriter.GetWrittenMemory();
157-
await stream.WriteAsync(memory).ConfigureAwait(false);
157+
return stream.WriteAsync(memory).AsTask();
158158
}
159159

160160
/// <summary>

WebApiClientCore/Serialization/IJsonSerializer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Buffers;
3+
using System.IO;
34
using System.Text.Json;
5+
using System.Threading.Tasks;
46

57
namespace WebApiClientCore.Serialization
68
{
@@ -25,5 +27,14 @@ public interface IJsonSerializer
2527
/// <param name="options">选项</param>
2628
/// <returns></returns>
2729
object? Deserialize(Span<byte> utf8Json, Type objType, JsonSerializerOptions? options);
30+
31+
/// <summary>
32+
/// 将utf8编码的Json流反序列化对象
33+
/// </summary>
34+
/// <param name="utf8Json">utf8编码的Json流</param>
35+
/// <param name="objType">对象类型</param>
36+
/// <param name="options">选项</param>
37+
/// <returns></returns>
38+
Task<object> DeserializeAsync(Stream utf8Json, Type objType, JsonSerializerOptions? options);
2839
}
2940
}

WebApiClientCore/Serialization/JsonSerializer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Buffers;
3+
using System.IO;
34
using System.Text.Json;
5+
using System.Threading.Tasks;
46

57
namespace WebApiClientCore.Serialization
68
{
@@ -52,5 +54,17 @@ public void Serialize(IBufferWriter<byte> bufferWriter, object? obj, JsonSeriali
5254
? objType.DefaultValue()
5355
: System.Text.Json.JsonSerializer.Deserialize(utf8Json, objType, options);
5456
}
57+
58+
/// <summary>
59+
/// 将utf8编码的Json流反序列化对象
60+
/// </summary>
61+
/// <param name="utf8Json">utf8编码的Json流</param>
62+
/// <param name="objType">对象类型</param>
63+
/// <param name="options">选项</param>
64+
/// <returns></returns>
65+
public Task<object> DeserializeAsync(Stream utf8Json, Type objType, JsonSerializerOptions? options)
66+
{
67+
return System.Text.Json.JsonSerializer.DeserializeAsync(utf8Json, objType, options).AsTask();
68+
}
5569
}
5670
}

WebApiClientCore/System.Net.Http/HttpContentExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,23 @@ public static Task<byte[]> ReadAsUtf8ByteArrayAsync(this HttpContent httpContent
2828
/// <returns></returns>
2929
public static async Task<byte[]> ReadAsByteArrayAsync(this HttpContent httpContent, Encoding dstEncoding)
3030
{
31+
var encoding = httpContent.GetEncoding();
3132
var byteArray = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false);
32-
var charSet = httpContent.Headers.ContentType?.CharSet;
33-
var encoding = string.IsNullOrEmpty(charSet) ? Encoding.UTF8 : Encoding.GetEncoding(charSet);
3433

3534
return encoding.Equals(dstEncoding)
3635
? byteArray
3736
: Encoding.Convert(encoding, dstEncoding, byteArray);
3837
}
38+
39+
/// <summary>
40+
/// 获取编码信息
41+
/// </summary>
42+
/// <param name="httpContent"></param>
43+
/// <returns></returns>
44+
public static Encoding GetEncoding(this HttpContent httpContent)
45+
{
46+
var charSet = httpContent.Headers.ContentType?.CharSet;
47+
return string.IsNullOrEmpty(charSet) ? Encoding.UTF8 : Encoding.GetEncoding(charSet);
48+
}
3949
}
4050
}

0 commit comments

Comments
 (0)