Browse Source

Set `PropertyNamingPolicy` to `null` to keep original property name and dictionary key.

pull/22656/head
maliming 2 years ago
parent
commit
5d1c9851e1
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 17
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs
  2. 52
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpNewtonsoftSerializerProviderTests.cs
  3. 37
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_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,
indented,
Options.JsonSerializerOptions
}, _ =>
}, _ => new JsonSerializerOptions(Options.JsonSerializerOptions)
{
var settings = new JsonSerializerOptions(Options.JsonSerializerOptions);
if (camelCase)
{
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}
if (indented)
{
settings.WriteIndented = true;
}
return settings;
PropertyNamingPolicy = camelCase ? JsonNamingPolicy.CamelCase : null,
WriteIndented = indented
});
}
}

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 Volo.Abp.Json.Newtonsoft;
using Xunit;
namespace Volo.Abp.Json;
public class AbpNewtonsoftSerializerProviderTests : AbpJsonNewtonsoftJsonTestBase
{
protected AbpNewtonsoftJsonSerializer JsonSerializer;
public AbpNewtonsoftSerializerProviderTests()
{
JsonSerializer = GetRequiredService<AbpNewtonsoftJsonSerializer>();
}
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}}}");
}
}

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

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Data;
@ -23,6 +25,13 @@ public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSyste
public string Name { get; set; }
}
public class File
{
public string FileName { get; set; }
public Dictionary<string, int> ExtraProperties { get; set; }
}
public class FileWithBoolean
{
public string Name { get; set; }
@ -74,6 +83,34 @@ public abstract class AbpSystemTextJsonSerializerProviderTestBase : AbpJsonSyste
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]
public void Serialize_Deserialize_With_Boolean()
{

Loading…
Cancel
Save