Mark van Renswoude
c75ea0cc62
Fixed class name missing when selecting from Tapeti class Added Filter to class selection dialog Fixed or removed some minor todo's
39 lines
1.0 KiB
C#
39 lines
1.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace PettingZoo.Core.Rendering
|
|
{
|
|
public class MessageBodyRenderer
|
|
{
|
|
private static readonly Dictionary<string, Func<byte[], string>> ContentTypeHandlers = new()
|
|
{
|
|
{ "application/json", RenderJson }
|
|
};
|
|
|
|
|
|
public static string Render(byte[] body, string? contentType)
|
|
{
|
|
return contentType != null && ContentTypeHandlers.TryGetValue(contentType, out var handler)
|
|
? handler(body)
|
|
: Encoding.UTF8.GetString(body);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|