Created
September 21, 2022 05:09
-
-
Save ulvesked/44a6fc30d8fd622671343100eef8f1ae to your computer and use it in GitHub Desktop.
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
using Newtonsoft.Json; | |
using System; | |
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace MyDurableTaskProject; | |
public class MyNewtonsoftJsonObjectSerializer : Azure.Core.Serialization.ObjectSerializer | |
{ | |
private static JsonSerializerSettings _settings = new JsonSerializerSettings | |
{ | |
TypeNameHandling = TypeNameHandling.Objects, | |
DateParseHandling = DateParseHandling.None | |
}; | |
private static JsonSerializer _newtonsoftJsonSerializer = JsonSerializer.Create(_settings); | |
#if DEBUG | |
private void WriteStreamToFile(Stream stream, string fileNameSuffix) | |
{ | |
using (var sw = File.OpenWrite(String.Format(@"c:\tmp\MyDurableTaskProject\newtonsoftjson_{0}_{1}_{2}.json", DateTime.UtcNow.Ticks, fileNameSuffix, Guid.NewGuid()))) | |
{ | |
stream.Position = 0; | |
stream.CopyTo(sw); | |
stream.Position = 0; | |
} | |
} | |
#endif | |
public override object Deserialize(Stream stream, Type returnType, CancellationToken cancellationToken) | |
{ | |
#if DEBUG | |
WriteStreamToFile(stream, nameof(Deserialize)); | |
#endif | |
using (var reader = new StreamReader(stream)) | |
{ | |
return _newtonsoftJsonSerializer.Deserialize(reader, returnType); | |
} | |
} | |
public override async ValueTask<object> DeserializeAsync(Stream stream, Type returnType, CancellationToken cancellationToken) | |
{ | |
#if DEBUG | |
WriteStreamToFile(stream, nameof(DeserializeAsync)); | |
#endif | |
using (var reader = new StreamReader(stream)) | |
{ | |
return _newtonsoftJsonSerializer.Deserialize(reader, returnType); | |
} | |
} | |
public override void Serialize(Stream stream, object value, Type inputType, CancellationToken cancellationToken) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(ms, leaveOpen: true)) | |
{ | |
_newtonsoftJsonSerializer.Serialize(writer, value, inputType); | |
} | |
#if DEBUG | |
WriteStreamToFile(ms, nameof(Serialize)); | |
#endif | |
ms.CopyTo(stream); | |
} | |
} | |
public override BinaryData Serialize(object value, Type inputType = null, CancellationToken cancellationToken = default) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(ms, leaveOpen: true)) | |
{ | |
_newtonsoftJsonSerializer.Serialize(writer, value, inputType); | |
} | |
#if DEBUG | |
WriteStreamToFile(ms, nameof(Serialize)); | |
#endif | |
return BinaryData.FromStream(ms); | |
} | |
} | |
public override async ValueTask SerializeAsync(Stream stream, object value, Type inputType, CancellationToken cancellationToken) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(ms, leaveOpen: true)) | |
{ | |
_newtonsoftJsonSerializer.Serialize(writer, value, inputType); | |
} | |
#if DEBUG | |
WriteStreamToFile(ms, nameof(Serialize)); | |
#endif | |
await ms.CopyToAsync(stream); | |
} | |
} | |
public override async ValueTask<BinaryData> SerializeAsync(object value, Type inputType = null, CancellationToken cancellationToken = default) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
using (var writer = new StreamWriter(ms, leaveOpen: true)) | |
{ | |
_newtonsoftJsonSerializer.Serialize(writer, value, inputType); | |
} | |
#if DEBUG | |
WriteStreamToFile(ms, nameof(Serialize)); | |
#endif | |
return BinaryData.FromStream(ms); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment