26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
43 lines
1.1 KiB
C#
Executable File
43 lines
1.1 KiB
C#
Executable File
using Newtonsoft.Json;
|
|
|
|
namespace DataModel.Helpers
|
|
{
|
|
/// <summary>
|
|
/// JSON serialization helpers
|
|
/// </summary>
|
|
public static class JsonHelpers
|
|
{
|
|
/// <summary>
|
|
/// Converts parameter to JSON
|
|
/// </summary>
|
|
/// <param name="value">Value to serialize</param>
|
|
/// <returns>Parameter value serialized in JSON format</returns>
|
|
public static string ToJSON<T>(this T value)
|
|
{
|
|
return value == null ?
|
|
"{}" :
|
|
JsonConvert.SerializeObject(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses value from JSON
|
|
/// </summary>
|
|
/// <param name="json">JSON to parse</param>
|
|
/// <returns>Parsed value</returns>
|
|
public static T FromJSON<T>(string json)
|
|
{
|
|
T value = default;
|
|
try
|
|
{
|
|
value = JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
catch
|
|
{
|
|
//Do nothing
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|