mirror of https://github.com/abpframework/abp.git
15 changed files with 161 additions and 140 deletions
@ -1,21 +1,31 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Newtonsoft.Json; |
|||
using Volo.Abp.Json.Newtonsoft; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; |
|||
|
|||
public class AbpMvcNewtonsoftJsonOptionsSetup : IConfigureOptions<MvcNewtonsoftJsonOptions> |
|||
{ |
|||
protected IServiceProvider ServiceProvider { get; } |
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public AbpMvcNewtonsoftJsonOptionsSetup(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
_serviceProvider = serviceProvider; |
|||
} |
|||
|
|||
public void Configure(MvcNewtonsoftJsonOptions options) |
|||
{ |
|||
options.SerializerSettings.ContractResolver = ServiceProvider.GetRequiredService<AbpNewtonsoftJsonContractResolver>(); |
|||
options.SerializerSettings.ContractResolver = _serviceProvider.GetRequiredService<AbpCamelCasePropertyNamesContractResolver>(); |
|||
|
|||
var converters = _serviceProvider.GetRequiredService<IOptions<AbpNewtonsoftJsonSerializerOptions>>().Value |
|||
.Converters.Select(converterType => _serviceProvider.GetRequiredService(converterType).As<JsonConverter>()) |
|||
.ToList(); |
|||
|
|||
options.SerializerSettings.Converters.InsertRange(0, converters); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,16 @@ |
|||
using System.Text.Json.Serialization.Metadata; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson; |
|||
|
|||
public class AbpDefaultJsonTypeInfoResolver : DefaultJsonTypeInfoResolver, ITransientDependency |
|||
{ |
|||
public AbpDefaultJsonTypeInfoResolver(IOptions<AbpSystemTextJsonSerializerModifiersOptions> options) |
|||
{ |
|||
foreach (var modifier in options.Value.Modifiers) |
|||
{ |
|||
Modifiers.Add(modifier); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson; |
|||
|
|||
public class AbpSystemTextJsonSerializerModifiersOptions |
|||
{ |
|||
public List<Action<JsonTypeInfo>> Modifiers { get; } |
|||
|
|||
public AbpSystemTextJsonSerializerModifiersOptions() |
|||
{ |
|||
Modifiers = new List<Action<JsonTypeInfo>> |
|||
{ |
|||
new IncludeNonPublicPropertiesModifiers<ExtensibleObject, ExtraPropertyDictionary>().CreateModifyAction("ExtraProperties") |
|||
}; |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson; |
|||
|
|||
public class AbpSystemTextJsonUnsupportedTypeMatcher : ITransientDependency |
|||
{ |
|||
protected AbpSystemTextJsonSerializerOptions Options { get; } |
|||
|
|||
public AbpSystemTextJsonUnsupportedTypeMatcher(IOptions<AbpSystemTextJsonSerializerOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual bool Match([CanBeNull] Type type) |
|||
{ |
|||
return Options.UnsupportedTypes.Contains(type); |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
|
|||
public class IncludeNonPublicPropertiesModifiers<TClass, TProperty> |
|||
where TClass : class |
|||
{ |
|||
private string _propertyName; |
|||
|
|||
public Action<JsonTypeInfo> CreateModifyAction(string propertyName) |
|||
{ |
|||
_propertyName = propertyName; |
|||
return Modify; |
|||
} |
|||
|
|||
public void Modify(JsonTypeInfo jsonTypeInfo) |
|||
{ |
|||
if (jsonTypeInfo.Type == typeof(TClass)) |
|||
{ |
|||
var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.Name.Equals(_propertyName, StringComparison.OrdinalIgnoreCase) && x.Set == null); |
|||
if (propertyJsonInfo != null) |
|||
{ |
|||
var propertyInfo = typeof(TClass).GetProperty(_propertyName); |
|||
if (propertyInfo != null) |
|||
{ |
|||
var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(TProperty), propertyJsonInfo.Name); |
|||
jsonPropertyInfo.Get = propertyInfo.GetValue; |
|||
jsonPropertyInfo.Set = propertyInfo.SetValue; |
|||
jsonTypeInfo.Properties.Remove(propertyJsonInfo); |
|||
jsonTypeInfo.Properties.Add(jsonPropertyInfo); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,65 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json; |
|||
|
|||
public class AbpSystemTextJsonUnsupportedTypeMatcher_Tests : AbpJsonTestBase |
|||
{ |
|||
private readonly AbpSystemTextJsonUnsupportedTypeMatcher _abpSystemTextJsonUnsupportedTypeMatcher; |
|||
|
|||
public AbpSystemTextJsonUnsupportedTypeMatcher_Tests() |
|||
{ |
|||
_abpSystemTextJsonUnsupportedTypeMatcher = GetRequiredService<AbpSystemTextJsonUnsupportedTypeMatcher>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
services.Configure<AbpSystemTextJsonSerializerOptions>(options => |
|||
{ |
|||
options.UnsupportedTypes.Add<MyClass>(); |
|||
options.UnsupportedTypes.Add<byte[]>(); |
|||
options.UnsupportedTypes.Add<Dictionary<string, MyClass4>>(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Match_Test() |
|||
{ |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass)).ShouldBeTrue(); |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(byte[])).ShouldBeTrue(); |
|||
|
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass2)).ShouldBeFalse(); |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass3)).ShouldBeFalse(); |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass4)).ShouldBeFalse(); |
|||
|
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(string)).ShouldBeFalse(); |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(string[])).ShouldBeFalse(); |
|||
|
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(Dictionary<string, MyClass4>)).ShouldBeTrue(); |
|||
_abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(IDictionary<string, MyClass4>)).ShouldBeFalse(); |
|||
} |
|||
|
|||
class MyClass |
|||
{ |
|||
public DateTime Prop1 { get; set; } |
|||
} |
|||
|
|||
class MyClass2 |
|||
{ |
|||
public DateTime Prop1 { get; set; } |
|||
} |
|||
|
|||
class MyClass3 |
|||
{ |
|||
public MyClass4 Prop1 { get; set; } |
|||
} |
|||
|
|||
class MyClass4 |
|||
{ |
|||
public DateTime Prop1 { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson; |
|||
using Volo.Abp.ObjectExtending; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json; |
|||
|
|||
public class ExtensibleObjectModifiers_Tests : AbpJsonTestBase |
|||
{ |
|||
[Fact] |
|||
public void Should_Modify_Object() |
|||
{ |
|||
var jsonSerializer = GetRequiredService<AbpSystemTextJsonSerializerProvider>(); |
|||
|
|||
var extensibleObject = jsonSerializer.Deserialize<ExtensibleObject>("{\"ExtraProperties\": {\"Test-Key\":\"Test-Value\"}}"); |
|||
|
|||
extensibleObject.ExtraProperties.ShouldContainKeyAndValue("Test-Key", "Test-Value"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue