Browse Source

Merge pull request #22669 from abpframework/auto-merge/rel-9-2/3648

Merge branch dev with rel-9.2
pull/22677/head
maliming 10 months ago
committed by GitHub
parent
commit
8eeed7ed4f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 17
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs
  2. 17
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpJsonTestBase.cs
  3. 52
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpNewtonsoftSerializerProviderTests.cs
  4. 47
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs
  5. 5
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/InputAndOutputDateTimeFormat_Tests.cs

17
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs

@ -40,21 +40,10 @@ public class AbpSystemTextJsonSerializer : IJsonSerializer, ITransientDependency
camelCase, camelCase,
indented, indented,
Options.JsonSerializerOptions Options.JsonSerializerOptions
}, _ => }, _ => new JsonSerializerOptions(Options.JsonSerializerOptions)
{ {
var settings = new JsonSerializerOptions(Options.JsonSerializerOptions); PropertyNamingPolicy = camelCase ? JsonNamingPolicy.CamelCase : null,
WriteIndented = indented
if (camelCase)
{
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}
if (indented)
{
settings.WriteIndented = true;
}
return settings;
}); });
} }
} }

17
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpJsonTestBase.cs

@ -1,4 +1,9 @@
using Volo.Abp.Testing; using System;
using System.Collections.Concurrent;
using System.Reflection;
using Newtonsoft.Json;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Testing;
namespace Volo.Abp.Json; namespace Volo.Abp.Json;
@ -12,6 +17,16 @@ public abstract class AbpJsonSystemTextJsonTestBase : AbpIntegratedTest<AbpJsonS
public abstract class AbpJsonNewtonsoftJsonTestBase : AbpIntegratedTest<AbpJsonNewtonsoftTestModule> public abstract class AbpJsonNewtonsoftJsonTestBase : AbpIntegratedTest<AbpJsonNewtonsoftTestModule>
{ {
protected AbpJsonNewtonsoftJsonTestBase()
{
var cache = typeof(AbpNewtonsoftJsonSerializer).GetField("JsonSerializerOptionsCache", BindingFlags.NonPublic | BindingFlags.Static);
if (cache != null)
{
var cacheValue = cache.GetValue(null)?.As<ConcurrentDictionary<object, JsonSerializerSettings>>();
cacheValue?.Clear();
}
}
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{ {
options.UseAutofac(); options.UseAutofac();

52
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpNewtonsoftSerializerProviderTests.cs

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using Shouldly;
using Xunit;
namespace Volo.Abp.Json;
[Collection("AbpJsonNewtonsoftJsonTest")]
public class AbpNewtonsoftSerializerProviderTests : AbpJsonNewtonsoftJsonTestBase
{
protected IJsonSerializer JsonSerializer;
public AbpNewtonsoftSerializerProviderTests()
{
JsonSerializer = GetRequiredService<IJsonSerializer>();
}
public class File
{
public string FileName { get; set; }
public Dictionary<string, int> ExtraProperties { get; set; }
}
[Fact]
public void Serialize_Deserialize_Test()
{
var defaultIndent = " "; // Default indent is 2 spaces
var newLine = Environment.NewLine;
var file = new File()
{
FileName = "abp",
ExtraProperties = new Dictionary<string, int>()
{
{ "One", 1 },
{ "Two", 2 }
}
};
var json = JsonSerializer.Serialize(file, camelCase: true);
json.ShouldBe("{\"fileName\":\"abp\",\"extraProperties\":{\"One\":1,\"Two\":2}}");
json = JsonSerializer.Serialize(file, camelCase: true, indented: true);
json.ShouldBe($"{{{newLine}{defaultIndent}\"fileName\": \"abp\",{newLine}{defaultIndent}\"extraProperties\": {{{newLine}{defaultIndent}{defaultIndent}\"One\": 1,{newLine}{defaultIndent}{defaultIndent}\"Two\": 2{newLine}{defaultIndent}}}{newLine}}}");
json = JsonSerializer.Serialize(file, camelCase: false);
json.ShouldBe("{\"FileName\":\"abp\",\"ExtraProperties\":{\"One\":1,\"Two\":2}}");
json = JsonSerializer.Serialize(file, camelCase: false, indented: true);
json.ShouldBe($"{{{newLine}{defaultIndent}\"FileName\": \"abp\",{newLine}{defaultIndent}\"ExtraProperties\": {{{newLine}{defaultIndent}{defaultIndent}\"One\": 1,{newLine}{defaultIndent}{defaultIndent}\"Two\": 2{newLine}{defaultIndent}}}{newLine}}}");
}
}

47
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Shouldly; using Shouldly;
using Volo.Abp.Data; using Volo.Abp.Data;
@ -11,11 +13,11 @@ namespace Volo.Abp.Json;
public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSystemTextJsonTestBase public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSystemTextJsonTestBase
{ {
protected AbpSystemTextJsonSerializer JsonSerializer; protected IJsonSerializer JsonSerializer;
public AbpSystemTextJsonSerializerProviderTestBase() public AbpSystemTextJsonSerializerProviderTestBase()
{ {
JsonSerializer = GetRequiredService<AbpSystemTextJsonSerializer>(); JsonSerializer = GetRequiredService<IJsonSerializer>();
} }
public class TestExtensibleObjectClass : ExtensibleObject public class TestExtensibleObjectClass : ExtensibleObject
@ -23,6 +25,13 @@ public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSyste
public string Name { get; set; } public string Name { get; set; }
} }
public class File
{
public string FileName { get; set; }
public Dictionary<string, int> ExtraProperties { get; set; }
}
public class FileWithBoolean public class FileWithBoolean
{ {
public string Name { get; set; } public string Name { get; set; }
@ -74,6 +83,34 @@ public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSyste
public class AbpSystemTextJsonSerializerProviderTests : AbpSystemTextJsonSerializerProviderTestBase public class AbpSystemTextJsonSerializerProviderTests : AbpSystemTextJsonSerializerProviderTestBase
{ {
[Fact]
public void Serialize_Deserialize_Test()
{
var defaultIndent = " "; // Default indent is 2 spaces
var newLine = Environment.NewLine;
var file = new File()
{
FileName = "abp",
ExtraProperties = new Dictionary<string, int>()
{
{ "One", 1 },
{ "Two", 2 }
}
};
var json = JsonSerializer.Serialize(file, camelCase: true);
json.ShouldBe("{\"fileName\":\"abp\",\"extraProperties\":{\"One\":1,\"Two\":2}}");
json = JsonSerializer.Serialize(file, camelCase: true, indented: true);
json.ShouldBe($"{{{newLine}{defaultIndent}\"fileName\": \"abp\",{newLine}{defaultIndent}\"extraProperties\": {{{newLine}{defaultIndent}{defaultIndent}\"One\": 1,{newLine}{defaultIndent}{defaultIndent}\"Two\": 2{newLine}{defaultIndent}}}{newLine}}}");
json = JsonSerializer.Serialize(file, camelCase: false);
json.ShouldBe("{\"FileName\":\"abp\",\"ExtraProperties\":{\"One\":1,\"Two\":2}}");
json = JsonSerializer.Serialize(file, camelCase: false, indented: true);
json.ShouldBe($"{{{newLine}{defaultIndent}\"FileName\": \"abp\",{newLine}{defaultIndent}\"ExtraProperties\": {{{newLine}{defaultIndent}{defaultIndent}\"One\": 1,{newLine}{defaultIndent}{defaultIndent}\"Two\": 2{newLine}{defaultIndent}}}{newLine}}}");
}
[Fact] [Fact]
public void Serialize_Deserialize_With_Boolean() public void Serialize_Deserialize_With_Boolean()
{ {
@ -223,6 +260,8 @@ public class AbpSystemTextJsonSerializerProviderDateTimeFormatTests : AbpSystemT
options.InputDateTimeFormats.Add("yyyy*MM*dd"); options.InputDateTimeFormats.Add("yyyy*MM*dd");
options.OutputDateTimeFormat = "yyyy*MM*dd HH*mm*ss"; options.OutputDateTimeFormat = "yyyy*MM*dd HH*mm*ss";
}); });
base.AfterAddApplication(services);
} }
[Fact] [Fact]
@ -289,6 +328,8 @@ public class AbpSystemTextJsonSerializerProviderDatetimeKindUtcTests : AbpSystem
{ {
Kind = DateTimeKind.Utc; Kind = DateTimeKind.Utc;
services.Configure<AbpClockOptions>(x => x.Kind = Kind); services.Configure<AbpClockOptions>(x => x.Kind = Kind);
base.AfterAddApplication(services);
} }
} }
@ -298,6 +339,8 @@ public class AbpSystemTextJsonSerializerProviderDatetimeKindLocalTests : AbpSyst
{ {
Kind = DateTimeKind.Local; Kind = DateTimeKind.Local;
services.Configure<AbpClockOptions>(x => x.Kind = Kind); services.Configure<AbpClockOptions>(x => x.Kind = Kind);
base.AfterAddApplication(services);
} }
} }

