Skip to content

Commit 9a431c7

Browse files
committed
Update newtonsoft.json
1 parent 47500ff commit 9a431c7

10 files changed

+137
-141
lines changed

sample/ODataNewtonsoftJsonSample/Startup.cs

+11-13
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ public Startup(IConfiguration configuration)
2525
// This method gets called by the runtime. Use this method to add services to the container.
2626
public void ConfigureServices(IServiceCollection services)
2727
{
28-
services.AddControllers();
29-
30-
services.AddOData(opt => opt.Select().Filter().AddModel("odata", GetEdmModel())).AddNewtonsoftJson(
31-
options =>
32-
{
33-
options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
34-
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
35-
//options.SerializerSettings.ContractResolver = WebApiJsonResolver.Instance;
36-
});
37-
38-
// You can also add the converter one by one using followings:
39-
//services.AddControllers().AddNewtonsoftJson(
40-
// opt => opt.SerializerSettings.Converters.Add(new JDynamicTypeWrapperConverter()));
28+
services.AddControllers()
29+
.AddOData(opt => opt.Select().Filter().AddModel("odata", GetEdmModel()))
30+
.AddODataNewtonsoftJson()
31+
//.AddNewtonsoftJson(
32+
//options =>
33+
//{
34+
// options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
35+
// options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
36+
// //options.SerializerSettings.ContractResolver = WebApiJsonResolver.Instance;
37+
//})
38+
;
4139
}
4240

4341
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/Microsoft.AspNetCore.OData.NewtonsoftJson/JDynamicTypeWrapperConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
1010
/// <summary>
1111
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="DynamicTypeWrapper"/> instances to JSON.
1212
/// </summary>
13-
public class JDynamicTypeWrapperConverter : JsonConverter
13+
internal class JDynamicTypeWrapperConverter : JsonConverter
1414
{
1515
/// <summary>
1616
/// Determines whether this instance can convert the specified <see cref="DynamicTypeWrapper"/> type.

src/Microsoft.AspNetCore.OData.NewtonsoftJson/JPageResultValueConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
1010
/// <summary>
1111
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="PageResult"/> instances to JSON.
1212
/// </summary>
13-
public class JPageResultValueConverter : JsonConverter
13+
internal class JPageResultValueConverter : JsonConverter
1414
{
1515
/// <summary>
1616
/// Determines whether this instance can convert the specified <see cref="PageResult"/> type.

src/Microsoft.AspNetCore.OData.NewtonsoftJson/JSelectExpandWrapperConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
1212
/// <summary>
1313
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="ISelectExpandWrapper"/> instances to JSON.
1414
/// </summary>
15-
public class JSelectExpandWrapperConverter : JsonConverter
15+
internal class JSelectExpandWrapperConverter : JsonConverter
1616
{
1717
private Func<IEdmModel, IEdmStructuredType, IPropertyMapper> _mapperProvider;
1818

src/Microsoft.AspNetCore.OData.NewtonsoftJson/JSingleResultValueConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
1111
/// <summary>
1212
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="SingleResult"/> instances to JSON.
1313
/// </summary>
14-
public class JSingleResultValueConverter : JsonConverter
14+
internal class JSingleResultValueConverter : JsonConverter
1515
{
1616
/// <summary>
1717
/// Determines whether this instance can convert the specified <see cref="SingleResult"/> type.

src/Microsoft.AspNetCore.OData.NewtonsoftJson/JsonPropertyNameMapper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
1515
/// <summary>
1616
/// Edm Property name mapper.
1717
/// </summary>
18-
public class JsonPropertyNameMapper : IPropertyMapper
18+
internal class JsonPropertyNameMapper : IPropertyMapper
1919
{
2020
private IEdmModel _model;
2121
private IEdmStructuredType _type;

src/Microsoft.AspNetCore.OData.NewtonsoftJson/Microsoft.AspNetCore.OData.NewtonsoftJson.xml

+20-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.OData.Query.Container;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.OData.Edm;
9+
10+
namespace Microsoft.AspNetCore.OData.NewtonsoftJson
11+
{
12+
/// <summary>
13+
/// Extension methods for adding OData Json converter to Newtonsoft.Json to <see cref="IMvcBuilder"/> and <see cref="IMvcCoreBuilder"/>.
14+
/// </summary>
15+
public static class ODataNewtonsoftJsonMvcBuilderExtensions
16+
{
17+
#region IMvcBuilder
18+
/// <summary>
19+
/// Configures Newtonsoft.Json using OData Json converter.
20+
/// </summary>
21+
/// <param name="builder">The Mvc builder.</param>
22+
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
23+
public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder)
24+
{
25+
return builder.AddODataNewtonsoftJson(null);
26+
}
27+
28+
/// <summary>
29+
/// Configures Newtonsoft.Json using OData Json converter.
30+
/// </summary>
31+
/// <param name="builder">The Mvc builder.</param>
32+
/// <param name="mapperProvider">The mapper provider.</param>
33+
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
34+
public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder,
35+
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
36+
{
37+
if (builder is null)
38+
{
39+
throw new ArgumentNullException(nameof(builder));
40+
}
41+
42+
return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider));
43+
}
44+
#endregion
45+
46+
#region IMvcCoreBuilder
47+
/// <summary>
48+
/// Configures Newtonsoft.Json using OData Json converter.
49+
/// </summary>
50+
/// <param name="builder">The Mvc core builder.</param>
51+
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
52+
public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder)
53+
{
54+
return builder.AddNewtonsoftJson(null);
55+
}
56+
57+
/// <summary>
58+
/// Configures Newtonsoft.Json using OData Json converter.
59+
/// </summary>
60+
/// <param name="builder">The Mvc core builder.</param>
61+
/// <param name="mapperProvider">The mapper provider.</param>
62+
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
63+
public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder,
64+
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
65+
{
66+
if (builder is null)
67+
{
68+
throw new ArgumentNullException(nameof(builder));
69+
}
70+
71+
return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider));
72+
}
73+
#endregion
74+
75+
private static Action<MvcNewtonsoftJsonOptions> BuildSetupAction(Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
76+
{
77+
Action<MvcNewtonsoftJsonOptions> odataSetupAction = opt =>
78+
{
79+
if (mapperProvider is null)
80+
{
81+
opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter());
82+
}
83+
else
84+
{
85+
opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter(mapperProvider));
86+
}
87+
88+
opt.SerializerSettings.Converters.Add(new JDynamicTypeWrapperConverter());
89+
opt.SerializerSettings.Converters.Add(new JPageResultValueConverter());
90+
opt.SerializerSettings.Converters.Add(new JSingleResultValueConverter());
91+
};
92+
93+
return odataSetupAction;
94+
}
95+
}
96+
}

src/Microsoft.AspNetCore.OData.NewtonsoftJson/ODataNewtonsoftServiceCollectionExtensions.cs

-81
This file was deleted.

0 commit comments

Comments
 (0)