Browse Source
Reuse more JsonSerializerOptions instances.
pull/10775/head
maliming
5 years ago
No known key found for this signature in database
GPG Key ID: 96224957E51C89E
7 changed files with
71 additions and
42 deletions
-
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/AbpJsonValueConverter.cs
-
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs
-
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
-
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs
-
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs
-
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverterFactory.cs
-
framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs
|
|
|
@ -19,11 +19,17 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters |
|
|
|
return JsonSerializer.Serialize(d); |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions DeserializeOptions = new JsonSerializerOptions() |
|
|
|
{ |
|
|
|
Converters = |
|
|
|
{ |
|
|
|
new ObjectToInferredTypesConverter() |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
private static TPropertyType DeserializeObject(string s) |
|
|
|
{ |
|
|
|
var deserializeOptions = new JsonSerializerOptions(); |
|
|
|
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter()); |
|
|
|
return JsonSerializer.Deserialize<TPropertyType>(s, deserializeOptions); |
|
|
|
return JsonSerializer.Deserialize<TPropertyType>(s, DeserializeOptions); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -41,17 +41,22 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters |
|
|
|
return JsonSerializer.Serialize(copyDictionary); |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions DeserializeOptions = new JsonSerializerOptions() |
|
|
|
{ |
|
|
|
Converters = |
|
|
|
{ |
|
|
|
new ObjectToInferredTypesConverter() |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
private static ExtraPropertyDictionary DeserializeObject(string extraPropertiesAsJson, Type entityType) |
|
|
|
{ |
|
|
|
if (extraPropertiesAsJson.IsNullOrEmpty() || extraPropertiesAsJson == "{}") |
|
|
|
{ |
|
|
|
return new ExtraPropertyDictionary(); |
|
|
|
} |
|
|
|
|
|
|
|
var deserializeOptions = new JsonSerializerOptions(); |
|
|
|
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter()); |
|
|
|
|
|
|
|
var dictionary = JsonSerializer.Deserialize<ExtraPropertyDictionary>(extraPropertiesAsJson, deserializeOptions) ?? |
|
|
|
|
|
|
|
var dictionary = JsonSerializer.Deserialize<ExtraPropertyDictionary>(extraPropertiesAsJson, DeserializeOptions) ?? |
|
|
|
new ExtraPropertyDictionary(); |
|
|
|
|
|
|
|
if (entityType != null) |
|
|
|
|
|
|
|
@ -90,6 +90,11 @@ namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
return await Cache.GetAsync(baseUrl, () => GetApiDescriptionFromServerAsync(client, baseUrl)); |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions DeserializeOptions = new JsonSerializerOptions |
|
|
|
{ |
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
|
|
|
}; |
|
|
|
|
|
|
|
protected virtual async Task<ApplicationApiDescriptionModel> GetApiDescriptionFromServerAsync( |
|
|
|
HttpClient client, |
|
|
|
string baseUrl) |
|
|
|
@ -113,10 +118,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying |
|
|
|
|
|
|
|
var content = await response.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
var result = JsonSerializer.Deserialize<ApplicationApiDescriptionModel>(content, new JsonSerializerOptions |
|
|
|
{ |
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
|
|
|
}); |
|
|
|
var result = JsonSerializer.Deserialize<ApplicationApiDescriptionModel>(content, DeserializeOptions); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Concurrent; |
|
|
|
using System.Text.Json; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
@ -39,21 +40,26 @@ namespace Volo.Abp.Json.SystemTextJson |
|
|
|
return JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase)); |
|
|
|
} |
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, JsonSerializerOptions> JsonSerializerOptionsCache = new ConcurrentDictionary<string, JsonSerializerOptions>(); |
|
|
|
|
|
|
|
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) |
|
|
|
{ |
|
|
|
var settings = new JsonSerializerOptions(Options.JsonSerializerOptions); |
|
|
|
|
|
|
|
if (camelCase) |
|
|
|
return JsonSerializerOptionsCache.GetOrAdd($"default{camelCase}{indented}", _ => |
|
|
|
{ |
|
|
|
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
|
|
|
} |
|
|
|
var settings = new JsonSerializerOptions(Options.JsonSerializerOptions); |
|
|
|
|
|
|
|
if (indented) |
|
|
|
{ |
|
|
|
settings.WriteIndented = true; |
|
|
|
} |
|
|
|
if (camelCase) |
|
|
|
{ |
|
|
|
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
|
|
|
} |
|
|
|
|
|
|
|
if (indented) |
|
|
|
{ |
|
|
|
settings.WriteIndented = true; |
|
|
|
} |
|
|
|
|
|
|
|
return settings; |
|
|
|
return settings; |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -17,13 +17,18 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|
|
|
{ |
|
|
|
_readJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, x => x == this); |
|
|
|
|
|
|
|
var converterFactory = _readJsonSerializerOptions.Converters.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory); |
|
|
|
converterFactory?.As<AbpHasExtraPropertiesJsonConverterFactory>().AddExcludeTypes(typeToConvert); |
|
|
|
|
|
|
|
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; |
|
|
|
if (rootElement.ValueKind == JsonValueKind.Object) |
|
|
|
{ |
|
|
|
var extensibleObject = rootElement.Deserialize<T>(_readJsonSerializerOptions); |
|
|
|
var converterFactory = _readJsonSerializerOptions.Converters |
|
|
|
.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory) |
|
|
|
.As<AbpHasExtraPropertiesJsonConverterFactory>(); |
|
|
|
|
|
|
|
T extensibleObject; |
|
|
|
using (converterFactory != null ? converterFactory.Exclude(typeToConvert) : NullDisposable.Instance) |
|
|
|
{ |
|
|
|
extensibleObject = rootElement.Deserialize<T>(_readJsonSerializerOptions); |
|
|
|
} |
|
|
|
|
|
|
|
var extraPropertiesJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IHasExtraProperties.ExtraProperties), StringComparison.OrdinalIgnoreCase)); |
|
|
|
|
|
|
|
|
|
|
|
@ -1,10 +1,11 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Concurrent; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Text.Json.Serialization; |
|
|
|
using System.Threading; |
|
|
|
using Volo.Abp.Data; |
|
|
|
|
|
|
|
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|
|
|
@ -13,17 +14,21 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|
|
|
{ |
|
|
|
private static readonly ConcurrentDictionary<Type, bool> CachedTypes = new ConcurrentDictionary<Type, bool>(); |
|
|
|
|
|
|
|
private readonly List<Type> _excludeTypes = new List<Type>(); |
|
|
|
private readonly AsyncLocal<List<Type>> _excludeTypes = new AsyncLocal<List<Type>>(); |
|
|
|
|
|
|
|
public virtual AbpHasExtraPropertiesJsonConverterFactory AddExcludeTypes(params Type[] excludeTypes) |
|
|
|
public IDisposable Exclude(params Type[] excludeTypes) |
|
|
|
{ |
|
|
|
_excludeTypes.AddIfNotContains(excludeTypes); |
|
|
|
return this; |
|
|
|
var parent = _excludeTypes.Value; |
|
|
|
_excludeTypes.Value = excludeTypes.ToList(); |
|
|
|
return new DisposeAction(() => |
|
|
|
{ |
|
|
|
_excludeTypes.Value = parent; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override bool CanConvert(Type typeToConvert) |
|
|
|
{ |
|
|
|
if (_excludeTypes.Contains(typeToConvert)) |
|
|
|
if (_excludeTypes.Value != null && _excludeTypes.Value.Contains(typeToConvert)) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
@ -24,6 +24,14 @@ namespace Volo.Abp.Localization.Json |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions DeserializeOptions = new JsonSerializerOptions |
|
|
|
{ |
|
|
|
PropertyNameCaseInsensitive = true, |
|
|
|
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, |
|
|
|
ReadCommentHandling = JsonCommentHandling.Skip, |
|
|
|
AllowTrailingCommas = true |
|
|
|
}; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Builds an <see cref="JsonLocalizationDictionaryBuilder" /> from given json string.
|
|
|
|
/// </summary>
|
|
|
|
@ -33,15 +41,7 @@ namespace Volo.Abp.Localization.Json |
|
|
|
JsonLocalizationFile jsonFile; |
|
|
|
try |
|
|
|
{ |
|
|
|
var options = new JsonSerializerOptions |
|
|
|
{ |
|
|
|
PropertyNameCaseInsensitive = true, |
|
|
|
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, |
|
|
|
ReadCommentHandling = JsonCommentHandling.Skip, |
|
|
|
AllowTrailingCommas = true |
|
|
|
}; |
|
|
|
|
|
|
|
jsonFile = JsonSerializer.Deserialize<JsonLocalizationFile>(jsonString, options); |
|
|
|
jsonFile = JsonSerializer.Deserialize<JsonLocalizationFile>(jsonString, DeserializeOptions); |
|
|
|
} |
|
|
|
catch (JsonException ex) |
|
|
|
{ |
|
|
|
|