mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-28 05:46:19 +00:00

* Simplify DNS Settings * fix * ExpectedIPs * Optimize ExpectedIPs Logic * Fixes geoip overrides rule_set when geosite is also set * rename DNSItem to SimpleDNSItem * Compatible * Fixes Combobox for desktop * Regional Preset * Fix * Refactor DNS tags handling * Uses correct DNS strategy for direct connections * auto-disable DNS items with empty NormalDNS on startup
135 lines
3.4 KiB
C#
135 lines
3.4 KiB
C#
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace ServiceLib.Common;
|
|
|
|
public class JsonUtils
|
|
{
|
|
private static readonly string _tag = "JsonUtils";
|
|
|
|
/// <summary>
|
|
/// DeepCopy
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static T DeepCopy<T>(T obj)
|
|
{
|
|
return Deserialize<T>(Serialize(obj, false))!;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deserialize to object
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="strJson"></param>
|
|
/// <returns></returns>
|
|
public static T? Deserialize<T>(string? strJson)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(strJson))
|
|
{
|
|
return default;
|
|
}
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
return JsonSerializer.Deserialize<T>(strJson, options);
|
|
}
|
|
catch
|
|
{
|
|
return default;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// parse
|
|
/// </summary>
|
|
/// <param name="strJson"></param>
|
|
/// <returns></returns>
|
|
public static JsonNode? ParseJson(string strJson)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(strJson))
|
|
{
|
|
return null;
|
|
}
|
|
return JsonNode.Parse(strJson);
|
|
}
|
|
catch
|
|
{
|
|
//SaveLog(ex.Message, ex);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize Object to Json string
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <param name="indented"></param>
|
|
/// <param name="nullValue"></param>
|
|
/// <returns></returns>
|
|
public static string Serialize(object? obj, bool indented = true, bool nullValue = false)
|
|
{
|
|
var result = string.Empty;
|
|
try
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return result;
|
|
}
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
WriteIndented = indented,
|
|
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull,
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
|
};
|
|
result = JsonSerializer.Serialize(obj, options);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logging.SaveLog(_tag, ex);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize Object to Json string
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
public static string Serialize(object? obj, JsonSerializerOptions options)
|
|
{
|
|
var result = string.Empty;
|
|
try
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return result;
|
|
}
|
|
result = JsonSerializer.Serialize(obj, options);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logging.SaveLog(_tag, ex);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// SerializeToNode
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static JsonNode? SerializeToNode(object? obj, JsonSerializerOptions? options = null)
|
|
{
|
|
return JsonSerializer.SerializeToNode(obj, options);
|
|
}
|
|
}
|