Use non-static to store excluded types.

pull/9626/head
maliming 4 years ago
parent a99c2aaa94
commit f59abe5aa0

@ -11,11 +11,23 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
{ {
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
var newOptions = JsonSerializerOptionsHelper.Create(options, x => var newOptions = JsonSerializerOptionsHelper.Create(options, x => x == this);
x == this ||
x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory)); var converterFactory = newOptions.Converters.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory).As<AbpHasExtraPropertiesJsonConverterFactory>();
var newConverterFactory = new AbpHasExtraPropertiesJsonConverterFactory();
if (converterFactory != null)
{
newOptions.Converters.Remove(converterFactory);
newConverterFactory.AddExcludeTypes(converterFactory.GetExcludeTypes().ToArray());
}
else
{
newConverterFactory.AddExcludeTypes(typeToConvert);
}
newConverterFactory.AddExcludeTypes(typeToConvert);
newOptions.Converters.Add(newConverterFactory);
newOptions.Converters.Add(new AbpHasExtraPropertiesJsonConverterFactory(typeToConvert));
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
if (rootElement.ValueKind == JsonValueKind.Object) if (rootElement.ValueKind == JsonValueKind.Object)
{ {

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -12,16 +13,22 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
{ {
private static readonly ConcurrentDictionary<Type, bool> CachedTypes = new ConcurrentDictionary<Type, bool>(); private static readonly ConcurrentDictionary<Type, bool> CachedTypes = new ConcurrentDictionary<Type, bool>();
private static readonly List<Type> ExcludeTypes = new List<Type>(); private readonly List<Type> _excludeTypes = new List<Type>();
public AbpHasExtraPropertiesJsonConverterFactory(params Type[] excludeTypes) public virtual AbpHasExtraPropertiesJsonConverterFactory AddExcludeTypes(params Type[] excludeTypes)
{ {
ExcludeTypes.AddIfNotContains(excludeTypes); _excludeTypes.AddIfNotContains(excludeTypes);
return this;
}
public virtual IReadOnlyList<Type> GetExcludeTypes()
{
return _excludeTypes.ToImmutableList();
} }
public override bool CanConvert(Type typeToConvert) public override bool CanConvert(Type typeToConvert)
{ {
if (ExcludeTypes.Contains(typeToConvert)) if (_excludeTypes.Contains(typeToConvert))
{ {
return false; return false;
} }

@ -43,6 +43,22 @@ namespace Volo.Abp.Json
fooDto.BarDtos.Count.ShouldBe(1); fooDto.BarDtos.Count.ShouldBe(1);
fooDto.BarDtos.First().Name.ShouldBe("bar-dto"); fooDto.BarDtos.First().Name.ShouldBe("bar-dto");
fooDto.BarDtos.First().GetProperty("bar").ShouldBe("bar-value"); fooDto.BarDtos.First().GetProperty("bar").ShouldBe("bar-value");
fooDto.Name = "new-foo-dto";
fooDto.SetProperty("foo", "new-foo-value");
fooDto.BarDtos.First().Name = "new-bar-dto";
fooDto.BarDtos.First().SetProperty("bar", "new-bar-value");
json = _jsonSerializer.Serialize(fooDto);
fooDto = _jsonSerializer.Deserialize<FooDto>(json);
fooDto.ShouldNotBeNull();
fooDto.Name.ShouldBe("new-foo-dto");
fooDto.GetProperty("foo").ShouldBe("new-foo-value");
fooDto.BarDtos.Count.ShouldBe(1);
fooDto.BarDtos.First().Name.ShouldBe("new-bar-dto");
fooDto.BarDtos.First().GetProperty("bar").ShouldBe("new-bar-value");
} }
} }

Loading…
Cancel
Save