Browse Source

Move to System.Text.Json

Resolve #1198
pull/5952/head
maliming 6 years ago
parent
commit
7b06cfb5a2
  1. 7
      framework/Volo.Abp.sln
  2. 1
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo.Abp.AspNetCore.Mvc.csproj
  3. 8
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs
  4. 25
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs
  5. 46
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs
  6. 39
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs
  7. 45
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingRuntimeIgnoreConverter.cs
  8. 29
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs
  9. 39
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs
  10. 29
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityJsonConverter.cs
  11. 12
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/AbpJsonValueConverter.cs
  12. 15
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs
  13. 15
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
  14. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ActionApiDescriptionModel.cs
  15. 6
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ApplicationApiDescriptionModel.cs
  16. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs
  17. 6
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerInterfaceApiDescriptionModel.cs
  18. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs
  19. 14
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModuleApiDescriptionModel.cs
  20. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs
  21. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ReturnValueApiDescriptionModel.cs
  22. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/TypeApiDescriptionModel.cs
  23. 3
      framework/src/Volo.Abp.Json.Newtonsoft/FodyWeavers.xml
  24. 30
      framework/src/Volo.Abp.Json.Newtonsoft/FodyWeavers.xsd
  25. 25
      framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj
  26. 21
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/AbpJsonModule.cs
  27. 0
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs
  28. 0
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerOptions.cs
  29. 1
      framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs
  30. 4
      framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj
  31. 10
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs
  32. 2
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonOptions.cs
  33. 49
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializer.cs
  34. 18
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializerOptions.cs
  35. 23
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializerOptionsSetup.cs
  36. 38
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/AbpDateTimeConverter.cs
  37. 50
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/AbpNullableDateTimeConverter.cs
  38. 54
      framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/ObjectToInferredTypesConverter.cs
  39. 4
      framework/src/Volo.Abp.Localization/Volo.Abp.Localization.csproj
  40. 19
      framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs
  41. 4
      framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationFile.cs
  42. 41
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs
  43. 14
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializerOptions.cs
  44. 1
      framework/test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/MultipleClaimAction_Tests.cs
  45. 14
      framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs
  46. 11
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs
  47. 1
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests_With_Int_Pk.cs
  48. 4
      modules/docs/src/Volo.Docs.Domain.Shared/Volo.Docs.Domain.Shared.csproj
  49. 11
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs
  50. 24
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/LocalizableStringInfoJsonConverter.cs
  51. 36
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemJsonConverter.cs
  52. 36
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemSourceJsonConverter.cs
  53. 47
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/StringValueTypeJsonConverter.cs
  54. 68
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/ValueValidatorJsonConverter.cs
  55. 90
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs
  56. 6
      modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs
  57. 1
      nupkg/common.ps1

7
framework/Volo.Abp.sln

@ -355,6 +355,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.AspNetCore.Compone
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Swashbuckle", "src\Volo.Abp.Swashbuckle\Volo.Abp.Swashbuckle.csproj", "{DD9519E0-5A68-48DC-A051-7BF2AC922F3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Json.Newtonsoft", "src\Volo.Abp.Json.Newtonsoft\Volo.Abp.Json.Newtonsoft.csproj", "{B5DEA857-1A22-4479-8441-E871D1BCBAB2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -1057,6 +1059,10 @@ Global
{DD9519E0-5A68-48DC-A051-7BF2AC922F3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD9519E0-5A68-48DC-A051-7BF2AC922F3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD9519E0-5A68-48DC-A051-7BF2AC922F3E}.Release|Any CPU.Build.0 = Release|Any CPU
{B5DEA857-1A22-4479-8441-E871D1BCBAB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5DEA857-1A22-4479-8441-E871D1BCBAB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5DEA857-1A22-4479-8441-E871D1BCBAB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5DEA857-1A22-4479-8441-E871D1BCBAB2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -1236,6 +1242,7 @@ Global
{B9D1ADCB-D552-4626-A1F1-78FF72C1E822} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{89840441-5A3A-4FD7-9CB4-E5B52FAEF72A} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{DD9519E0-5A68-48DC-A051-7BF2AC922F3E} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{B5DEA857-1A22-4479-8441-E871D1BCBAB2} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

1
framework/src/Volo.Abp.AspNetCore.Mvc/Volo.Abp.AspNetCore.Mvc.csproj

@ -29,7 +29,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.0-rc.2.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0-rc.2.*" />
</ItemGroup>
</Project>

8
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs

@ -35,6 +35,7 @@ using Volo.Abp.Http;
using Volo.Abp.DynamicProxy;
using Volo.Abp.GlobalFeatures;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Json.Microsoft;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.UI;
@ -114,11 +115,6 @@ namespace Volo.Abp.AspNetCore.Mvc
);
var mvcBuilder = context.Services.AddMvc()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver =
new AbpMvcJsonContractResolver(context.Services);
})
.AddRazorRuntimeCompilation()
.AddDataAnnotationsLocalization(options =>
{
@ -139,6 +135,8 @@ namespace Volo.Abp.AspNetCore.Mvc
})
.AddViewLocalization(); //TODO: How to configure from the application? Also, consider to move to a UI module since APIs does not care about it.
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<JsonOptions>, AbpJsonOptionsSetup>());
Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(

25
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs

@ -0,0 +1,25 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.Microsoft;
using Volo.Abp.Json.Microsoft.JsonConverters;
namespace Volo.Abp.AspNetCore.Mvc.Json
{
public class AbpJsonOptionsSetup : IConfigureOptions<JsonOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpJsonOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(JsonOptions options)
{
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
}
}
}

