/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit/Serialization/SystemTextJsonObjectMessageBody.cs
78 строк
2 KB
Chris Patterson
Related to #3490 - Changed EF Core object property serialization so that it should work with Newtonsoft, Also Azure Table storage
11 июл 2022, 16:07
11 июл 2022, 16:07
514eef7
Код
Авторство
О чём код?
#nullable enable namespace MassTransit.Serialization { using System; using System.IO; using System.Runtime.Serialization; using System.Text; using System.Text.Json; public class SystemTextJsonObjectMessageBody : MessageBody { readonly JsonSerializerOptions _options; readonly object _value; byte[]? _bytes; string? _string; public SystemTextJsonObjectMessageBody(object value, JsonSerializerOptions options) { _value = value; _options = options; } public long? Length => _bytes?.Length ?? _string?.Length; public Stream GetStream() { return new MemoryStream(GetBytes(), false); } public byte[] GetBytes() { if (_bytes != null) return _bytes; if (_string != null) { _bytes = Encoding.UTF8.GetBytes(_string); return _bytes; } try { _bytes = JsonSerializer.SerializeToUtf8Bytes(_value, _options); return _bytes; } catch (Exception ex) { throw new SerializationException("Failed to serialize message", ex); } } public string GetString() { if (_string != null) return _string; if (_bytes != null) { _string = Encoding.UTF8.GetString(_bytes); return _string; } try { _string = JsonSerializer.Serialize(_value, _options); return _string; } catch (Exception ex) { throw new SerializationException("Failed to serialize message", ex); } } } }