Created
June 7, 2025 15:09
-
-
Save ZedDevStuff/681b7d8a2528aace0a41e14f9799d1e4 to your computer and use it in GitHub Desktop.
Request.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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