46
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs

@ -1,46 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Reflection;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Reflection;
using Volo.Abp.Timing;
namespace Volo.Abp.AspNetCore.Mvc.Json
{
public class AbpMvcJsonContractResolver : DefaultContractResolver
{
private readonly Lazy<AbpJsonIsoDateTimeConverter> _dateTimeConverter;
public AbpMvcJsonContractResolver(IServiceCollection services)
{
_dateTimeConverter = services.GetServiceLazy<AbpJsonIsoDateTimeConverter>();
NamingStrategy = new CamelCaseNamingStrategy();
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
ModifyProperty(member, property);
return property;
}
protected virtual void ModifyProperty(MemberInfo member, JsonProperty property)
{
if (property.PropertyType != typeof(DateTime) && property.PropertyType != typeof(DateTime?))
{
return;
}
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableDateTimeNormalizationAttribute>(member) == null)
{
property.Converter = _dateTimeConverter.Value;
}
}
}
}

39
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Volo.Abp.Auditing
{
public class AuditingContractResolver : CamelCasePropertyNamesContractResolver
{
private readonly List<Type> _ignoredTypes;
public AuditingContractResolver(List<Type> ignoredTypes)
{
_ignoredTypes = ignoredTypes;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (member.IsDefined(typeof(DisableAuditingAttribute)) || member.IsDefined(typeof(JsonIgnoreAttribute)))
{
property.ShouldSerialize = instance => false;
}
foreach (var ignoredType in _ignoredTypes)
{
if (ignoredType.GetTypeInfo().IsAssignableFrom(property.PropertyType))
{
property.ShouldSerialize = instance => false;
break;
}
}
return property;
}
}
}

45
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingRuntimeIgnoreConverter.cs

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.Auditing
{
public class AuditingRuntimeIgnoreConverter : JsonConverter<Dictionary<string, object>>
{
private readonly List<Type> _ignoredTypes;
public AuditingRuntimeIgnoreConverter(List<Type> ignoredTypes)
{
_ignoredTypes = ignoredTypes;
}
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new JsonException();
}
public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
{
var newDictionary = new Dictionary<string, object>();
foreach (var item in value.Where(x => x.Value != null))
{
if (item.GetType().IsDefined(typeof(DisableAuditingAttribute), true) ||
item.GetType().IsDefined(typeof(JsonIgnoreAttribute), true))
{
continue;
}
if (_ignoredTypes.Any(x => x.IsInstanceOfType(item.Value)))
{
continue;
}
newDictionary[item.Key] = item.Value;
}
JsonSerializer.Serialize(writer, newDictionary);
}
}
}

29
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs

@ -1,5 +1,5 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Auditing
@ -16,29 +16,14 @@ namespace Volo.Abp.Auditing
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj, GetSharedJsonSerializerSettings());
return JsonSerializer.Serialize(obj, GetJsonSerializerOptions());
}
private static readonly object SyncObj = new object();
private static JsonSerializerSettings _sharedJsonSerializerSettings;
private JsonSerializerSettings GetSharedJsonSerializerSettings()
private JsonSerializerOptions GetJsonSerializerOptions()
{
if (_sharedJsonSerializerSettings == null)
{
lock (SyncObj)
{
if (_sharedJsonSerializerSettings == null)
{
_sharedJsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new AuditingContractResolver(Options.IgnoredTypes)
};
}
}
}
return _sharedJsonSerializerSettings;
var options = new JsonSerializerOptions();
options.Converters.Add(new AuditingRuntimeIgnoreConverter(Options.IgnoredTypes));
return options;
}
}
}
}

39
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityHelper.cs

@ -77,7 +77,7 @@ namespace Volo.Abp.Domain.Entities
{
var entity1Key = entity1Keys[i];
var entity2Key = entity2Keys[i];
if (entity1Key == null)
{
if (entity2Key == null)
@ -89,13 +89,13 @@ namespace Volo.Abp.Domain.Entities
//entity2Key is not null!
return false;
}
if (entity2Key == null)
{
//entity1Key was not null!
return false;
}
if (TypeHelper.IsDefaultValue(entity1Key) && TypeHelper.IsDefaultValue(entity2Key))
{
return false;
@ -266,5 +266,36 @@ namespace Volo.Abp.Domain.Entities
property?.SetValue(entity, idFactory());
}
public static void TrySetId<TKey>(
object entity,
TKey id,
bool checkForDisableIdGenerationAttribute = false)
{
var property = CachedIdProperties.GetOrAdd(
$"{entity.GetType().FullName}-{checkForDisableIdGenerationAttribute}", () =>
{
var idProperty = entity
.GetType()
.GetProperties()
.FirstOrDefault(x => x.Name == nameof(IEntity<object>.Id) &&
x.GetSetMethod(true) != null);
if (idProperty == null)
{
return null;
}
if (checkForDisableIdGenerationAttribute &&
idProperty.IsDefined(typeof(DisableIdGenerationAttribute), true))
{
return null;
}
return idProperty;
});
property?.SetValue(entity, id);
}
}
}
}

