Dreamine.Communication.Core 1.0.2
Dreamine.Communication.Core 통신 기능과 관련 API를 제공합니다.
로딩중...
검색중...
일치하는것 없음
JsonMessageSerializer.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Text.Json;
3using Dreamine.Communication.Abstractions.Interfaces;
4using Dreamine.Communication.Abstractions.Models;
5
7
16public sealed class JsonMessageSerializer : IMessageSerializer
17{
26 private readonly JsonSerializerOptions _options;
27
37 {
38 _options = new JsonSerializerOptions
39 {
40 PropertyNameCaseInsensitive = true,
41 WriteIndented = false
42 };
43 }
44
69 public JsonMessageSerializer(JsonSerializerOptions options)
70 {
71 _options = options ?? throw new ArgumentNullException(nameof(options));
72 }
73
114 public byte[] Serialize(MessageEnvelope message)
115 {
116 ArgumentNullException.ThrowIfNull(message);
117 return JsonSerializer.SerializeToUtf8Bytes(message, _options);
118 }
119
168 public MessageEnvelope Deserialize(byte[] data)
169 {
170 ArgumentNullException.ThrowIfNull(data);
171
172 var message = JsonSerializer.Deserialize<MessageEnvelope>(data, _options);
173
174 if (message is null)
175 {
176 throw new InvalidOperationException("Failed to deserialize MessageEnvelope.");
177 }
178
179 return message;
180 }
181}