Skip to content

Commit

Permalink
Fix a json serializer bug. (#6722)
Browse files Browse the repository at this point in the history
* fixUSGovSingleTenant

* Add UT

* AseChannelValidation

* Add UT

* Rollback AuthTenant Property Name

* The Ctor do contains the old ones, Add Ctor to ApiCompatBaseline

* fix a coding mistake

* fix json Serializer bug

* 1

* fix setting
  • Loading branch information
fangyangci authored and Tracy Boehrer committed Jan 24, 2024
1 parent e8e13e4 commit c374ed6
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector.Streaming.Payloads;
Expand All @@ -19,6 +18,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Microsoft.Bot.Connector.Streaming.Session
{
Expand All @@ -42,6 +43,13 @@ internal class StreamingSession

private readonly object _receiveSync = new object();

static StreamingSession()
{
var tmpSetting = SerializationSettings.DefaultSerializationSettings;
tmpSetting.NullValueHandling = NullValueHandling.Ignore;
Serializer = JsonSerializer.Create(tmpSetting);
}

public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogger logger, CancellationToken connectionCancellationToken = default)
{
_receiver = receiver ?? throw new ArgumentNullException(nameof(receiver));
Expand All @@ -52,6 +60,8 @@ public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogge
_connectionCancellationToken = connectionCancellationToken;
}

private static JsonSerializer Serializer { get; }

public async Task<ReceiveResponse> SendRequestAsync(StreamingRequest request, CancellationToken cancellationToken)
{
if (request == null)
Expand Down Expand Up @@ -461,10 +471,16 @@ private static T DeserializeTo<T>(ReadOnlySequence<byte> payload)
mainPayload = payload.Slice(_utf8Bom.Length);
}

var reader = new Utf8JsonReader(mainPayload);
return System.Text.Json.JsonSerializer.Deserialize<T>(
ref reader,
new JsonSerializerOptions() { IgnoreNullValues = true, PropertyNameCaseInsensitive = true });
using (var ms = new MemoryStream(mainPayload.ToArray()))
{
using (var sr = new StreamReader(ms))
{
using (var jsonReader = new JsonTextReader(sr))
{
return Serializer.Deserialize<T>(jsonReader);
}
}
}
}

private static void CreatePlaceholderStreams(Header header, List<IContentStream> placeholders, List<StreamDescription> streamInfo)
Expand Down

0 comments on commit c374ed6

Please sign in to comment.