|
|
|
|
@ -1,5 +1,8 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
using Volo.Abp.Json;
|
|
|
|
|
|
|
|
|
|
@ -7,21 +10,43 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
|
|
|
|
|
{
|
|
|
|
|
public class Utf8JsonMemoryDbSerializer : IMemoryDbSerializer, ITransientDependency
|
|
|
|
|
{
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
private static readonly JsonSerializerSettings MemoryDbSerializerSettings;
|
|
|
|
|
|
|
|
|
|
public Utf8JsonMemoryDbSerializer(IJsonSerializer jsonSerializer)
|
|
|
|
|
static Utf8JsonMemoryDbSerializer()
|
|
|
|
|
{
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
MemoryDbSerializerSettings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ContractResolver = new ResolverWithPrivateSetters(),
|
|
|
|
|
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] IMemoryDbSerializer.Serialize(object obj)
|
|
|
|
|
{
|
|
|
|
|
return Encoding.UTF8.GetBytes(_jsonSerializer.Serialize(obj));
|
|
|
|
|
var jsonString = JsonConvert.SerializeObject(obj, MemoryDbSerializerSettings);
|
|
|
|
|
return Encoding.UTF8.GetBytes(jsonString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Deserialize(byte[] value, Type type)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.Deserialize(type, Encoding.UTF8.GetString(value));
|
|
|
|
|
var jsonString = Encoding.UTF8.GetString(value);
|
|
|
|
|
return JsonConvert.DeserializeObject(jsonString, type, MemoryDbSerializerSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ResolverWithPrivateSetters : DefaultContractResolver
|
|
|
|
|
{
|
|
|
|
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
|
|
|
|
{
|
|
|
|
|
var prop = base.CreateProperty(member, memberSerialization);
|
|
|
|
|
if (prop.Writable)
|
|
|
|
|
{
|
|
|
|
|
return prop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prop.Writable = member.As<PropertyInfo>()?.GetSetMethod(true) != null;
|
|
|
|
|
|
|
|
|
|
return prop;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|