forked from dotnetcore/WebApiClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
112 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 12 additions & 7 deletions
19
WebApiClientCore.Extensions.JsonRpc/Internals/JsonRpcContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
using WebApiClientCore.HttpContents; | ||
using System.Text.Json; | ||
using WebApiClientCore.HttpContents; | ||
using WebApiClientCore.Serialization; | ||
|
||
namespace WebApiClientCore.Extensions.JsonRpc | ||
{ | ||
/// <summary> | ||
/// 表示JsonRpc请求内容 | ||
/// </summary> | ||
class JsonRpcContent : BufferContent | ||
sealed class JsonRpcContent : BufferContent | ||
{ | ||
/// <summary> | ||
/// 获取对应的ContentType | ||
/// </summary> | ||
public static string MediaType => "application/json-rpc"; | ||
public static string MediaType => "application/json-rpc"; | ||
|
||
/// <summary> | ||
/// JsonRpc请求内容 | ||
/// </summary> | ||
/// <param name="mediaType">媒体类型</param> | ||
public JsonRpcContent(string? mediaType) | ||
/// uft8的json内容 | ||
/// </summary> | ||
/// <param name="mediaType"></param> | ||
/// <param name="value">对象值</param> | ||
/// <param name="jsonSerializerOptions">json序列化选项</param> | ||
public JsonRpcContent(object? value, JsonSerializerOptions? jsonSerializerOptions, string? mediaType) | ||
: base(mediaType ?? MediaType) | ||
{ | ||
JsonBufferSerializer.Serialize(this, value, jsonSerializerOptions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
namespace WebApiClientCore | ||
{ | ||
/// <summary> | ||
/// 提供Encoding扩展 | ||
/// </summary> | ||
static class EncodingExtensions | ||
{ | ||
/// <summary> | ||
/// 转换编码 | ||
/// </summary> | ||
/// <param name="srcEncoding"></param> | ||
/// <param name="dstEncoding">目标编码</param> | ||
/// <param name="buffer">源内容</param> | ||
/// <param name="writer">目标写入器</param> | ||
public static void Convert(this Encoding srcEncoding, Encoding dstEncoding, ReadOnlySpan<byte> buffer, IBufferWriter<byte> writer) | ||
{ | ||
var decoder = srcEncoding.GetDecoder(); | ||
var charCount = decoder.GetCharCount(buffer, false); | ||
var charArray = ArrayPool<char>.Shared.Rent(charCount); | ||
|
||
try | ||
{ | ||
decoder.Convert(buffer, charArray, true, out _, out var charsUsed, out _); | ||
Debug.Assert(charCount == charsUsed); | ||
var chars = charArray.AsSpan().Slice(0, charsUsed); | ||
|
||
var encoder = dstEncoding.GetEncoder(); | ||
var byteCount = encoder.GetByteCount(chars, false); | ||
var bytes = writer.GetSpan(byteCount); | ||
|
||
encoder.Convert(chars, bytes, true, out _, out var byteUsed, out _); | ||
Debug.Assert(byteCount == byteUsed); | ||
writer.Advance(byteUsed); | ||
} | ||
finally | ||
{ | ||
ArrayPool<char>.Shared.Return(charArray); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters