mirror of https://github.com/abpframework/abp.git
7 changed files with 96 additions and 2 deletions
@ -0,0 +1,39 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpExtraPropertyDictionaryJsonConverter<T> : JsonConverter<T> |
|||
where T : ExtensibleObject |
|||
{ |
|||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
var newOptions = new JsonSerializerOptions(options); |
|||
newOptions.Converters.RemoveAll(x => x == this || x.GetType() == typeof(AbpExtraPropertyDictionaryJsonConverterFactory)); |
|||
|
|||
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; |
|||
var extensibleObject = JsonSerializer.Deserialize<T>(rootElement.GetRawText(), newOptions); |
|||
var extraProperties = rootElement.EnumerateObject().FirstOrDefault(x => |
|||
x.Name.Equals(nameof(ExtensibleObject.ExtraProperties), StringComparison.OrdinalIgnoreCase)) |
|||
.Value.GetRawText(); |
|||
|
|||
var extraPropertyDictionary = JsonSerializer.Deserialize(extraProperties, typeof(ExtraPropertyDictionary), newOptions); |
|||
|
|||
ObjectHelper.TrySetProperty(extensibleObject, x => x.ExtraProperties, () => extraPropertyDictionary); |
|||
|
|||
return extensibleObject; |
|||
} |
|||
|
|||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) |
|||
{ |
|||
var newOptions = new JsonSerializerOptions(options); |
|||
newOptions.Converters.RemoveAll(x => x == this || x.GetType() == typeof(AbpExtraPropertyDictionaryJsonConverterFactory)); |
|||
JsonSerializer.Serialize(writer, value, newOptions); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpExtraPropertyDictionaryJsonConverterFactory : JsonConverterFactory |
|||
{ |
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
return typeToConvert == typeof(ExtensibleObject) || typeToConvert.IsSubclassOf(typeof(ExtensibleObject)); |
|||
} |
|||
|
|||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
return (JsonConverter) Activator.CreateInstance( |
|||
typeof(AbpExtraPropertyDictionaryJsonConverter<>).MakeGenericType(typeToConvert), |
|||
BindingFlags.Instance | BindingFlags.Public, |
|||
binder: null, |
|||
null, |
|||
culture: null)!; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue