mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 114 additions and 10 deletions
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpStringToEnumFactory : JsonConverterFactory |
|||
{ |
|||
private readonly JsonNamingPolicy _namingPolicy; |
|||
private readonly bool _allowIntegerValues; |
|||
|
|||
public AbpStringToEnumFactory() |
|||
: this(namingPolicy: null, allowIntegerValues: true) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public AbpStringToEnumFactory(JsonNamingPolicy namingPolicy, bool allowIntegerValues) |
|||
{ |
|||
_namingPolicy = namingPolicy; |
|||
_allowIntegerValues = allowIntegerValues; |
|||
} |
|||
|
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
return typeToConvert.IsEnum; |
|||
} |
|||
|
|||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
return (JsonConverter) Activator.CreateInstance( |
|||
typeof(AbpStringToEnumConverter<>).MakeGenericType(typeToConvert), |
|||
BindingFlags.Instance | BindingFlags.Public, |
|||
binder: null, |
|||
new object[] { _namingPolicy, _allowIntegerValues }, |
|||
culture: null)!; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json |
|||
{ |
|||
public class AbpSystemTextJsonSerializerProvider_Tests : AbpJsonTestBase |
|||
{ |
|||
private readonly AbpSystemTextJsonSerializerProvider _jsonSerializer; |
|||
|
|||
public AbpSystemTextJsonSerializerProvider_Tests() |
|||
{ |
|||
_jsonSerializer = GetRequiredService<AbpSystemTextJsonSerializerProvider>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Serialize_Deserialize_With_Boolean() |
|||
{ |
|||
var json = "{\"name\":\"abp\",\"IsDeleted\":\"fAlSe\"}"; |
|||
var file = _jsonSerializer.Deserialize<FileWithBoolean>(json); |
|||
file.Name.ShouldBe("abp"); |
|||
file.IsDeleted.ShouldBeFalse(); |
|||
|
|||
file.IsDeleted = false; |
|||
var newJson = _jsonSerializer.Serialize(file); |
|||
newJson.ShouldBe("{\"name\":\"abp\",\"isDeleted\":false}"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Serialize_Deserialize_With_Enum() |
|||
{ |
|||
var json = "{\"name\":\"abp\",\"type\":\"Exe\"}"; |
|||
var file = _jsonSerializer.Deserialize<FileWithEnum>(json); |
|||
file.Name.ShouldBe("abp"); |
|||
file.Type.ShouldBe(FileType.Exe); |
|||
|
|||
var newJson = _jsonSerializer.Serialize(file); |
|||
newJson.ShouldBe("{\"name\":\"abp\",\"type\":2}"); |
|||
} |
|||
|
|||
class FileWithBoolean |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsDeleted { get; set; } |
|||
|
|||
} |
|||
|
|||
class FileWithEnum |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public FileType Type { get; set; } |
|||
} |
|||
|
|||
|
|||
enum FileType |
|||
{ |
|||
Zip = 0, |
|||
Exe = 2 |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue