Skip to content

Instantly share code, notes, and snippets.

@ZedDevStuff
Created June 7, 2025 15:09
Show Gist options
  • Save ZedDevStuff/681b7d8a2528aace0a41e14f9799d1e4 to your computer and use it in GitHub Desktop.
Save ZedDevStuff/681b7d8a2528aace0a41e14f9799d1e4 to your computer and use it in GitHub Desktop.
Request.cs
using System;
using System.Text.Json;
namespace BridgeLib;
public struct Request
{
public int Id { get; set; }
public string MethodName { get; set; }
public string JsonData { get; set; }
/// <summary>
/// Deserialize the JsonData into the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public readonly T GetData<T>()
{
return JsonSerializer.Deserialize<T>(JsonData) ?? throw new InvalidOperationException("Failed to deserialize JsonData.");
}
/// <summary>
/// Attempts to deserialize JsonData into the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public readonly bool TryGetData<T>(out T? data)
{
try
{
data = GetData<T>();
return true;
}
catch
{
data = default;
return false;
}
}
public readonly Response ToResponse(bool success, object? data = null)
{
return new Response(Id, success, data ?? new { });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment