Browse Source

Add AbpHybridJsonSerializer_Tests.

pull/5952/head
maliming 6 years ago
parent
commit
5650f6461d
  1. 5
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs
  2. 4
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs
  3. 14
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs
  4. 2
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs
  5. 125
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs
  6. 2
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSupportTypeMatcher_Tests.cs

5
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs

@ -1,4 +1,5 @@
using System.Text.Json;
using Volo.Abp.Collections;
namespace Volo.Abp.Json.SystemTextJson
{
@ -6,6 +7,8 @@ namespace Volo.Abp.Json.SystemTextJson
{
public JsonSerializerOptions JsonSerializerOptions { get; }
public ITypeList UnsupportedTypes { get; }
public AbpSystemTextJsonSerializerOptions()
{
//TODO:Defaults?
@ -13,6 +16,8 @@ namespace Volo.Abp.Json.SystemTextJson
//https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs
JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
UnsupportedTypes = new TypeList();
}
}
}

4
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs

@ -6,9 +6,9 @@ namespace Volo.Abp.Json.SystemTextJson
{
public class AbpSystemTextJsonSupportTypeMatcher : ITransientDependency
{
protected AbpSystemTextJsonSupportTypeMatcherOptions Options { get; }
protected AbpSystemTextJsonSerializerOptions Options { get; }
public AbpSystemTextJsonSupportTypeMatcher(IOptions<AbpSystemTextJsonSupportTypeMatcherOptions> options)
public AbpSystemTextJsonSupportTypeMatcher(IOptions<AbpSystemTextJsonSerializerOptions> options)
{
Options = options.Value;
}

14
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs

@ -1,14 +0,0 @@
using Volo.Abp.Collections;
namespace Volo.Abp.Json.SystemTextJson
{
public class AbpSystemTextJsonSupportTypeMatcherOptions
{
public ITypeList UnsupportedTypes { get; }
public AbpSystemTextJsonSupportTypeMatcherOptions()
{
UnsupportedTypes = new TypeList();
}
}
}

2
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs

@ -20,7 +20,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ModelBinding
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.Configure<AbpSystemTextJsonSupportTypeMatcherOptions>(options =>
services.Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
options.UnsupportedTypes.Add<GetDateTimeKindModel>();
options.UnsupportedTypes.Add<GetDateTimeKindModel.GetDateTimeKindInnerModel>();

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

@ -0,0 +1,125 @@
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;
using Volo.Abp.Json.SystemTextJson;
using Xunit;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
namespace Volo.Abp.Json
{
public class AbpHybridJsonSerializer_Tests : AbpJsonTestBase
{
private readonly IJsonSerializer _jsonSerializer;
public AbpHybridJsonSerializer_Tests()
{
_jsonSerializer = GetRequiredService<IJsonSerializer>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
options.UnsupportedTypes.Add<MyClass1>();
options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConverter());
});
services.Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<NewtonsoftJsonConverter>();
});
}
[Fact]
public void NewtonsoftSerialize_Test()
{
var json = _jsonSerializer.Serialize(new MyClass1
{
Providers = new List<MyClass3>
{
new MyClass3()
}
});
json.ShouldContain("Newtonsoft");
}
[Fact]
public void SystemTextJsonSerialize_Test()
{
var json = _jsonSerializer.Serialize(new MyClass2
{
Providers = new List<MyClass3>
{
new MyClass3()
}
});
json.ShouldContain("SystemTextJson");
}
public class MyClass1
{
public string Provider { get; set; }
public List<MyClass3> Providers { get; set; }
}
public class MyClass2
{
public string Provider { get; set; }
public List<MyClass3> Providers { get; set; }
}
public class MyClass3
{
public string Provider { get; set; }
}
class NewtonsoftJsonConverter : JsonConverter<MyClass1>, ITransientDependency
{
public override void WriteJson(JsonWriter writer, MyClass1 value, JsonSerializer serializer)
{
value.Provider = "Newtonsoft";
foreach (var provider in value.Providers)
{
provider.Provider = "Newtonsoft";
}
writer.WriteRawValue(JsonConvert.SerializeObject(value));
}
public override MyClass1 ReadJson(JsonReader reader, Type objectType, MyClass1 existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return (MyClass1)serializer.Deserialize(reader, objectType);
}
}
class SystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyClass2>, ITransientDependency
{
public override void Write(Utf8JsonWriter writer, MyClass2 value, JsonSerializerOptions options)
{
value.Provider = "SystemTextJson";
foreach (var provider in value.Providers)
{
provider.Provider = "SystemTextJson";
}
System.Text.Json.JsonSerializer.Serialize(writer, value);
}
public override MyClass2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return (MyClass2)System.Text.Json.JsonSerializer.Deserialize(ref reader, typeToConvert);
}
}
}
}

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

@ -18,7 +18,7 @@ namespace Volo.Abp.Json
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<AbpSystemTextJsonSupportTypeMatcherOptions>(options =>
services.Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
options.UnsupportedTypes.Add<MyClass>();
options.UnsupportedTypes.Add<byte[]>();

Loading…
Cancel
Save