Skip to content

Commit

Permalink
(#110) Removing Newtonsoft.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Nov 23, 2021
1 parent af9f463 commit de6fe6a
Show file tree
Hide file tree
Showing 96 changed files with 353 additions and 344 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ClassifiedAds.Application.Decorators.AuditLog
{
Expand All @@ -18,7 +18,7 @@ public AuditLogCommandDecorator(ICommandHandler<TCommand> handler)

public async Task HandleAsync(TCommand command, CancellationToken cancellationToken = default)
{
var commandJson = JsonConvert.SerializeObject(command);
var commandJson = JsonSerializer.Serialize(command);
Console.WriteLine($"Command of type {command.GetType().Name}: {commandJson}");
await _handler.HandleAsync(command, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ClassifiedAds.Application.Decorators.AuditLog
{
Expand All @@ -18,7 +18,7 @@ public AuditLogQueryDecorator(IQueryHandler<TQuery, TResult> handler)

public Task<TResult> HandleAsync(TQuery query, CancellationToken cancellationToken = default)
{
string queryJson = JsonConvert.SerializeObject(query);
string queryJson = JsonSerializer.Serialize(query);
Console.WriteLine($"Query of type {query.GetType().Name}: {queryJson}");
return _handler.HandleAsync(query, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>ClassifiedAds.CrossCuttingConcerns</RootNamespace>
<CodeAnalysisRuleSet>ClassifiedAds.CrossCuttingConcerns.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>ClassifiedAds.CrossCuttingConcerns</RootNamespace>
<CodeAnalysisRuleSet>ClassifiedAds.CrossCuttingConcerns.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="6.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using System.Reflection;
using System.Reflection;
using System.Text.Json;

namespace ClassifiedAds.CrossCuttingConcerns.ExtensionMethods
{
Expand Down Expand Up @@ -28,13 +28,7 @@ public static T DeepCloneJson<T>(this T source)
return default(T);
}

// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };

return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(source));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Newtonsoft.Json;
using System.IO;
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;

namespace ClassifiedAds.CrossCuttingConcerns.ExtensionMethods
Expand All @@ -10,19 +9,22 @@ public static class HttpContentExtensions
{
public static async Task<T> ReadAs<T>(this HttpContent httpContent)
{
using (var streamReader = new StreamReader(await httpContent.ReadAsStreamAsync()))
var json = await httpContent.ReadAsStringAsync();

if (string.IsNullOrWhiteSpace(json))
{
using (var jsonTextReader = new JsonTextReader(streamReader))
{
var jsonSerializer = new JsonSerializer();
return jsonSerializer.Deserialize<T>(jsonTextReader);
}
return default;
}

return JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
});
}

public static StringContent AsStringContent(this object obj, string contentType)
{
var content = new StringContent(JsonConvert.SerializeObject(obj));
var content = new StringContent(JsonSerializer.Serialize(obj));
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return content;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Newtonsoft.Json;
using System.Text.Json;

namespace ClassifiedAds.CrossCuttingConcerns.ExtensionMethods
{
public static class ObjectExtensions
{
public static string AsJsonString(this object obj)
{
var content = JsonConvert.SerializeObject(obj, Formatting.Indented);
var content = JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
return content;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<CodeAnalysisRuleSet>ClassifiedAds.Domain.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<CodeAnalysisRuleSet>ClassifiedAds.Domain.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.CrossCuttingConcerns\ClassifiedAds.CrossCuttingConcerns.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.CrossCuttingConcerns\ClassifiedAds.CrossCuttingConcerns.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using System.Text;
using System.Text;
using System.Text.Json;

namespace ClassifiedAds.Domain.Infrastructure.MessageBrokers
{
Expand All @@ -11,7 +11,7 @@ public class Message<T>

public string SerializeObject()
{
return JsonConvert.SerializeObject(this);
return JsonSerializer.Serialize(this);
}

public byte[] GetBytes()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>ClassifiedAds.Infrastructure</RootNamespace>
<CodeAnalysisRuleSet>ClassifiedAds.Infrastructure.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>ClassifiedAds.Infrastructure</RootNamespace>
<CodeAnalysisRuleSet>ClassifiedAds.Infrastructure.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
Expand Down Expand Up @@ -55,7 +55,6 @@
<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
<PackageReference Include="MiniProfiler.EntityFrameworkCore" Version="4.2.22" />
<PackageReference Include="MiniProfiler.Providers.SqlServer" Version="4.2.22" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.2.0-beta2.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.2.0-beta2.1" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.2.0-beta2.1" />
Expand All @@ -80,19 +79,19 @@
<PackageReference Include="Twilio" Version="5.68.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassifiedAds.Domain\ClassifiedAds.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<ContentWithTargetPath Include="..\libs\libwkhtmltox\libwkhtmltox-0.12.4-x64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libwkhtmltox.dll</TargetPath>
</ContentWithTargetPath>
<ContentWithTargetPath Include="..\libs\libwkhtmltox\libwkhtmltox-0.12.4-x64.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libwkhtmltox.so</TargetPath>
</ContentWithTargetPath>
</ItemGroup>

<ItemGroup>
<ContentWithTargetPath Include="..\libs\libwkhtmltox\libwkhtmltox-0.12.4-x64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libwkhtmltox.dll</TargetPath>
</ContentWithTargetPath>
<ContentWithTargetPath Include="..\libs\libwkhtmltox\libwkhtmltox-0.12.4-x64.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libwkhtmltox.so</TargetPath>
</ContentWithTargetPath>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ClassifiedAds.Infrastructure.Interceptors
{
Expand All @@ -27,7 +28,12 @@ public void Intercept(IInvocation invocation)
var methodName = method.Name;

// TODO: Ignore serialize large argument object.
var arguments = JsonConvert.SerializeObject(invocation.Arguments, Formatting.None, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
var arguments = JsonSerializer.Serialize(invocation.Arguments, new JsonSerializerOptions()
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
});
_logger.LogError($"An unhandled exception has occurred while executing the method: {className}.{methodName} with ({arguments}). {Environment.NewLine}{ex.ToString()}");
throw;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace ClassifiedAds.Infrastructure.Interceptors
{
Expand All @@ -19,7 +20,13 @@ public void Intercept(IInvocation invocation)
var method = invocation.Method;
var className = method.DeclaringType.Name;
var methodName = method.Name;
var arguments = JsonConvert.SerializeObject(invocation.Arguments, Formatting.None, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

var arguments = JsonSerializer.Serialize(invocation.Arguments, new JsonSerializerOptions()
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
});

_logger.LogDebug($"Start calling method: {className}.{methodName} with ({arguments}).");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Azure;
using Azure.Messaging.EventGrid;
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -27,7 +27,7 @@ public async Task SendAsync(T message, MetaData metaData, CancellationToken canc

EventGridPublisherClient client = new EventGridPublisherClient(new Uri(_domainEndpoint), new AzureKeyCredential(_domainKey));

var data = new BinaryData(JsonConvert.SerializeObject(new Message<T>
var data = new BinaryData(JsonSerializer.Serialize(new Message<T>
{
Data = message,
MetaData = metaData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Azure.Messaging.EventHubs.Processor;
using Azure.Storage.Blobs;
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.MessageBrokers.AzureEventHub
Expand Down Expand Up @@ -43,7 +43,7 @@ Task ProcessEventHandler(ProcessEventArgs eventArgs)
try
{
var messageAsString = Encoding.UTF8.GetString(eventArgs.Data.EventBody);
var message = JsonConvert.DeserializeObject<Message<T>>(messageAsString);
var message = JsonSerializer.Deserialize<Message<T>>(messageAsString);
action(message.Data, message.MetaData);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -25,7 +25,7 @@ public async Task SendAsync(T message, MetaData metaData, CancellationToken canc

var events = new List<EventData>
{
new EventData(JsonConvert.SerializeObject(new Message<T>
new EventData(JsonSerializer.Serialize(new Message<T>
{
Data = message,
MetaData = metaData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Azure.Storage.Queues;
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
using Newtonsoft.Json;
using System;
using System.Text.Json;
using System.Threading.Tasks;

namespace ClassifiedAds.Infrastructure.MessageBrokers.AzureQueue
Expand All @@ -28,7 +28,7 @@ private Task ReceiveAsync(Action<T, MetaData> action)
{
return ReceiveStringAsync(retrievedMessage =>
{
var message = JsonConvert.DeserializeObject<Message<T>>(retrievedMessage);
var message = JsonSerializer.Deserialize<Message<T>>(retrievedMessage);
action(message.Data, message.MetaData);
});
}
Expand Down
Loading

0 comments on commit de6fe6a

Please sign in to comment.