Last works ...
This commit is contained in:
@@ -1,9 +1,105 @@
|
||||
using xApi.Models;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace xApi.Extensions
|
||||
{
|
||||
public static class ModelExtensions
|
||||
{
|
||||
//
|
||||
#region Common ...
|
||||
/// <summary>
|
||||
/// Parse Json String to Specified Type of Object ...
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="json"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public static T? ParseJSON<T>(
|
||||
this string json,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
return JsonSerializer
|
||||
.Deserialize<T>(json, options);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to deserialize JSON to {typeof(T).Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert to Json String ...
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public static string? ToJSON(
|
||||
this object? obj,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (obj is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
try
|
||||
{
|
||||
//
|
||||
return JsonSerializer
|
||||
.Serialize(obj, options);
|
||||
}
|
||||
catch (NotSupportedException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to serialize object of type {obj.GetType().Name}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert to Pretty Json String ...
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string? ToPrettyJSON(
|
||||
this object? obj,
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (obj is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
options = GetDefaultJsonSerializerOptions(options);
|
||||
options.WriteIndented = true;
|
||||
return JsonSerializer.Serialize(obj, options);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Validate Configuration Model ...
|
||||
/// </summary>
|
||||
@@ -20,5 +116,49 @@ namespace xApi.Extensions
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Configuration Model ...
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this AzureServiceConfiguration? configuration)
|
||||
{
|
||||
//
|
||||
var result = configuration != null
|
||||
&& !string.IsNullOrWhiteSpace(configuration.Pat)
|
||||
&& !string.IsNullOrWhiteSpace(configuration.BaseUrl);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Retrieve Default JsonSerializerOptions ...
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
private static JsonSerializerOptions GetDefaultJsonSerializerOptions(
|
||||
JsonSerializerOptions? options = null
|
||||
)
|
||||
{
|
||||
//
|
||||
if (options is null)
|
||||
{
|
||||
options = new()
|
||||
{
|
||||
WriteIndented = false,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
return options;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user