File tree Expand file tree Collapse file tree 3 files changed +33
-11
lines changed Expand file tree Collapse file tree 3 files changed +33
-11
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,17 @@ namespace Simplify.Web.Json.Model.Binding
1010 /// </summary>
1111 public class JsonModelBinder : IModelBinder
1212 {
13+ private readonly JsonSerializerSettings ? _settings ;
14+
15+ /// <summary>
16+ /// Initializes a new instance of the <see cref="JsonModelBinder"/> class.
17+ /// </summary>
18+ /// <param name="settings">The settings.</param>
19+ public JsonModelBinder ( JsonSerializerSettings ? settings = null )
20+ {
21+ _settings = settings ;
22+ }
23+
1324 /// <summary>
1425 /// Binds the model asynchronously.
1526 /// </summary>
@@ -27,7 +38,7 @@ public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
2738 if ( string . IsNullOrEmpty ( args . Context . RequestBody ) )
2839 throw new ModelValidationException ( "JSON request body is null or empty" ) ;
2940
30- args . SetModel ( JsonConvert . DeserializeObject < T > ( args . Context . RequestBody ) ) ;
41+ args . SetModel ( JsonConvert . DeserializeObject < T > ( args . Context . RequestBody , _settings ) ) ;
3142 }
3243 }
3344}
Original file line number Diff line number Diff line change 77 <Product >Simplify</Product >
88 <Description >Simplify.Web JSON controller response and model binder</Description >
99 <Copyright >Licensed under LGPL</Copyright >
10- <Version >2.0. 1</Version >
10+ <Version >2.1</Version >
1111 <PackageProjectUrl >https://github.com/SimplifyNet/Simplify.Web.Json</PackageProjectUrl >
1212 <PackageIconUrl >https://raw.githubusercontent.com/SimplifyNet/Images/master/LogoWeb32x32.png</PackageIconUrl >
1313 <RepositoryUrl >https://github.com/SimplifyNet/Simplify.Web.Json</RepositoryUrl >
1414 <RepositoryType >GIT</RepositoryType >
1515 <PackageTags >Simplify.Web JSON</PackageTags >
1616 <PackageReleaseNotes >
17- Updates
18- * Upgrade to Simplify.Web 4.0.2
19- * .NET Core App 3.1 and .NET Core App 3.0 target frameworks added
20- Bug Fixes
21- * JsonModelBinder checking ContentType for null added
22- * ReadRequestBodyAsync moved before accessing RequestBody (caused synchronous model access)
17+ New
18+ * Ability to specify JsonSerializerSettings for JSON deserialization
2319 </PackageReleaseNotes >
2420 <OutputPath >bin\Any CPU\$(Configuration)\</OutputPath >
2521 <DocumentationFile >bin\Any CPU\$(Configuration)\$(TargetFramework)\Simplify.Web.Json.xml</DocumentationFile >
Original file line number Diff line number Diff line change 1- using Simplify . DI ;
1+ using Newtonsoft . Json ;
2+ using Simplify . DI ;
23using Simplify . Web . Json . Model . Binding ;
34
45namespace Simplify . Web . Json
@@ -12,9 +13,23 @@ public static class SimplifyDIRegistratorExtensions
1213 /// Registers Simplify.Web.Json JsonModelBinder.
1314 /// </summary>
1415 /// <param name="registrator">The registrator.</param>
15- public static IDIRegistrator RegisterJsonModelBinder ( this IDIRegistrator registrator )
16+ /// <param name="settings">The settings.</param>
17+ /// <returns></returns>
18+ public static IDIRegistrator RegisterJsonModelBinder ( this IDIRegistrator registrator , JsonSerializerSettings ? settings = null )
1619 {
17- registrator . Register < JsonModelBinder > ( LifetimeType . Singleton ) ;
20+ registrator . Register ( r => new JsonModelBinder ( settings ) , LifetimeType . Singleton ) ;
21+
22+ return registrator ;
23+ }
24+
25+ /// <summary>
26+ /// Registers Simplify.Web.Json JsonModelBinder which will search for `JsonSerializerSettings` registered in container (JsonSerializerSettings should be manually registered in container).
27+ /// </summary>
28+ /// <param name="registrator">The registrator.</param>
29+ /// <returns></returns>
30+ public static IDIRegistrator RegisterJsonModelBinderWithSettingsFromContainer ( this IDIRegistrator registrator )
31+ {
32+ registrator . Register ( r => new JsonModelBinder ( r . Resolve < JsonSerializerSettings > ( ) ) , LifetimeType . Singleton ) ;
1833
1934 return registrator ;
2035 }
You can’t perform that action at this time.
0 commit comments