Browse Source

Merge pull request #6146 from abpframework/maliming/AbpStringToEnumFactory

Add AbpStringToEnumFactory and some unit test.
pull/6153/head
liangshiwei 6 years ago
committed by GitHub
parent
commit
5bbdcce559
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs
  2. 2
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs
  3. 15
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpStringToEnumConverter.cs
  4. 40
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpStringToEnumFactory.cs
  5. 2
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs
  6. 63
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs

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

@ -25,7 +25,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Json
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumConverter());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
}
}

2
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs

@ -19,7 +19,7 @@ namespace Volo.Abp.Json.SystemTextJson
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumConverter());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());
}
}

15
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpStringToEnumConverter.cs

@ -1,16 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.Json.SystemTextJson.JsonConverters
{
public class AbpStringToEnumConverter : JsonConverter<object>
public class AbpStringToEnumConverter<T> : JsonConverter<T>
where T : struct, Enum
{
private readonly JsonStringEnumConverter _innerJsonStringEnumConverter;
public AbpStringToEnumConverter()
: this(namingPolicy: null, allowIntegerValues: true)
{
}
public AbpStringToEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true)
@ -23,18 +26,18 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
return typeToConvert.IsEnum;
}
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var newOptions = new JsonSerializerOptions(options);
newOptions.Converters.Remove(this);
newOptions.Converters.RemoveAll(x => x == this || x.GetType() == typeof(AbpStringToEnumFactory));
newOptions.Converters.Add(_innerJsonStringEnumConverter.CreateConverter(typeToConvert, newOptions));
return JsonSerializer.Deserialize(ref reader, typeToConvert, newOptions);
return JsonSerializer.Deserialize<T>(ref reader, newOptions);
}
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var newOptions = new JsonSerializerOptions(options);
newOptions.Converters.Remove(this);
newOptions.Converters.RemoveAll(x => x == this || x.GetType() == typeof(AbpStringToEnumFactory));
JsonSerializer.Serialize(writer, value, newOptions);
}
}

40
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpStringToEnumFactory.cs

@ -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)!;
}
}
}

2
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs

@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Shouldly;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Json.Newtonsoft;

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

@ -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…
Cancel
Save