Skip to content

Commit de90e5e

Browse files
committed
[add] Explicit .NET 6 targeting
[uo] Simplify.Web bump to 4.6 [r] file-scoped namespace, latest lang [del]explicit output folder
1 parent 2e021aa commit de90e5e

File tree

7 files changed

+103
-96
lines changed

7 files changed

+103
-96
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"cSpell.words": [
3-
"deserialized"
3+
"cref",
4+
"deserialized",
5+
"registrator"
46
]
57
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Nuget Download](https://img.shields.io/nuget/dt/Simplify.Web.Json)](https://www.nuget.org/packages/Simplify.Web.Json/)
55
[![Build Package](https://github.com/SimplifyNet/Simplify.Web.Json/actions/workflows/build.yml/badge.svg)](https://github.com/SimplifyNet/Simplify.Web.Json/actions/workflows/build.yml)[![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/nuget/Simplify.Web.Json)](https://libraries.io/nuget/Simplify.Web.Json)
66
[![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/SimplifyNet/Simplify.Web.Json)](https://www.codefactor.io/repository/github/simplifynet/simplify.web.Json)
7-
![Platform](https://img.shields.io/badge/platform-.NET%205.0%20%7C%20.NET%20Standard%202.0%20%7C%20.NET%204.6.2%20%7C%20.NET%20Core%203.1-lightgrey)
7+
![Platform](https://img.shields.io/badge/platform-.NET%206.0%20%7C%20.NET%205.0%20%7C%20.NET%20Standard%202.0%20%7C%20.NET%204.6.2%20%7C%20.NET%20Core%203.1-lightgrey)
88
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen)](http://makeapullrequest.com)
99

1010
[Simplify.Web.Json](https://www.nuget.org/packages/Simplify.Web.Json/) is a package which provides JSON serialization/deserialization for [Simplify.Web](https://github.com/SimplifyNet/Simplify.Web) web-framework controllers.

src/Simplify.Web.Json/CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
# Changelog
22

3+
## [2.2.3] - 2022-05-18
4+
5+
### Added
6+
7+
- Explicit .NET 6 targeting
8+
9+
### Dependencies
10+
11+
- Simplify.Web bump to 4.6
12+
313
## [2.2.2] - 2021-11-24
414

515
### Dependencies
616

717
- Simplify.Web bump to 4.5
818

9-
1019
## [2.2.1] - 2021-04-26
1120

1221
### Added

src/Simplify.Web.Json/Model/Binding/JsonModelBinder.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,40 @@
44
using Simplify.Web.Model.Binding;
55
using Simplify.Web.Model.Validation;
66

7-
namespace Simplify.Web.Json.Model.Binding
7+
namespace Simplify.Web.Json.Model.Binding;
8+
9+
/// <summary>
10+
/// Provides HTTP request JSON data to object binding
11+
/// </summary>
12+
public class JsonModelBinder : IModelBinder
813
{
14+
private readonly JsonSerializerSettings? _settings;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="JsonModelBinder"/> class.
18+
/// </summary>
19+
/// <param name="settings">The settings.</param>
20+
public JsonModelBinder(JsonSerializerSettings? settings = null) => _settings = settings;
21+
922
/// <summary>
10-
/// Provides HTTP request JSON data to object binding
23+
/// Binds the model asynchronously.
1124
/// </summary>
12-
public class JsonModelBinder : IModelBinder
25+
/// <typeparam name="T">Model type</typeparam>
26+
/// <param name="args">The <see cref="T:Simplify.Web.Model.Binding.ModelBinderEventArgs`1" /> instance containing the event data.</param>
27+
/// <returns></returns>
28+
/// <exception cref="ModelValidationException">JSON request body is null or empty</exception>
29+
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
1330
{
14-
private readonly JsonSerializerSettings? _settings;
15-
16-
/// <summary>
17-
/// Initializes a new instance of the <see cref="JsonModelBinder"/> class.
18-
/// </summary>
19-
/// <param name="settings">The settings.</param>
20-
public JsonModelBinder(JsonSerializerSettings? settings = null) => _settings = settings;
21-
22-
/// <summary>
23-
/// Binds the model asynchronously.
24-
/// </summary>
25-
/// <typeparam name="T">Model type</typeparam>
26-
/// <param name="args">The <see cref="T:Simplify.Web.Model.Binding.ModelBinderEventArgs`1" /> instance containing the event data.</param>
27-
/// <returns></returns>
28-
/// <exception cref="ModelValidationException">JSON request body is null or empty</exception>
29-
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
30-
{
31-
if (args.Context.Request.ContentType == null || !args.Context.Request.ContentType.Contains("application/json"))
32-
return;
33-
34-
await args.Context.ReadRequestBodyAsync();
35-
36-
if (string.IsNullOrEmpty(args.Context.RequestBody))
37-
throw new ModelValidationException("JSON request body is null or empty");
38-
39-
var deserializedModel = JsonConvert.DeserializeObject<T>(args.Context.RequestBody, _settings) ?? throw new InvalidOperationException("Deserialized model is null");
40-
41-
args.SetModel(deserializedModel);
42-
}
31+
if (args.Context.Request.ContentType == null || !args.Context.Request.ContentType.Contains("application/json"))
32+
return;
33+
34+
await args.Context.ReadRequestBodyAsync();
35+
36+
if (string.IsNullOrEmpty(args.Context.RequestBody))
37+
throw new ModelValidationException("JSON request body is null or empty");
38+
39+
var deserializedModel = JsonConvert.DeserializeObject<T>(args.Context.RequestBody, _settings) ?? throw new InvalidOperationException("Deserialized model is null");
40+
41+
args.SetModel(deserializedModel);
4342
}
44-
}
43+
}
Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
using System.Threading.Tasks;
22
using Newtonsoft.Json;
33

4-
namespace Simplify.Web.Json.Responses
4+
namespace Simplify.Web.Json.Responses;
5+
6+
/// <summary>
7+
/// Provides controller JSON response (send only JSON string to response)
8+
/// </summary>
9+
public class Json : ControllerResponse
510
{
11+
private readonly object _objectToConvert;
12+
613
/// <summary>
7-
/// Provides controller JSON response (send only JSON string to response)
14+
/// Gets the HTTP response status code.
815
/// </summary>
9-
public class Json : ControllerResponse
10-
{
11-
private readonly object _objectToConvert;
16+
/// <value>
17+
/// The HTTP response status code.
18+
/// </value>
19+
private readonly int _statusCode;
1220

13-
/// <summary>
14-
/// Gets the HTTP response status code.
15-
/// </summary>
16-
/// <value>
17-
/// The HTTP response status code.
18-
/// </value>
19-
private readonly int _statusCode;
20-
21-
/// <summary>
22-
/// Initializes a new instance of the <see cref="Json"/> class.
23-
/// </summary>
24-
/// <param name="objectToConvert">The object to convert to JSON.</param>
25-
/// <param name="statusCode">The HTTP response status code.</param>
26-
public Json(object objectToConvert, int statusCode = 200)
27-
{
28-
_objectToConvert = objectToConvert;
29-
_statusCode = statusCode;
30-
}
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="Json"/> class.
23+
/// </summary>
24+
/// <param name="objectToConvert">The object to convert to JSON.</param>
25+
/// <param name="statusCode">The HTTP response status code.</param>
26+
public Json(object objectToConvert, int statusCode = 200)
27+
{
28+
_objectToConvert = objectToConvert;
29+
_statusCode = statusCode;
30+
}
3131

32-
/// <summary>
33-
/// Processes this response
34-
/// </summary>
35-
/// <returns></returns>
36-
public override async Task<ControllerResponseResult> Process()
37-
{
38-
Context.Response.ContentType = "application/json";
39-
Context.Response.StatusCode = _statusCode;
32+
/// <summary>
33+
/// Processes this response
34+
/// </summary>
35+
/// <returns></returns>
36+
public override async Task<ControllerResponseResult> Process()
37+
{
38+
Context.Response.ContentType = "application/json";
39+
Context.Response.StatusCode = _statusCode;
4040

41-
await ResponseWriter.WriteAsync(JsonConvert.SerializeObject(_objectToConvert), Context.Response);
41+
await ResponseWriter.WriteAsync(JsonConvert.SerializeObject(_objectToConvert), Context.Response);
4242

43-
return ControllerResponseResult.RawOutput;
44-
}
43+
return ControllerResponseResult.RawOutput;
4544
}
4645
}

src/Simplify.Web.Json/Simplify.Web.Json.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.0;net462</TargetFrameworks>
4-
<LangVersion>9.0</LangVersion>
3+
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;netstandard2.0;net462</TargetFrameworks>
4+
<LangVersion>latest</LangVersion>
55
<Nullable>enable</Nullable>
6-
<OutputPath>bin\Any CPU\$(Configuration)\</OutputPath>
76
<EmbedUntrackedSources>true</EmbedUntrackedSources>
87
<IncludeSymbols>true</IncludeSymbols>
98
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
109
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1110

12-
<Version>2.2.2</Version>
11+
<Version>2.2.3</Version>
1312

1413
<Authors>Alexander Krylkov</Authors>
1514
<Product>Simplify</Product>
@@ -23,8 +22,8 @@
2322
<PackageReleaseNotes>See https://github.com/SimplifyNet/Simplify.Web.Json/tree/master/src/Simplify.Web.Json/CHANGELOG.md for details</PackageReleaseNotes>
2423
</PropertyGroup>
2524
<ItemGroup>
26-
<PackageReference Include="Simplify.Web" Version="4.6.*" />
27-
<PackageReference Include="Newtonsoft.Json" Version="13.0.*" />
25+
<PackageReference Include="Simplify.Web" Version="4.6.0" />
26+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2827
</ItemGroup>
2928
<ItemGroup>
3029
<None Include="..\..\images\icon.png" Pack="true" Visible="false" PackagePath="" />

src/Simplify.Web.Json/SimplifyDIRegistratorExtensions.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22
using Simplify.DI;
33
using Simplify.Web.Json.Model.Binding;
44

5-
namespace Simplify.Web.Json
5+
namespace Simplify.Web.Json;
6+
7+
/// <summary>
8+
/// Provides Simplify.Web.Json default registration
9+
/// </summary>
10+
public static class SimplifyDIRegistratorExtensions
611
{
712
/// <summary>
8-
/// Provides Simplify.Web.Json default registration
13+
/// Registers Simplify.Web.Json JsonModelBinder.
914
/// </summary>
10-
public static class SimplifyDIRegistratorExtensions
11-
{
12-
/// <summary>
13-
/// Registers Simplify.Web.Json JsonModelBinder.
14-
/// </summary>
15-
/// <param name="registrator">The registrator.</param>
16-
/// <param name="settings">The settings.</param>
17-
/// <returns></returns>
18-
public static IDIRegistrator RegisterJsonModelBinder(this IDIRegistrator registrator, JsonSerializerSettings? settings = null) =>
19-
registrator.Register(r => new JsonModelBinder(settings), LifetimeType.Singleton);
15+
/// <param name="registrator">The registrator.</param>
16+
/// <param name="settings">The settings.</param>
17+
/// <returns></returns>
18+
public static IDIRegistrator RegisterJsonModelBinder(this IDIRegistrator registrator, JsonSerializerSettings? settings = null) =>
19+
registrator.Register(r => new JsonModelBinder(settings), LifetimeType.Singleton);
2020

21-
/// <summary>
22-
/// Registers Simplify.Web.Json JsonModelBinder which will search for `JsonSerializerSettings` registered in container (JsonSerializerSettings should be manually registered in container).
23-
/// </summary>
24-
/// <param name="registrator">The registrator.</param>
25-
/// <returns></returns>
26-
public static IDIRegistrator RegisterJsonModelBinderWithSettingsFromContainer(this IDIRegistrator registrator) =>
27-
registrator.Register(r => new JsonModelBinder(r.Resolve<JsonSerializerSettings>()), LifetimeType.Singleton);
28-
}
21+
/// <summary>
22+
/// Registers Simplify.Web.Json JsonModelBinder which will search for `JsonSerializerSettings` registered in container (JsonSerializerSettings should be manually registered in container).
23+
/// </summary>
24+
/// <param name="registrator">The registrator.</param>
25+
/// <returns></returns>
26+
public static IDIRegistrator RegisterJsonModelBinderWithSettingsFromContainer(this IDIRegistrator registrator) =>
27+
registrator.Register(r => new JsonModelBinder(r.Resolve<JsonSerializerSettings>()), LifetimeType.Singleton);
2928
}

0 commit comments

Comments
 (0)