29
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/EntityJsonConverter.cs

@ -0,0 +1,29 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.Domain.Entities
{
public class EntityJsonConverter<TEntity, TKey> : JsonConverter<TEntity>
{
public override TEntity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);
var entity = (TEntity)JsonSerializer.Deserialize(jsonDocument.RootElement.GetRawText(), typeToConvert);
var idJsonElement = jsonDocument.RootElement.GetProperty(nameof(IEntity<object>.Id));
var id = JsonSerializer.Deserialize<TKey>(idJsonElement.GetRawText());
if (id != null)
{
EntityHelper.TrySetId(entity, id);
}
return entity;
}
public override void Write(Utf8JsonWriter writer, TEntity value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value);
}
}
}

12
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/AbpJsonValueConverter.cs

@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Newtonsoft.Json;
using Volo.Abp.Data;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Volo.Abp.Json.Microsoft.JsonConverters;
namespace Volo.Abp.EntityFrameworkCore.ValueConverters
{
@ -16,12 +16,14 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
private static string SerializeObject(TPropertyType d)
{
return JsonConvert.SerializeObject(d, Formatting.None);
return JsonSerializer.Serialize(d);
}
private static TPropertyType DeserializeObject(string s)
{
return JsonConvert.DeserializeObject<TPropertyType>(s);
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter());
return JsonSerializer.Deserialize<TPropertyType>(s, deserializeOptions);
}
}
}

15
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/ExtraPropertiesValueConverter.cs

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Newtonsoft.Json;
using Volo.Abp.Json.Microsoft.JsonConverters;
using Volo.Abp.ObjectExtending;
namespace Volo.Abp.EntityFrameworkCore.ValueConverters
@ -36,13 +36,14 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
}
}
return JsonConvert.SerializeObject(copyDictionary, Formatting.None);
return JsonSerializer.Serialize(copyDictionary);
}
private static Dictionary<string, object> DeserializeObject(string extraPropertiesAsJson, Type entityType)
{
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(extraPropertiesAsJson);
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter());
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(extraPropertiesAsJson, deserializeOptions) ?? new Dictionary<string, object>();
if (entityType != null)
{
var objectExtension = ObjectExtensionManager.Instance.GetOrNull(entityType);
@ -59,7 +60,7 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
}
private static object GetNormalizedValue(
Dictionary<string, object> dictionary,
Dictionary<string, object> dictionary,
ObjectExtensionPropertyInfo property)
{
var value = dictionary.GetOrDefault(property.Name);
@ -84,4 +85,4 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters
}
}
}
}
}

15
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs

@ -2,9 +2,8 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Threading;
@ -17,11 +16,6 @@ namespace Volo.Abp.Http.Client.DynamicProxying
protected IApiDescriptionCache Cache { get; }
private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
public ApiDescriptionFinder(IApiDescriptionCache cache)
{
Cache = cache;
@ -91,9 +85,10 @@ namespace Volo.Abp.Http.Client.DynamicProxying
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject(
content,
typeof(ApplicationApiDescriptionModel), SharedJsonSerializerSettings);
var result = JsonSerializer.Deserialize<ApplicationApiDescriptionModel>(content, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
return (ApplicationApiDescriptionModel)result;
}

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ActionApiDescriptionModel.cs

@ -26,7 +26,7 @@ namespace Volo.Abp.Http.Modeling
public ReturnValueApiDescriptionModel ReturnValue { get; set; }
private ActionApiDescriptionModel()
public ActionApiDescriptionModel()
{
}
@ -71,4 +71,4 @@ namespace Volo.Abp.Http.Modeling
return $"[ActionApiDescriptionModel {Name}]";
}
}
}
}

6
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ApplicationApiDescriptionModel.cs

@ -12,9 +12,9 @@ namespace Volo.Abp.Http.Modeling
public IDictionary<string, TypeApiDescriptionModel> Types { get; set; }
private ApplicationApiDescriptionModel()
public ApplicationApiDescriptionModel()
{
}
public static ApplicationApiDescriptionModel Create()
@ -56,4 +56,4 @@ namespace Volo.Abp.Http.Modeling
return subModel;
}
}
}
}

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs

@ -16,7 +16,7 @@ namespace Volo.Abp.Http.Modeling
public Dictionary<string, ActionApiDescriptionModel> Actions { get; set; }
private ControllerApiDescriptionModel()
public ControllerApiDescriptionModel()
{
}
@ -79,4 +79,4 @@ namespace Volo.Abp.Http.Modeling
return $"[ControllerApiDescriptionModel {ControllerName}]";
}
}
}
}

