Skip to content

Commit

Permalink
feat: creates newtonsoft json project
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosbcabral committed Apr 13, 2024
1 parent 1969097 commit 3741226
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Mvc;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

using Scaffolding.AspNetCore;
using Scaffolding.AspNetCore;
using Scaffolding.AspNetCore.Filters;
using Scaffolding.Configuration;

using System.Text.Json;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionServiceExtensions
Expand All @@ -29,7 +20,7 @@ public static IServiceCollection AddScaffoldingSettings(this IServiceCollection
var interf = implSetting.GetInterfaces().FirstOrDefault(x => x.Name != "ISettings");
if (interf != null)
services.AddSingleton(interf, setting);

services.AddSingleton(setting.GetType(), setting);
}

Expand All @@ -44,19 +35,6 @@ public static void AddSerilogRequestResponseLogging(this IServiceCollection serv
});
}

public static IServiceCollection ConfigureJson(this IServiceCollection services, NamingStrategy namingStrategy, Action<MvcNewtonsoftJsonOptions> configureOptions = null)
{
ScaffoldingJsonNamingStrategy.DEFAULT_STRATEGY = namingStrategy;
services.AddControllers().AddNewtonsoftJson(x =>
{
x.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = ScaffoldingJsonNamingStrategy.DEFAULT_STRATEGY };
x.SerializerSettings.Converters.Add(new StringEnumConverter(ScaffoldingJsonNamingStrategy.GET_DEFAULT_STRATEGY_TYPE));
x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
configureOptions?.Invoke(x);
});
return services;
}

public static IServiceCollection AddGlobalExceptionHandler(this IServiceCollection services)
{
services.AddExceptionHandler<GlobalExceptionHandler>();
Expand Down
1 change: 0 additions & 1 deletion src/Scaffolding.AspNetCore/Scaffolding.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static IServiceCollection AddScaffoldingDocumentation(this IServiceCollec
Version = applicationSettings.Version ?? "v1",
Title = applicationSettings.Name
});
}).AddSwaggerGenNewtonsoftSupport();
});
}
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Scaffolding.Json.Newtonsoft/Scaffolding.Json.Newtonsoft.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Newtonsoft.Json.Serialization;

using System.Text.Json;

namespace Scaffolding.AspNetCore;
namespace Scaffolding.Json.Newtonsoft;

public class ScaffoldingJsonNamingStrategy
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

using Scaffolding.Json.Newtonsoft;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionServiceExtensions
{
public static IServiceCollection ConfigureJsonNewtonsoft(this IServiceCollection services, NamingStrategy namingStrategy, Action<MvcNewtonsoftJsonOptions> configureOptions = null)

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot convert null literal to non-nullable reference type.

Check warning on line 13 in src/Scaffolding.Json.Newtonsoft/ServiceCollectionServiceExtensions.cs

View workflow job for this annotation

GitHub Actions / deploy

Cannot convert null literal to non-nullable reference type.
{
ScaffoldingJsonNamingStrategy.DEFAULT_STRATEGY = namingStrategy;
services.AddControllers().AddNewtonsoftJson(x =>
{
x.SerializerSettings.ContractResolver = new DefaultContractResolver() { NamingStrategy = ScaffoldingJsonNamingStrategy.DEFAULT_STRATEGY };
x.SerializerSettings.Converters.Add(new StringEnumConverter(ScaffoldingJsonNamingStrategy.GET_DEFAULT_STRATEGY_TYPE));
x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
configureOptions?.Invoke(x);
});
return services;
}
}
}
6 changes: 6 additions & 0 deletions src/Scaffolding.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scaffolding.Documentation",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scaffolding.LegacyAutoMapper", "Scaffolding.LegacyAutoMapper\Scaffolding.LegacyAutoMapper.csproj", "{7175610F-5B80-4A40-82E1-B10A197F971B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scaffolding.Json.Newtonsoft", "Scaffolding.Json.Newtonsoft\Scaffolding.Json.Newtonsoft.csproj", "{8BEE933E-11C9-4FEE-A2B5-05A897641A19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,10 @@ Global
{7175610F-5B80-4A40-82E1-B10A197F971B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7175610F-5B80-4A40-82E1-B10A197F971B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7175610F-5B80-4A40-82E1-B10A197F971B}.Release|Any CPU.Build.0 = Release|Any CPU
{8BEE933E-11C9-4FEE-A2B5-05A897641A19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BEE933E-11C9-4FEE-A2B5-05A897641A19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BEE933E-11C9-4FEE-A2B5-05A897641A19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BEE933E-11C9-4FEE-A2B5-05A897641A19}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 9 additions & 3 deletions src/Scaffolding/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Newtonsoft.Json.Serialization;

using Scaffolding.AspNetCore.Filters;
using Scaffolding.Configuration.Builders;
using Scaffolding.Configuration.Settings.Implementations;
using Scaffolding.HealthCheck.Extensions;
using Scaffolding.Logging.Settings.Implementations;

using System.Text.Json.Serialization;
using System.Text.Json;
using Microsoft.AspNetCore.Http.Json;

var builder = WebApplication.CreateBuilder(args);

var scaffoldingConfiguration = new ScaffoldingConfigurationBuilder(builder.Configuration)
Expand All @@ -24,7 +26,11 @@

builder.Services
.AddScaffoldingSettings(scaffoldingConfiguration)
.ConfigureJson(new SnakeCaseNamingStrategy());
.Configure<JsonOptions>(o =>
{
o.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
o.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
}); ;

builder.Services.AddSerilogRequestResponseLogging();

Expand Down

0 comments on commit 3741226

Please sign in to comment.