5
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/InputAndOutputDateTimeFormat_Tests.cs

@ -31,6 +31,8 @@ public class InputAndOutputDateTimeFormatSystemTextJsonTests : AbpJsonSystemText
{ {
options.Kind = DateTimeKind.Utc; options.Kind = DateTimeKind.Utc;
}); });
base.AfterAddApplication(services);
} }
[Fact] [Fact]
@ -59,6 +61,7 @@ public class InputAndOutputDateTimeFormatSystemTextJsonTests : AbpJsonSystemText
} }
} }
[Collection("AbpJsonNewtonsoftJsonTest")]
public class InputAndOutputDateTimeFormatNewtonsoftTests : AbpJsonNewtonsoftJsonTestBase public class InputAndOutputDateTimeFormatNewtonsoftTests : AbpJsonNewtonsoftJsonTestBase
{ {
private readonly IJsonSerializer _jsonSerializer; private readonly IJsonSerializer _jsonSerializer;
@ -83,6 +86,8 @@ public class InputAndOutputDateTimeFormatNewtonsoftTests : AbpJsonNewtonsoftJson
{ {
options.Kind = DateTimeKind.Utc; options.Kind = DateTimeKind.Utc;
}); });
base.AfterAddApplication(services);
} }
[Fact] [Fact]

Loading…
Cancel
Save