6
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerInterfaceApiDescriptionModel.cs

@ -7,9 +7,9 @@ namespace Volo.Abp.Http.Modeling
{
public string Type { get; set; }
private ControllerInterfaceApiDescriptionModel()
public ControllerInterfaceApiDescriptionModel()
{
}
public static ControllerInterfaceApiDescriptionModel Create(Type type)
@ -20,4 +20,4 @@ namespace Volo.Abp.Http.Modeling
};
}
}
}
}

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs

@ -19,7 +19,7 @@ namespace Volo.Abp.Http.Modeling
public object DefaultValue { get; set; }
private MethodParameterApiDescriptionModel()
public MethodParameterApiDescriptionModel()
{
}

14
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModuleApiDescriptionModel.cs

@ -11,22 +11,22 @@ namespace Volo.Abp.Http.Modeling
/// <summary>
/// "app".
/// </summary>
public const string DefaultRootPath = "app";
public const string DefaultRootPath = "app";
/// <summary>
/// "Default".
/// </summary>
public const string DefaultRemoteServiceName = "Default";
public const string DefaultRemoteServiceName = "Default";
public string RootPath { get; set; }
public string RemoteServiceName { get; set; }
public IDictionary<string, ControllerApiDescriptionModel> Controllers { get; set; }
private ModuleApiDescriptionModel()
public ModuleApiDescriptionModel()
{
}
public static ModuleApiDescriptionModel Create(string rootPath, string remoteServiceName)
@ -53,7 +53,7 @@ namespace Volo.Abp.Http.Modeling
{
return Controllers.GetOrAdd(uniqueName, () => ControllerApiDescriptionModel.Create(name, type, ignoredInterfaces));
}
public ModuleApiDescriptionModel CreateSubModel(string[] controllers, string[] actions)
{
var subModel = Create(RootPath, RemoteServiceName);
@ -69,4 +69,4 @@ namespace Volo.Abp.Http.Modeling
return subModel;
}
}
}
}

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs

@ -24,7 +24,7 @@ namespace Volo.Abp.Http.Modeling
public string DescriptorName { get; set; }
private ParameterApiDescriptionModel()
public ParameterApiDescriptionModel()
{
}

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ReturnValueApiDescriptionModel.cs

@ -11,7 +11,7 @@ namespace Volo.Abp.Http.Modeling
public string TypeSimple { get; set; }
private ReturnValueApiDescriptionModel()
public ReturnValueApiDescriptionModel()
{
}

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/TypeApiDescriptionModel.cs

@ -19,7 +19,7 @@ namespace Volo.Abp.Http.Modeling
public PropertyApiDescriptionModel[] Properties { get; set; }
private TypeApiDescriptionModel()
public TypeApiDescriptionModel()
{
}

3
framework/src/Volo.Abp.Json.Newtonsoft/FodyWeavers.xml

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>

30
framework/src/Volo.Abp.Json.Newtonsoft/FodyWeavers.xsd

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" />
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

25
framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\configureawait.props" />
<Import Project="..\..\..\common.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Volo.Abp.Json.Newtonsoft</AssemblyName>
<PackageId>Volo.Abp.Json.Newtonsoft</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
</ItemGroup>
</Project>

21
framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/AbpJsonModule.cs

@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.Microsoft;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Modularity;
namespace Volo.Abp.Json
{
[DependsOn(typeof(AbpJsonModule))]
public class AbpJsonNewtonsoftModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<AbpJsonIsoDateTimeConverter>();
});
}
}
}

0
framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs → framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs

0
framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerOptions.cs → framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerOptions.cs

1
framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs → framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs

@ -9,6 +9,7 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Json.Newtonsoft
{
[Dependency(ReplaceServices = true)]
public class NewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
{
private static readonly CamelCaseExceptDictionaryKeysResolver SharedCamelCaseExceptDictionaryKeysResolver =

4
framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj

@ -14,10 +14,6 @@
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Timing\Volo.Abp.Timing.csproj" />
</ItemGroup>

10
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs

@ -1,4 +1,7 @@
using Volo.Abp.Json.Newtonsoft;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.Microsoft;
using Volo.Abp.Modularity;
using Volo.Abp.Timing;
@ -9,10 +12,7 @@ namespace Volo.Abp.Json
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<AbpJsonIsoDateTimeConverter>();
});
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AbpJsonSerializerOptions>, AbpJsonSerializerOptionsSetup>());
}
}
}

2
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonOptions.cs

@ -7,4 +7,4 @@
/// </summary>
public string DefaultDateTimeFormat { get; set; }
}
}
}

49
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializer.cs

@ -0,0 +1,49 @@
using System;
using System.Text.Json;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Json.Microsoft
{
public class AbpJsonSerializer : IJsonSerializer, ITransientDependency
{
protected AbpJsonSerializerOptions Options { get; }
public AbpJsonSerializer(IOptions<AbpJsonSerializerOptions> options)
{
Options = options.Value;
}
public string Serialize(object obj, bool camelCase = true, bool indented = false)
{
return JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented));
}
public T Deserialize<T>(string jsonString, bool camelCase = true)
{
return JsonSerializer.Deserialize<T>(jsonString, CreateJsonSerializerOptions(camelCase));
}
public object Deserialize(Type type, string jsonString, bool camelCase = true)
{
return JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase));
}
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
{
var settings = new JsonSerializerOptions(Options.JsonSerializerOptions);
if (camelCase)
{
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}
if (indented)
{
settings.WriteIndented = true;
}
return settings;
}
}
}

18
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializerOptions.cs

@ -0,0 +1,18 @@
using System.Text.Json;
namespace Volo.Abp.Json.Microsoft
{
public class AbpJsonSerializerOptions
{
public JsonSerializerOptions JsonSerializerOptions { get; }
public AbpJsonSerializerOptions()
{
//TODO:Defaults?
//https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/JsonOptions.cs#L18
//https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs
JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
}
}
}

23
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/AbpJsonSerializerOptionsSetup.cs

@ -0,0 +1,23 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Json.Microsoft.JsonConverters;
namespace Volo.Abp.Json.Microsoft
{
public class AbpJsonSerializerOptionsSetup : IConfigureOptions<AbpJsonSerializerOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpJsonSerializerOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(AbpJsonSerializerOptions options)
{
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
}
}
}

38
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/AbpDateTimeConverter.cs

@ -0,0 +1,38 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Timing;
namespace Volo.Abp.Json.Microsoft.JsonConverters
{
public class AbpDateTimeConverter : JsonConverter<DateTime>, ITransientDependency
{
private readonly IClock _clock;
private readonly AbpJsonOptions _options;
public AbpDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> abpJsonOptions)
{
_clock = clock;
_options = abpJsonOptions.Value;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return _clock.Normalize(reader.GetDateTime());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
if (_options.DefaultDateTimeFormat.IsNullOrWhiteSpace())
{
writer.WriteStringValue(_clock.Normalize(value));
}
else
{
writer.WriteStringValue(_clock.Normalize(value).ToString(_options.DefaultDateTimeFormat));
}
}
}
}

50
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/AbpNullableDateTimeConverter.cs

@ -0,0 +1,50 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Timing;
namespace Volo.Abp.Json.Microsoft.JsonConverters
{
public class AbpNullableDateTimeConverter : JsonConverter<DateTime?>, ITransientDependency
{
private readonly IClock _clock;
private readonly AbpJsonOptions _options;
public AbpNullableDateTimeConverter(IClock clock, IOptions<AbpJsonOptions> abpJsonOptions)
{
_clock = clock;
_options = abpJsonOptions.Value;
}
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TryGetDateTime(out var dateTime))
{
return _clock.Normalize(dateTime);
}
return null;
}
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
if (_options.DefaultDateTimeFormat.IsNullOrWhiteSpace())
{
writer.WriteStringValue(_clock.Normalize(value.Value));
}
else
{
writer.WriteStringValue(_clock.Normalize(value.Value).ToString(_options.DefaultDateTimeFormat));
}
}
}
}
}

54
framework/src/Volo.Abp.Json/Volo/Abp/Json/Microsoft/JsonConverters/ObjectToInferredTypesConverter.cs

@ -0,0 +1,54 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.Json.Microsoft.JsonConverters
{
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.True)
{
return true;
}
if (reader.TokenType == JsonTokenType.False)
{
return false;
}
if (reader.TokenType == JsonTokenType.Number)
{
if (reader.TryGetInt64(out var l))
{
return l;
}
return reader.GetDouble();
}
if (reader.TokenType == JsonTokenType.String)
{
if (reader.TryGetDateTime(out var datetime))
{
return datetime;
}
return reader.GetString();
}
// Use JsonElement as fallback.
// Newtonsoft uses JArray or JObject.
using (var document = JsonDocument.ParseValue(ref reader))
{
return document.RootElement.Clone();
}
}
public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options)
{
throw new InvalidOperationException("Should not get here.");
}
}
}

4
framework/src/Volo.Abp.Localization/Volo.Abp.Localization.csproj

@ -19,10 +19,6 @@
<EmbeddedResource Include="Volo\Abp\Localization\Resources\**\*.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Localization.Abstractions\Volo.Abp.Localization.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Settings\Volo.Abp.Settings.csproj" />

19
framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs

@ -1,19 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Volo.Abp.Localization.Json
{
public static class JsonLocalizationDictionaryBuilder
{
private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
/// <summary>
/// Builds an <see cref="JsonLocalizationDictionaryBuilder" /> from given file.
/// </summary>
@ -39,8 +33,13 @@ namespace Volo.Abp.Localization.Json
JsonLocalizationFile jsonFile;
try
{
jsonFile = JsonConvert.DeserializeObject<JsonLocalizationFile>(
jsonString, SharedJsonSerializerSettings);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
};
jsonFile = JsonSerializer.Deserialize<JsonLocalizationFile>(jsonString, options);
}
catch (JsonException ex)
{
@ -80,4 +79,4 @@ namespace Volo.Abp.Localization.Json
return new StaticLocalizationDictionary(cultureCode, dictionary);
}
}
}
}

4
framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationFile.cs

@ -9,11 +9,11 @@ namespace Volo.Abp.Localization.Json
/// </summary>
public string Culture { get; set; }
public Dictionary<string, string> Texts { get; }
public Dictionary<string, string> Texts { get; set; }
public JsonLocalizationFile()
{
Texts = new Dictionary<string, string>();
}
}
}
}

41
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs

@ -1,52 +1,27 @@
using System;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Json;
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
public class Utf8JsonMemoryDbSerializer : IMemoryDbSerializer, ITransientDependency
{
private static readonly JsonSerializerSettings MemoryDbSerializerSettings;
protected Utf8JsonMemoryDbSerializerOptions Options { get; }
static Utf8JsonMemoryDbSerializer()
public Utf8JsonMemoryDbSerializer(IOptions<Utf8JsonMemoryDbSerializerOptions> options)
{
MemoryDbSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new ResolverWithPrivateSetters(),
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
Options = options.Value;
}
byte[] IMemoryDbSerializer.Serialize(object obj)
{
var jsonString = JsonConvert.SerializeObject(obj, MemoryDbSerializerSettings);
return Encoding.UTF8.GetBytes(jsonString);
return JsonSerializer.SerializeToUtf8Bytes(obj, Options.JsonSerializerOptions);
}
public object Deserialize(byte[] value, Type type)
{
var jsonString = Encoding.UTF8.GetString(value);
return JsonConvert.DeserializeObject(jsonString, type, MemoryDbSerializerSettings);
}
public class ResolverWithPrivateSetters : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var prop = base.CreateProperty(member, memberSerialization);
if (prop.Writable)
{
return prop;
}
prop.Writable = member.As<PropertyInfo>()?.GetSetMethod(true) != null;
return prop;
}
return JsonSerializer.Deserialize(value, type, Options.JsonSerializerOptions);
}
}
}
}

14
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializerOptions.cs

@ -0,0 +1,14 @@
using System.Text.Json;
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
public class Utf8JsonMemoryDbSerializerOptions
{
public JsonSerializerOptions JsonSerializerOptions { get; }
public Utf8JsonMemoryDbSerializerOptions()
{
JsonSerializerOptions = new JsonSerializerOptions();
}
}
}

1
framework/test/Volo.Abp.AspNetCore.Authentication.OAuth.Tests/Volo/Abp/AspNetCore/Authentication/OAuth/Claims/MultipleClaimAction_Tests.cs

@ -1,7 +1,6 @@
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using Newtonsoft.Json.Linq;
using Shouldly;
using Volo.Abp.Security.Claims;
using Xunit;

14
framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs

@ -1,10 +1,8 @@
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Shouldly;
using Volo.Abp.AspNetCore.TestBase;
@ -18,16 +16,10 @@ namespace Volo.Abp.AspNetCore
public abstract class AbpAspNetCoreTestBase<TStartup> : AbpAspNetCoreIntegratedTestBase<TStartup>
where TStartup : class
{
private static readonly JsonSerializerSettings SharedJsonSerializerSettings =
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
protected virtual async Task<T> GetResponseAsObjectAsync<T>(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
{
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
return JsonConvert.DeserializeObject<T>(strResponse, SharedJsonSerializerSettings);
return JsonSerializer.Deserialize<T>(strResponse, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
@ -49,4 +41,4 @@ namespace Volo.Abp.AspNetCore
}
}
}
}
}

11
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs

@ -4,14 +4,16 @@ using Volo.Abp.Modularity;
using Volo.Abp.TestApp.MemoryDb;
using Volo.Abp.Data;
using Volo.Abp.Autofac;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.MemoryDb;
using Volo.Abp.TestApp;
using Volo.Abp.TestApp.Domain;
namespace Volo.Abp.MemoryDb
{
[DependsOn(
typeof(TestAppModule),
typeof(AbpMemoryDbModule),
typeof(TestAppModule),
typeof(AbpMemoryDbModule),
typeof(AbpAutofacModule))]
public class AbpMemoryDbTestModule : AbpModule
{
@ -29,6 +31,11 @@ namespace Volo.Abp.MemoryDb
options.AddDefaultRepositories();
options.AddRepository<City, CityRepository>();
});
Configure<Utf8JsonMemoryDbSerializerOptions>(options =>
{
options.JsonSerializerOptions.Converters.Add(new EntityJsonConverter<EntityWithIntPk, int>());
});
}
}
}

1
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests_With_Int_Pk.cs

@ -1,4 +1,5 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Domain.Repositories;

4
modules/docs/src/Volo.Docs.Domain.Shared/Volo.Docs.Domain.Shared.csproj

@ -13,5 +13,9 @@
<ItemGroup>
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Localization\Volo.Abp.Localization.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

11
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs

