Supports nested ExtensibleObject classes.

Resolve #9581
pull/9626/head
maliming 4 years ago
parent cf8d33eb5e
commit a99c2aaa94

@ -15,6 +15,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
x == this || x == this ||
x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory)); x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory));
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,5 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -11,8 +12,20 @@ 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>();
public AbpHasExtraPropertiesJsonConverterFactory(params Type[] excludeTypes)
{
ExcludeTypes.AddIfNotContains(excludeTypes);
}
public override bool CanConvert(Type typeToConvert) public override bool CanConvert(Type typeToConvert)
{ {
if (ExcludeTypes.Contains(typeToConvert))
{
return false;
}
//Only for private or protected ExtraProperties. //Only for private or protected ExtraProperties.
if (typeof(IHasExtraProperties).IsAssignableFrom(typeToConvert)) if (typeof(IHasExtraProperties).IsAssignableFrom(typeToConvert))
{ {

@ -0,0 +1,60 @@
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Xunit;
namespace Volo.Abp.Json
{
public class AbpHasExtraPropertiesJsonConverter_Tests: AbpJsonTestBase
{
private readonly IJsonSerializer _jsonSerializer;
public AbpHasExtraPropertiesJsonConverter_Tests()
{
_jsonSerializer = GetRequiredService<IJsonSerializer>();
}
[Fact]
public void JsonConverter_Test()
{
var fooDto = new FooDto
{
Name = "foo-dto",
BarDtos = new List<BarDto>()
};
fooDto.SetProperty("foo", "foo-value");
var barDto = new BarDto
{
Name = "bar-dto"
};
barDto.SetProperty("bar", "bar-value");
fooDto.BarDtos.Add(barDto);
var json = _jsonSerializer.Serialize(fooDto);
fooDto = _jsonSerializer.Deserialize<FooDto>(json);
fooDto.ShouldNotBeNull();
fooDto.Name.ShouldBe("foo-dto");
fooDto.GetProperty("foo").ShouldBe("foo-value");
fooDto.BarDtos.Count.ShouldBe(1);
fooDto.BarDtos.First().Name.ShouldBe("bar-dto");
fooDto.BarDtos.First().GetProperty("bar").ShouldBe("bar-value");
}
}
public class FooDto : ExtensibleObject
{
public string Name { get; set; }
public List<BarDto> BarDtos { get; set; }
}
public class BarDto : ExtensibleObject
{
public string Name { get; set; }
}
}
Loading…
Cancel
Save