mirror of https://github.com/abpframework/abp.git
committed by
GitHub
15 changed files with 136 additions and 34 deletions
@ -0,0 +1,21 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Text.Json.Serialization; |
||||
|
|
||||
|
namespace System.Text.Json |
||||
|
{ |
||||
|
public static class JsonSerializerOptionsHelper |
||||
|
{ |
||||
|
public static JsonSerializerOptions Create(JsonSerializerOptions baseOptions, JsonConverter removeConverter, params JsonConverter[] addConverters) |
||||
|
{ |
||||
|
return Create(baseOptions, x => x == removeConverter, addConverters); |
||||
|
} |
||||
|
|
||||
|
public static JsonSerializerOptions Create(JsonSerializerOptions baseOptions, Func<JsonConverter, bool> removeConverterPredicate, params JsonConverter[] addConverters) |
||||
|
{ |
||||
|
var options = new JsonSerializerOptions(baseOptions); |
||||
|
options.Converters.RemoveAll(removeConverterPredicate); |
||||
|
options.Converters.AddIfNotContains(addConverters); |
||||
|
return options; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
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 = JsonSerializerOptionsHelper.Create(options, 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 = JsonSerializerOptionsHelper.Create(options, 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)!; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,17 +0,0 @@ |
|||||
using System.Collections.Generic; |
|
||||
using System.Text.Json; |
|
||||
using System.Text.Json.Serialization; |
|
||||
|
|
||||
namespace Volo.Abp.FeatureManagement.JsonConverters |
|
||||
{ |
|
||||
internal static class JsonSerializerOptionsHelper |
|
||||
{ |
|
||||
public static JsonSerializerOptions Create(JsonSerializerOptions baseOptions, JsonConverter removeConverter, params JsonConverter[] addConverters) |
|
||||
{ |
|
||||
var options = new JsonSerializerOptions(baseOptions); |
|
||||
options.Converters.RemoveAll(x => x == removeConverter); |
|
||||
options.Converters.AddIfNotContains(addConverters); |
|
||||
return options; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue