Migrate from Newtonsoft.Json to System.Text.Json

pull/4711/head
2dust 10 months ago
parent bd838094cd
commit 2e30c04238

@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace v2rayN
{
@ -31,7 +32,7 @@ namespace v2rayN
{
return default;
}
return JsonConvert.DeserializeObject<T>(strJson);
return JsonSerializer.Deserialize<T>(strJson);
}
catch
{
@ -44,7 +45,7 @@ namespace v2rayN
/// </summary>
/// <param name="strJson"></param>
/// <returns></returns>
public static JObject? ParseJson(string strJson)
public static JsonNode? ParseJson(string strJson)
{
try
{
@ -52,8 +53,7 @@ namespace v2rayN
{
return null;
}
return JObject.Parse(strJson);
return JsonNode.Parse(strJson);
}
catch
{
@ -77,9 +77,12 @@ namespace v2rayN
{
return result;
}
result = JsonConvert.SerializeObject(obj,
indented ? Formatting.Indented : Formatting.None,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var options = new JsonSerializerOptions
{
WriteIndented = indented ? true : false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
result = JsonSerializer.Serialize(obj, options);
}
catch (Exception ex)
{
@ -88,6 +91,13 @@ namespace v2rayN
return result;
}
/// <summary>
/// SerializeToNode
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static JsonNode? SerializeToNode(object? obj) => JsonSerializer.SerializeToNode(obj);
/// <summary>
/// Save as json file
/// </summary>
@ -100,13 +110,15 @@ namespace v2rayN
int result;
try
{
using StreamWriter file = File.CreateText(filePath);
var serializer = new JsonSerializer()
using FileStream file = File.Create(filePath);
var options = new JsonSerializerOptions
{
Formatting = Formatting.Indented,
NullValueHandling = nullValue ? NullValueHandling.Include : NullValueHandling.Ignore
WriteIndented = true,
DefaultIgnoreCondition = nullValue ? JsonIgnoreCondition.Never : JsonIgnoreCondition.WhenWritingNull
};
serializer.Serialize(file, obj);
JsonSerializer.Serialize(file, obj, options);
result = 0;
}
catch (Exception ex)

@ -927,6 +927,37 @@ namespace v2rayN
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 杂项
#region TempPath

@ -675,7 +675,7 @@ namespace v2rayN.Handler
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;
}

@ -1,6 +1,4 @@
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;
using System.Net;
using System.Net.NetworkInformation;
using v2rayN.Mode;
using v2rayN.Resx;
@ -721,9 +719,8 @@ namespace v2rayN.Handler
outbound.settings.userLevel = 0;
}
var obj = JsonUtils.ParseJson(normalDNS) ?? [];
if (!obj.ContainsKey("servers"))
var obj = JsonUtils.ParseJson(normalDNS);
if (obj is null)
{
List<string> servers = [];
string[] arrDNS = normalDNS.Split(',');
@ -731,36 +728,26 @@ namespace v2rayN.Handler
{
servers.Add(str);
}
obj["servers"] = JArray.FromObject(servers);
obj = JsonUtils.ParseJson("{}");
obj["servers"] = JsonUtils.SerializeToNode(servers);
}
// 追加至 dns 设置
if (item.useSystemHosts)
{
var hostfile = @"C:\Windows\System32\drivers\etc\hosts";
if (File.Exists(hostfile))
var systemHosts = Utils.GetSystemHosts();
if (systemHosts.Count > 0)
{
var hosts = File.ReadAllText(hostfile).Replace("\r", "");
var hostsList = hosts.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// 获取系统hosts
var systemHosts = new Dictionary<string, string>();
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]);
}
// 追加至 dns 设置
var normalHost = obj["hosts"] ?? new JObject();
foreach (var host in systemHosts)
var normalHost = obj["hosts"];
if (normalHost != null)
{
if (normalHost[host.Key] != null) continue;
normalHost[host.Key] = host.Value;
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
{
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
{
[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
{
@ -580,7 +580,7 @@ namespace v2rayN.Mode
/// <summary>
/// 用户代理
/// </summary>
[JsonProperty("User-Agent")]
[JsonPropertyName("User-Agent")]
public string UserAgent { get; set; }
}

@ -64,7 +64,7 @@ namespace v2rayN.ViewModels
if (!Utils.IsNullOrEmpty(normalDNS))
{
var obj = JsonUtils.ParseJson(normalDNS);
if (obj != null && obj.ContainsKey("servers") == true)
if (obj != null && obj["servers"] != null)
{
}
else

Loading…
Cancel
Save