Browse Source

Merge pull request #6325 from abpframework/maliming/Serialize-Null

Handle the serialized object as null.
pull/6329/head
liangshiwei 6 years ago
committed by GitHub
parent
commit
582d511f54
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs
  2. 3
      framework/src/Volo.Abp.Json/Volo/Abp/Json/IJsonSerializerProvider.cs
  3. 3
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs

15
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpHybridJsonSerializer.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -18,17 +19,19 @@ namespace Volo.Abp.Json
ServiceScopeFactory = serviceScopeFactory; ServiceScopeFactory = serviceScopeFactory;
} }
public string Serialize(object obj, bool camelCase = true, bool indented = false) public string Serialize([CanBeNull]object obj, bool camelCase = true, bool indented = false)
{ {
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
var serializerProvider = GetSerializerProvider(scope.ServiceProvider, obj.GetType()); var serializerProvider = GetSerializerProvider(scope.ServiceProvider, obj?.GetType());
return serializerProvider.Serialize(obj, camelCase, indented); return serializerProvider.Serialize(obj, camelCase, indented);
} }
} }
public T Deserialize<T>(string jsonString, bool camelCase = true) public T Deserialize<T>([NotNull]string jsonString, bool camelCase = true)
{ {
Check.NotNull(jsonString, nameof(jsonString));
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
var serializerProvider = GetSerializerProvider(scope.ServiceProvider, typeof(T)); var serializerProvider = GetSerializerProvider(scope.ServiceProvider, typeof(T));
@ -36,8 +39,10 @@ namespace Volo.Abp.Json
} }
} }
public object Deserialize(Type type, string jsonString, bool camelCase = true) public object Deserialize(Type type, [NotNull]string jsonString, bool camelCase = true)
{ {
Check.NotNull(jsonString, nameof(jsonString));
using (var scope = ServiceScopeFactory.CreateScope()) using (var scope = ServiceScopeFactory.CreateScope())
{ {
var serializerProvider = GetSerializerProvider(scope.ServiceProvider, type); var serializerProvider = GetSerializerProvider(scope.ServiceProvider, type);
@ -45,7 +50,7 @@ namespace Volo.Abp.Json
} }
} }
protected virtual IJsonSerializerProvider GetSerializerProvider(IServiceProvider serviceProvider, Type type) protected virtual IJsonSerializerProvider GetSerializerProvider(IServiceProvider serviceProvider, [CanBeNull]Type type)
{ {
foreach (var providerType in Options.Providers.Reverse()) foreach (var providerType in Options.Providers.Reverse())
{ {

3
framework/src/Volo.Abp.Json/Volo/Abp/Json/IJsonSerializerProvider.cs

@ -1,10 +1,11 @@
using System; using System;
using JetBrains.Annotations;
namespace Volo.Abp.Json namespace Volo.Abp.Json
{ {
public interface IJsonSerializerProvider public interface IJsonSerializerProvider
{ {
bool CanHandle(Type type); bool CanHandle([CanBeNull]Type type);
string Serialize(object obj, bool camelCase = true, bool indented = false); string Serialize(object obj, bool camelCase = true, bool indented = false);

3
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs

@ -1,4 +1,5 @@
using System; using System;
using JetBrains.Annotations;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -13,7 +14,7 @@ namespace Volo.Abp.Json.SystemTextJson
Options = options.Value; Options = options.Value;
} }
public virtual bool Match(Type type) public virtual bool Match([CanBeNull]Type type)
{ {
return Options.UnsupportedTypes.Contains(type); return Options.UnsupportedTypes.Contains(type);
} }

Loading…
Cancel
Save