Skip to content

Commit

Permalink
Update newtonsoft.json
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Apr 30, 2021
1 parent 47500ff commit 9a431c7
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 141 deletions.
24 changes: 11 additions & 13 deletions sample/ODataNewtonsoftJsonSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

services.AddOData(opt => opt.Select().Filter().AddModel("odata", GetEdmModel())).AddNewtonsoftJson(
options =>
{
options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
//options.SerializerSettings.ContractResolver = WebApiJsonResolver.Instance;
});

// You can also add the converter one by one using followings:
//services.AddControllers().AddNewtonsoftJson(
// opt => opt.SerializerSettings.Converters.Add(new JDynamicTypeWrapperConverter()));
services.AddControllers()
.AddOData(opt => opt.Select().Filter().AddModel("odata", GetEdmModel()))
.AddODataNewtonsoftJson()
//.AddNewtonsoftJson(
//options =>
//{
// options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
// options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// //options.SerializerSettings.ContractResolver = WebApiJsonResolver.Instance;
//})
;
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
/// <summary>
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="DynamicTypeWrapper"/> instances to JSON.
/// </summary>
public class JDynamicTypeWrapperConverter : JsonConverter
internal class JDynamicTypeWrapperConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified <see cref="DynamicTypeWrapper"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
/// <summary>
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="PageResult"/> instances to JSON.
/// </summary>
public class JPageResultValueConverter : JsonConverter
internal class JPageResultValueConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified <see cref="PageResult"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
/// <summary>
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="ISelectExpandWrapper"/> instances to JSON.
/// </summary>
public class JSelectExpandWrapperConverter : JsonConverter
internal class JSelectExpandWrapperConverter : JsonConverter
{
private Func<IEdmModel, IEdmStructuredType, IPropertyMapper> _mapperProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
/// <summary>
/// Represents a custom <see cref="JsonConverter"/> to serialize <see cref="SingleResult"/> instances to JSON.
/// </summary>
public class JSingleResultValueConverter : JsonConverter
internal class JSingleResultValueConverter : JsonConverter
{
/// <summary>
/// Determines whether this instance can convert the specified <see cref="SingleResult"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.OData.NewtonsoftJson
/// <summary>
/// Edm Property name mapper.
/// </summary>
public class JsonPropertyNameMapper : IPropertyMapper
internal class JsonPropertyNameMapper : IPropertyMapper
{
private IEdmModel _model;
private IEdmStructuredType _type;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query.Container;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;

namespace Microsoft.AspNetCore.OData.NewtonsoftJson
{
/// <summary>
/// Extension methods for adding OData Json converter to Newtonsoft.Json to <see cref="IMvcBuilder"/> and <see cref="IMvcCoreBuilder"/>.
/// </summary>
public static class ODataNewtonsoftJsonMvcBuilderExtensions
{
#region IMvcBuilder
/// <summary>
/// Configures Newtonsoft.Json using OData Json converter.
/// </summary>
/// <param name="builder">The Mvc builder.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder)
{
return builder.AddODataNewtonsoftJson(null);
}

/// <summary>
/// Configures Newtonsoft.Json using OData Json converter.
/// </summary>
/// <param name="builder">The Mvc builder.</param>
/// <param name="mapperProvider">The mapper provider.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder,
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider));
}
#endregion

#region IMvcCoreBuilder
/// <summary>
/// Configures Newtonsoft.Json using OData Json converter.
/// </summary>
/// <param name="builder">The Mvc core builder.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder)
{
return builder.AddNewtonsoftJson(null);
}

/// <summary>
/// Configures Newtonsoft.Json using OData Json converter.
/// </summary>
/// <param name="builder">The Mvc core builder.</param>
/// <param name="mapperProvider">The mapper provider.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder,
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

return builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider));
}
#endregion

private static Action<MvcNewtonsoftJsonOptions> BuildSetupAction(Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
{
Action<MvcNewtonsoftJsonOptions> odataSetupAction = opt =>
{
if (mapperProvider is null)
{
opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter());
}
else
{
opt.SerializerSettings.Converters.Add(new JSelectExpandWrapperConverter(mapperProvider));
}
opt.SerializerSettings.Converters.Add(new JDynamicTypeWrapperConverter());
opt.SerializerSettings.Converters.Add(new JPageResultValueConverter());
opt.SerializerSettings.Converters.Add(new JSingleResultValueConverter());
};

return odataSetupAction;
}
}
}

This file was deleted.

Loading

0 comments on commit 9a431c7

Please sign in to comment.