mirror of https://github.com/2dust/v2rayN
Migrate from Newtonsoft.Json to System.Text.Json
parent
bd838094cd
commit
2e30c04238
|
@ -1,6 +1,7 @@
|
||||||
using Newtonsoft.Json;
|
using System.IO;
|
||||||
using Newtonsoft.Json.Linq;
|
using System.Text.Json;
|
||||||
using System.IO;
|
using System.Text.Json.Nodes;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace v2rayN
|
namespace v2rayN
|
||||||
{
|
{
|
||||||
|
@ -31,7 +32,7 @@ namespace v2rayN
|
||||||
{
|
{
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
return JsonConvert.DeserializeObject<T>(strJson);
|
return JsonSerializer.Deserialize<T>(strJson);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -44,7 +45,7 @@ namespace v2rayN
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="strJson"></param>
|
/// <param name="strJson"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static JObject? ParseJson(string strJson)
|
public static JsonNode? ParseJson(string strJson)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -52,8 +53,7 @@ namespace v2rayN
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return JsonNode.Parse(strJson);
|
||||||
return JObject.Parse(strJson);
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -77,9 +77,12 @@ namespace v2rayN
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
result = JsonConvert.SerializeObject(obj,
|
var options = new JsonSerializerOptions
|
||||||
indented ? Formatting.Indented : Formatting.None,
|
{
|
||||||
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
WriteIndented = indented ? true : false,
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
|
};
|
||||||
|
result = JsonSerializer.Serialize(obj, options);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -88,6 +91,13 @@ namespace v2rayN
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SerializeToNode
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Save as json file
|
/// Save as json file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -100,13 +110,15 @@ namespace v2rayN
|
||||||
int result;
|
int result;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using StreamWriter file = File.CreateText(filePath);
|
using FileStream file = File.Create(filePath);
|
||||||
var serializer = new JsonSerializer()
|
|
||||||
|
var options = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
WriteIndented = true,
|
||||||
NullValueHandling = nullValue ? NullValueHandling.Include : NullValueHandling.Ignore
|
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
|
||||||
};
|
};
|
||||||
serializer.Serialize(file, obj);
|
|
||||||
|
JsonSerializer.Serialize(file, obj, options);
|
||||||
result = 0;
|
result = 0;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
@ -927,6 +927,37 @@ namespace v2rayN
|
||||||
return value is int i && i > 0;
|
return value is int i && i > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取系统hosts
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Dictionary<string, string> GetSystemHosts()
|
||||||
|
{
|
||||||
|
var systemHosts = new Dictionary<string, string>();
|
||||||
|
var hostfile = @"C:\Windows\System32\drivers\etc\hosts";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (File.Exists(hostfile))
|
||||||
|
{
|
||||||
|
var hosts = File.ReadAllText(hostfile).Replace("\r", "");
|
||||||
|
var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var host in hostsList)
|
||||||
|
{
|
||||||
|
if (host.StartsWith("#")) continue;
|
||||||
|
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (hostItem.Length < 2) continue;
|
||||||
|
systemHosts.Add(hostItem[1], hostItem[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logging.SaveLog(ex.Message, ex);
|
||||||
|
}
|
||||||
|
return systemHosts;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion 杂项
|
#endregion 杂项
|
||||||
|
|
||||||
#region TempPath
|
#region TempPath
|
||||||
|
|
|
@ -675,7 +675,7 @@ namespace v2rayN.Handler
|
||||||
|
|
||||||
private void ParseV2Domain(string domain, Rule4Sbox rule)
|
private void ParseV2Domain(string domain, Rule4Sbox rule)
|
||||||
{
|
{
|
||||||
if (domain.StartsWith("ext:") || domain.StartsWith("ext-domain:"))
|
if (domain.StartsWith("#") || domain.StartsWith("ext:") || domain.StartsWith("ext-domain:"))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
using Newtonsoft.Json.Linq;
|
using System.Net;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using v2rayN.Mode;
|
using v2rayN.Mode;
|
||||||
using v2rayN.Resx;
|
using v2rayN.Resx;
|
||||||
|
@ -721,9 +719,8 @@ namespace v2rayN.Handler
|
||||||
outbound.settings.userLevel = 0;
|
outbound.settings.userLevel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var obj = JsonUtils.ParseJson(normalDNS) ?? [];
|
var obj = JsonUtils.ParseJson(normalDNS);
|
||||||
|
if (obj is null)
|
||||||
if (!obj.ContainsKey("servers"))
|
|
||||||
{
|
{
|
||||||
List<string> servers = [];
|
List<string> servers = [];
|
||||||
string[] arrDNS = normalDNS.Split(',');
|
string[] arrDNS = normalDNS.Split(',');
|
||||||
|
@ -731,36 +728,26 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
servers.Add(str);
|
servers.Add(str);
|
||||||
}
|
}
|
||||||
obj["servers"] = JArray.FromObject(servers);
|
obj = JsonUtils.ParseJson("{}");
|
||||||
|
obj["servers"] = JsonUtils.SerializeToNode(servers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 追加至 dns 设置
|
||||||
if (item.useSystemHosts)
|
if (item.useSystemHosts)
|
||||||
{
|
{
|
||||||
var hostfile = @"C:\Windows\System32\drivers\etc\hosts";
|
var systemHosts = Utils.GetSystemHosts();
|
||||||
|
if (systemHosts.Count > 0)
|
||||||
if (File.Exists(hostfile))
|
|
||||||
{
|
{
|
||||||
var hosts = File.ReadAllText(hostfile).Replace("\r", "");
|
var normalHost = obj["hosts"];
|
||||||
var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
if (normalHost != null)
|
||||||
|
|
||||||
// 获取系统hosts
|
|
||||||
var systemHosts = new Dictionary<string, string>();
|
|
||||||
foreach (var host in hostsList)
|
|
||||||
{
|
{
|
||||||
if (host.StartsWith("#")) continue;
|
foreach (var host in systemHosts)
|
||||||
var hostItem = host.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
{
|
||||||
if (hostItem.Length < 2) continue;
|
if (normalHost[host.Key] != null)
|
||||||
systemHosts.Add(hostItem[1], hostItem[0]);
|
continue;
|
||||||
|
normalHost[host.Key] = host.Value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 追加至 dns 设置
|
|
||||||
var normalHost = obj["hosts"] ?? new JObject();
|
|
||||||
foreach (var host in systemHosts)
|
|
||||||
{
|
|
||||||
if (normalHost[host.Key] != null) continue;
|
|
||||||
normalHost[host.Key] = host.Value;
|
|
||||||
}
|
|
||||||
obj["hosts"] = normalHost;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,68 +1,68 @@
|
||||||
using Newtonsoft.Json;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace v2rayN.Mode
|
namespace v2rayN.Mode
|
||||||
{
|
{
|
||||||
public class GitHubReleaseAsset
|
public class GitHubReleaseAsset
|
||||||
{
|
{
|
||||||
[JsonProperty("url")] public string Url { get; set; }
|
[JsonPropertyName("url")] public string Url { get; set; }
|
||||||
|
|
||||||
[JsonProperty("id")] public int Id { get; set; }
|
[JsonPropertyName("id")] public int Id { get; set; }
|
||||||
|
|
||||||
[JsonProperty("node_id")] public string NodeId { get; set; }
|
[JsonPropertyName("node_id")] public string NodeId { get; set; }
|
||||||
|
|
||||||
[JsonProperty("name")] public string Name { get; set; }
|
[JsonPropertyName("name")] public string Name { get; set; }
|
||||||
|
|
||||||
[JsonProperty("label")] public object Label { get; set; }
|
[JsonPropertyName("label")] public object Label { get; set; }
|
||||||
|
|
||||||
[JsonProperty("content_type")] public string ContentType { get; set; }
|
[JsonPropertyName("content_type")] public string ContentType { get; set; }
|
||||||
|
|
||||||
[JsonProperty("state")] public string State { get; set; }
|
[JsonPropertyName("state")] public string State { get; set; }
|
||||||
|
|
||||||
[JsonProperty("size")] public int Size { get; set; }
|
[JsonPropertyName("size")] public int Size { get; set; }
|
||||||
|
|
||||||
[JsonProperty("download_count")] public int DownloadCount { get; set; }
|
[JsonPropertyName("download_count")] public int DownloadCount { get; set; }
|
||||||
|
|
||||||
[JsonProperty("created_at")] public DateTime CreatedAt { get; set; }
|
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; }
|
||||||
|
|
||||||
[JsonProperty("updated_at")] public DateTime UpdatedAt { get; set; }
|
[JsonPropertyName("updated_at")] public DateTime UpdatedAt { get; set; }
|
||||||
|
|
||||||
[JsonProperty("browser_download_url")] public string BrowserDownloadUrl { get; set; }
|
[JsonPropertyName("browser_download_url")] public string BrowserDownloadUrl { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GitHubRelease
|
public class GitHubRelease
|
||||||
{
|
{
|
||||||
[JsonProperty("url")] public string Url { get; set; }
|
[JsonPropertyName("url")] public string Url { get; set; }
|
||||||
|
|
||||||
[JsonProperty("assets_url")] public string AssetsUrl { get; set; }
|
[JsonPropertyName("assets_url")] public string AssetsUrl { get; set; }
|
||||||
|
|
||||||
[JsonProperty("upload_url")] public string UploadUrl { get; set; }
|
[JsonPropertyName("upload_url")] public string UploadUrl { get; set; }
|
||||||
|
|
||||||
[JsonProperty("html_url")] public string HtmlUrl { get; set; }
|
[JsonPropertyName("html_url")] public string HtmlUrl { get; set; }
|
||||||
|
|
||||||
[JsonProperty("id")] public int Id { get; set; }
|
[JsonPropertyName("id")] public int Id { get; set; }
|
||||||
|
|
||||||
[JsonProperty("node_id")] public string NodeId { get; set; }
|
[JsonPropertyName("node_id")] public string NodeId { get; set; }
|
||||||
|
|
||||||
[JsonProperty("tag_name")] public string TagName { get; set; }
|
[JsonPropertyName("tag_name")] public string TagName { get; set; }
|
||||||
|
|
||||||
[JsonProperty("target_commitish")] public string TargetCommitish { get; set; }
|
[JsonPropertyName("target_commitish")] public string TargetCommitish { get; set; }
|
||||||
|
|
||||||
[JsonProperty("name")] public string Name { get; set; }
|
[JsonPropertyName("name")] public string Name { get; set; }
|
||||||
|
|
||||||
[JsonProperty("draft")] public bool Draft { get; set; }
|
[JsonPropertyName("draft")] public bool Draft { get; set; }
|
||||||
|
|
||||||
[JsonProperty("prerelease")] public bool Prerelease { get; set; }
|
[JsonPropertyName("prerelease")] public bool Prerelease { get; set; }
|
||||||
|
|
||||||
[JsonProperty("created_at")] public DateTime CreatedAt { get; set; }
|
[JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; }
|
||||||
|
|
||||||
[JsonProperty("published_at")] public DateTime PublishedAt { get; set; }
|
[JsonPropertyName("published_at")] public DateTime PublishedAt { get; set; }
|
||||||
|
|
||||||
[JsonProperty("assets")] public List<GitHubReleaseAsset> Assets { get; set; }
|
[JsonPropertyName("assets")] public List<GitHubReleaseAsset> Assets { get; set; }
|
||||||
|
|
||||||
[JsonProperty("tarball_url")] public string TarballUrl { get; set; }
|
[JsonPropertyName("tarball_url")] public string TarballUrl { get; set; }
|
||||||
|
|
||||||
[JsonProperty("zipball_url")] public string ZipballUrl { get; set; }
|
[JsonPropertyName("zipball_url")] public string ZipballUrl { get; set; }
|
||||||
|
|
||||||
[JsonProperty("body")] public string Body { get; set; }
|
[JsonPropertyName("body")] public string Body { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
using Newtonsoft.Json;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace v2rayN.Mode
|
namespace v2rayN.Mode
|
||||||
{
|
{
|
||||||
|
@ -580,7 +580,7 @@ namespace v2rayN.Mode
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户代理
|
/// 用户代理
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonProperty("User-Agent")]
|
[JsonPropertyName("User-Agent")]
|
||||||
public string UserAgent { get; set; }
|
public string UserAgent { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,7 @@ namespace v2rayN.ViewModels
|
||||||
if (!Utils.IsNullOrEmpty(normalDNS))
|
if (!Utils.IsNullOrEmpty(normalDNS))
|
||||||
{
|
{
|
||||||
var obj = JsonUtils.ParseJson(normalDNS);
|
var obj = JsonUtils.ParseJson(normalDNS);
|
||||||
if (obj != null && obj.ContainsKey("servers") == true)
|
if (obj != null && obj["servers"] != null)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue