Skip to content

Commit

Permalink
Allow ReceiveMessage to auto deserialize Json/Messagepack.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Apr 12, 2024
1 parent 8e7a6c1 commit 184395d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/HouseofCat.RabbitMQ/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Constants
public const string HeaderValueForContentTypeBinary = "application/octet-stream";
public const string HeaderValueForContentTypePlainText = "text/plain";
public const string HeaderValueForContentTypeJson = "application/json";
public const string HeaderValueForContentTypeMessagePack = "application/msgpack";
public static string HeaderForObjectType { get; set; } = "X-RD-OBJECTTYPE";
public static string HeaderValueForMessageObjectType { get; set; } = "IMESSAGE";

Expand Down
9 changes: 9 additions & 0 deletions src/HouseofCat.RabbitMQ/Messages/ReceivedMessage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using HouseofCat.Serialization;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
Expand Down Expand Up @@ -122,6 +123,14 @@ private void ReadHeaders()
catch
{ FailedToDeserialize = true; }
break;
case Constants.HeaderValueForContentTypeMessagePack:
try
{
Message = MessagePackProvider.GlobalDeserialize<Message>(Body);
}
catch
{ FailedToDeserialize = true; }
break;
case Constants.HeaderValueForContentTypeBinary:
case Constants.HeaderValueForContentTypePlainText:
default:
Expand Down
9 changes: 9 additions & 0 deletions src/HouseofCat.Serialization/MessagePackProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ public class MessagePackProvider : ISerializationProvider
{
private readonly MessagePackSerializerOptions _options;

// After nearly a decade, they haven't made their mind up. Tired of waiting.
// https://github.com/msgpack/msgpack/issues/194
public string ContentType { get; private set; } = "application/msgpack";

public MessagePackProvider(MessagePackSerializerOptions options = null)
{
_options = options;
}

public static TOut GlobalDeserialize<TOut>(ReadOnlyMemory<byte> input, MessagePackSerializerOptions options = null)
{
return MessagePackSerializer.Deserialize<TOut>(input, options);
}

public ReadOnlyMemory<byte> Serialize<TIn>(TIn input)
{
return MessagePackSerializer.Serialize(input, _options);
Expand Down
2 changes: 2 additions & 0 deletions src/HouseofCat.Serialization/NewtonsoftJsonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public sealed class NewtonsoftJsonProvider : ISerializationProvider
{
private readonly JsonSerializer _jsonSerializer = new JsonSerializer();

public string ContentType { get; private set; } = "application/json";

public TOut Deserialize<TOut>(ReadOnlyMemory<byte> input)
{
Guard.AgainstEmpty(input, nameof(input));
Expand Down

0 comments on commit 184395d

Please sign in to comment.