@ -1,5 +1,7 @@
using Volo.Abp.Application;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.FeatureManagement.JsonConverters;
using Volo.Abp.Json;
using Volo.Abp.Json.Microsoft;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
@ -7,7 +9,8 @@ namespace Volo.Abp.FeatureManagement
{
[DependsOn(
typeof(AbpFeatureManagementDomainSharedModule),
typeof(AbpDddApplicationModule)
typeof(AbpDddApplicationModule),
typeof(AbpJsonModule)
)]
public class AbpFeatureManagementApplicationContractsModule : AbpModule
{
@ -18,9 +21,9 @@ namespace Volo.Abp.FeatureManagement
options.FileSets.AddEmbedded<AbpFeatureManagementApplicationContractsModule>();
});
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
Configure<AbpJsonSerializerOptions>(options =>
{
options.Converters.Add<StringValueTypeJsonConverter>();
options.JsonSerializerOptions.Converters.Add(new StringValueTypeJsonConverter());
});
}
}

24
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/LocalizableStringInfoJsonConverter.cs

@ -0,0 +1,24 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class LocalizableStringInfoJsonConverter : JsonConverter<LocalizableStringInfo>
{
public override LocalizableStringInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
return new LocalizableStringInfo(
rootElement.GetProperty("ResourceName").GetString(),
rootElement.GetProperty("Name").GetString()
);
}
public override void Write(Utf8JsonWriter writer, LocalizableStringInfo value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value);
}
}
}

36
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemJsonConverter.cs

@ -0,0 +1,36 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class SelectionStringValueItemJsonConverter : JsonConverter<ISelectionStringValueItem>
{
public override ISelectionStringValueItem Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new LocalizableStringInfoJsonConverter());
return new LocalizableSelectionStringValueItem
{
Value = rootElement.GetProperty("Value").GetString(),
DisplayText = JsonSerializer.Deserialize<LocalizableStringInfo>(rootElement.GetProperty("DisplayText").GetRawText(), jsonSerializerOptions)
};
}
public override void Write(Utf8JsonWriter writer, ISelectionStringValueItem value, JsonSerializerOptions options)
{
if (value.GetType() == typeof(LocalizableStringInfo))
{
JsonSerializer.Serialize(writer, (LocalizableSelectionStringValueItem)value);
}
else
{
throw new JsonException("Unknown ISelectionStringValueItem type!");
}
}
}
}

36
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemSourceJsonConverter.cs

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class SelectionStringValueItemSourceJsonConverter : JsonConverter<ISelectionStringValueItemSource>
{
public override ISelectionStringValueItemSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new SelectionStringValueItemJsonConverter());
var selectionStringValueItem =
JsonSerializer.Deserialize<List<LocalizableSelectionStringValueItem>>(rootElement.GetProperty("Items").GetRawText(), jsonSerializerOptions) ??
new List<LocalizableSelectionStringValueItem>();
return new StaticSelectionStringValueItemSource(selectionStringValueItem.ToArray());
}
public override void Write(Utf8JsonWriter writer, ISelectionStringValueItemSource value, JsonSerializerOptions options)
{
if (value.GetType() == typeof(StaticSelectionStringValueItemSource))
{
JsonSerializer.Serialize(writer, (StaticSelectionStringValueItemSource)value);
}
else
{
throw new JsonException("Unknown ISelectionStringValueItemSource type!");
}
}
}
}

47
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/StringValueTypeJsonConverter.cs

@ -0,0 +1,47 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class StringValueTypeJsonConverter : JsonConverter<IStringValueType>
{
public override IStringValueType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new ValueValidatorJsonConverter());
jsonSerializerOptions.Converters.Add(new SelectionStringValueItemSourceJsonConverter());
var name = rootElement.GetProperty("Name").GetString();
return name switch
{
"SelectionStringValueType" => JsonSerializer.Deserialize<SelectionStringValueType>(rootElement.GetRawText(), jsonSerializerOptions),
"FreeTextStringValueType" => JsonSerializer.Deserialize<FreeTextStringValueType>(rootElement.GetRawText(), jsonSerializerOptions),
"ToggleStringValueType" => JsonSerializer.Deserialize<ToggleStringValueType>(rootElement.GetRawText(), jsonSerializerOptions),
_ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!")
};
}
public override void Write(Utf8JsonWriter writer, IStringValueType value, JsonSerializerOptions options)
{
if (value.GetType() == typeof(FreeTextStringValueType))
{
JsonSerializer.Serialize(writer, (FreeTextStringValueType)value);
}
else if (value.GetType() == typeof(SelectionStringValueType))
{
JsonSerializer.Serialize(writer, (SelectionStringValueType)value);
}
else if (value.GetType() == typeof(ToggleStringValueType))
{
JsonSerializer.Serialize(writer, (ToggleStringValueType)value);
}
else
{
throw new JsonException("Unknown IStringValueType type!");
}
}
}
}

