1
0
mirror of synced 2024-11-15 01:33:51 +00:00
PettingZoo/PettingZoo.Core/Rendering/MessageBodyRenderer.cs
Mark van Renswoude 2755e93440 Started major refactoring
- Upgrade to .NET 5
- Split functionality into libraries
- Tabbed interface for multiple subscribers
- Placeholder for publishing tab
2021-11-28 14:18:21 +01:00

41 lines
1.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace PettingZoo.Core.Rendering
{
public class MessageBodyRenderer
{
public static Dictionary<string, Func<byte[], string>> ContentTypeHandlers = new()
{
{ "application/json", RenderJson }
};
public static string Render(byte[] body, string contentType = "")
{
return ContentTypeHandlers.TryGetValue(contentType, out var handler)
? handler(body)
: Encoding.UTF8.GetString(body);
// ToDo hex output if required
}
public static string RenderJson(byte[] body)
{
var bodyText = Encoding.UTF8.GetString(body);
try
{
var obj = JsonConvert.DeserializeObject(bodyText);
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}
catch
{
return bodyText;
}
}
}
}