diff --git a/src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs b/src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs index de942caca7..4e42422b3b 100644 --- a/src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs +++ b/src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs @@ -500,8 +500,8 @@ namespace Microsoft.AspNetCore.SignalR.Protocol } else if (message.HasResult) { - using var token = GetParsedObject(message.Result, message.Result?.GetType()); - token.RootElement.WriteProperty(ResultPropertyNameBytes.EncodedUtf8Bytes, writer); + writer.WritePropertyName(ResultPropertyNameBytes); + JsonSerializer.WriteValue(writer, message.Result, message.Result?.GetType(), _payloadSerializerOptions); } } @@ -514,8 +514,8 @@ namespace Microsoft.AspNetCore.SignalR.Protocol { WriteInvocationId(message, writer); - using var token = GetParsedObject(message.Item, message.Item?.GetType()); - token.RootElement.WriteProperty(ItemPropertyNameBytes.EncodedUtf8Bytes, writer); + writer.WritePropertyName(ItemPropertyNameBytes); + JsonSerializer.WriteValue(writer, message.Item, message.Item?.GetType(), _payloadSerializerOptions); } private void WriteInvocationMessage(InvocationMessage message, Utf8JsonWriter writer) @@ -562,20 +562,12 @@ namespace Microsoft.AspNetCore.SignalR.Protocol } else { - using var token = GetParsedObject(argument, type); - token.RootElement.WriteValue(writer); + JsonSerializer.WriteValue(writer, argument, type, _payloadSerializerOptions); } } writer.WriteEndArray(); } - private JsonDocument GetParsedObject(object obj, Type type) - { - var bytes = JsonSerializer.ToUtf8Bytes(obj, type, _payloadSerializerOptions); - var token = JsonDocument.Parse(bytes); - return token; - } - private void WriteStreamIds(string[] streamIds, Utf8JsonWriter writer) { if (streamIds == null)