68
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/ValueValidatorJsonConverter.cs

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Json.Microsoft.JsonConverters;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class ValueValidatorJsonConverter : JsonConverter<IValueValidator>
{
public override IValueValidator Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var valueValidator = CreateValueValidatorByName(rootElement.GetProperty("Name").GetString());
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter());
var properties = JsonSerializer.Deserialize<IDictionary<string, object>>(rootElement.GetProperty("Properties").GetRawText(), deserializeOptions);
if (properties != null && properties.Any())
{
foreach (var property in properties)
{
valueValidator.Properties[property.Key] = property.Value;
}
}
return valueValidator;
}
public override void Write(Utf8JsonWriter writer, IValueValidator value, JsonSerializerOptions options)
{
if (value.GetType() == typeof(AlwaysValidValueValidator))
{
JsonSerializer.Serialize(writer, (AlwaysValidValueValidator)value);
}
else if (value.GetType() == typeof(BooleanValueValidator))
{
JsonSerializer.Serialize(writer, (BooleanValueValidator)value);
}
else if (value.GetType() == typeof(NumericValueValidator))
{
JsonSerializer.Serialize(writer, (NumericValueValidator)value);
}
else if (value.GetType() == typeof(StringValueValidator))
{
JsonSerializer.Serialize(writer, (StringValueValidator)value);
}
else
{
throw new JsonException("Unknown IValueValidator type!");
}
}
protected virtual IValueValidator CreateValueValidatorByName(string name)
{
return name switch
{
"NULL" => new AlwaysValidValueValidator(),
"BOOLEAN" => new BooleanValueValidator(),
"NUMERIC" => new NumericValueValidator(),
"STRING" => new StringValueValidator(),
_ => throw new ArgumentException($"{nameof(IValueValidator)} named {name} was not found!")
};
}
}
}

90
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs

@ -1,90 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement
{
public class StringValueTypeJsonConverter : JsonConverter, ITransientDependency
{
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IStringValueType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException("This method should not be called to write (since CanWrite is false).");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject)
{
return null;
}
var jsonObject = JObject.Load(reader);
var stringValue = CreateStringValueTypeByName(jsonObject, jsonObject["name"].ToString());
foreach (var o in serializer.Deserialize<Dictionary<string, object>>(
new JsonTextReader(new StringReader(jsonObject["properties"].ToString()))))
{
stringValue[o.Key] = o.Value;
}
stringValue.Validator = CreateValueValidatorByName(jsonObject["validator"], jsonObject["validator"]["name"].ToString());
foreach (var o in serializer.Deserialize<Dictionary<string, object>>(
new JsonTextReader(new StringReader(jsonObject["validator"]["properties"].ToString()))))
{
stringValue.Validator[o.Key] = o.Value;
}
return stringValue;
}
protected virtual IStringValueType CreateStringValueTypeByName(JObject jObject, string name)
{
if (name == "SelectionStringValueType")
{
var selectionStringValueType = new SelectionStringValueType();
if (jObject["itemSource"].HasValues)
{
selectionStringValueType.ItemSource = new StaticSelectionStringValueItemSource(jObject["itemSource"]["items"]
.Select(item => new LocalizableSelectionStringValueItem()
{
Value = item["value"].ToString(),
DisplayText = new LocalizableStringInfo(item["displayText"]["resourceName"].ToString(), item["displayText"]["name"].ToString())
}).ToArray());
}
return selectionStringValueType;
}
return name switch
{
"FreeTextStringValueType" => new FreeTextStringValueType(),
"ToggleStringValueType" => new ToggleStringValueType(),
_ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!")
};
}
protected virtual IValueValidator CreateValueValidatorByName(JToken jObject, string name)
{
return name switch
{
"NULL" => new AlwaysValidValueValidator(),
"BOOLEAN" => new BooleanValueValidator(),
"NUMERIC" => new NumericValueValidator(),
"STRING" => new StringValueValidator(),
_ => throw new ArgumentException($"{nameof(IValueValidator)} named {name} was not found!")
};
}
}
}

6
modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json;
using Shouldly;
using Volo.Abp.Json;
using Volo.Abp.Validation.StringValues;
@ -66,9 +66,11 @@ namespace Volo.Abp.FeatureManagement
}
};
var serialized = _jsonSerializer.Serialize(featureListDto, indented: true);
var serialized = _jsonSerializer.Serialize(featureListDto);
var featureListDto2 = _jsonSerializer.Deserialize<GetFeatureListResultDto>(serialized);
featureListDto2.ShouldNotBeNull();
featureListDto2.Groups[0].Features[0].ValueType.ShouldBeOfType<FreeTextStringValueType>();
featureListDto2.Groups[0].Features[0].ValueType.Validator.ShouldBeOfType<BooleanValueValidator>();

1
nupkg/common.ps1

@ -109,6 +109,7 @@ $projects = (
"framework/src/Volo.Abp.Http",
"framework/src/Volo.Abp.IdentityModel",
"framework/src/Volo.Abp.Json",
"framework/src/Volo.Abp.Json.Newtonsoft",
"framework/src/Volo.Abp.Ldap",
"framework/src/Volo.Abp.Localization.Abstractions",
"framework/src/Volo.Abp.MailKit",

Loading…
Cancel
Save