Browse Source

fix(cap): 修复某些情况下事件数据序列化与abp不一致的问题

pull/383/head
cKey 4 years ago
parent
commit
2701c593ea
  1. 167
      aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs

167
aspnet-core/modules/common/LINGYUN.Abp.EventBus.CAP/LINGYUN/Abp/EventBus/CAP/AbpCapSerializer.cs

@ -1,78 +1,89 @@
using DotNetCore.CAP.Messages; using DotNetCore.CAP.Messages;
using DotNetCore.CAP.Serialization; using DotNetCore.CAP.Serialization;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Buffers; using System.Buffers;
using System.Text.Json; using System.Text;
using System.Threading.Tasks; using System.Text.Json;
using Volo.Abp.Json.SystemTextJson; using System.Threading.Tasks;
using Volo.Abp.Json;
namespace LINGYUN.Abp.EventBus.CAP using Volo.Abp.Json.SystemTextJson;
{
public class AbpCapSerializer : ISerializer namespace LINGYUN.Abp.EventBus.CAP
{ {
private readonly AbpSystemTextJsonSerializerOptions _jsonSerializerOptions; public class AbpCapSerializer : ISerializer
{
public AbpCapSerializer(IOptions<AbpSystemTextJsonSerializerOptions> options) private readonly IJsonSerializer _jsonSerializer;
{ private readonly AbpSystemTextJsonSerializerOptions _jsonSerializerOptions;
_jsonSerializerOptions = options.Value;
} public AbpCapSerializer(
IJsonSerializer jsonSerializer,
public Task<TransportMessage> SerializeAsync(Message message) IOptions<AbpSystemTextJsonSerializerOptions> options)
{ {
if (message == null) _jsonSerializer = jsonSerializer;
{ _jsonSerializerOptions = options.Value;
throw new ArgumentNullException(nameof(message)); }
}
public Task<TransportMessage> SerializeAsync(Message message)
if (message.Value == null) {
{ if (message == null)
return Task.FromResult(new TransportMessage(message.Headers, null)); {
} throw new ArgumentNullException(nameof(message));
}
var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(message.Value, _jsonSerializerOptions.JsonSerializerOptions);
return Task.FromResult(new TransportMessage(message.Headers, jsonBytes)); if (message.Value == null)
} {
return Task.FromResult(new TransportMessage(message.Headers, null));
public Task<Message> DeserializeAsync(TransportMessage transportMessage, Type valueType) }
{ var messageStr = _jsonSerializer.Serialize(message.Value);
if (valueType == null || transportMessage.Body == null) var jsonBytes = Encoding.UTF8.GetBytes(messageStr);
{ // var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(message.Value, _jsonSerializerOptions.JsonSerializerOptions);
return Task.FromResult(new Message(transportMessage.Headers, null)); return Task.FromResult(new TransportMessage(message.Headers, jsonBytes));
} }
var obj = JsonSerializer.Deserialize(transportMessage.Body, valueType, _jsonSerializerOptions.JsonSerializerOptions); public Task<Message> DeserializeAsync(TransportMessage transportMessage, Type valueType)
{
return Task.FromResult(new Message(transportMessage.Headers, obj)); if (valueType == null || transportMessage.Body == null)
} {
return Task.FromResult(new Message(transportMessage.Headers, null));
public string Serialize(Message message) }
{
return JsonSerializer.Serialize(message, _jsonSerializerOptions.JsonSerializerOptions); var messageBytes = Encoding.UTF8.GetString(transportMessage.Body);
} var obj = _jsonSerializer.Deserialize(valueType, messageBytes);
// var obj = JsonSerializer.Deserialize(transportMessage.Body, valueType, _jsonSerializerOptions.JsonSerializerOptions);
public Message Deserialize(string json)
{ return Task.FromResult(new Message(transportMessage.Headers, obj));
return JsonSerializer.Deserialize<Message>(json, _jsonSerializerOptions.JsonSerializerOptions); }
}
public string Serialize(Message message)
public object Deserialize(object value, Type valueType) {
{ return _jsonSerializer.Serialize(message);
if (value is JsonElement jToken) // return JsonSerializer.Serialize(message, _jsonSerializerOptions.JsonSerializerOptions);
{ }
var bufferWriter = new ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter)) public Message Deserialize(string json)
{ {
jToken.WriteTo(writer); return _jsonSerializer.Deserialize<Message>(json);
} // return JsonSerializer.Deserialize<Message>(json, _jsonSerializerOptions.JsonSerializerOptions);
return JsonSerializer.Deserialize(bufferWriter.WrittenSpan, valueType, _jsonSerializerOptions.JsonSerializerOptions); }
}
throw new NotSupportedException("Type is not of type JToken"); public object Deserialize(object value, Type valueType)
} {
if (value is JsonElement jToken)
public bool IsJsonType(object jsonObject) {
{ var bufferWriter = new ArrayBufferWriter<byte>();
return jsonObject is JsonElement; using (var writer = new Utf8JsonWriter(bufferWriter))
} {
} jToken.WriteTo(writer);
} }
return JsonSerializer.Deserialize(bufferWriter.WrittenSpan, valueType, _jsonSerializerOptions.JsonSerializerOptions);
}
throw new NotSupportedException("Type is not of type JToken");
}
public bool IsJsonType(object jsonObject)
{
return jsonObject is JsonElement;
}
}
}

Loading…
Cancel
Save