From 378f350c52324eaac26843e88b12671499c38dc6 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 23 Nov 2020 21:18:44 +0800 Subject: [PATCH] ToJsonFile --- v2rayN/v2rayN/Handler/V2rayConfigHandler.cs | 4 ++-- v2rayN/v2rayN/Tool/Utils.cs | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index a79ae73e..bc28924c 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -84,7 +84,7 @@ namespace v2rayN.Handler // TODO: 统计配置 statistic(config, ref v2rayConfig); - Utils.ToJsonFile(v2rayConfig, fileName); + Utils.ToJsonFile(v2rayConfig, fileName, false); msg = string.Format(UIRes.I18N("SuccessfulConfiguration"), config.getSummary()); } @@ -919,7 +919,7 @@ namespace v2rayN.Handler //传出设置 ServerOutbound(config, ref v2rayConfig); - Utils.ToJsonFile(v2rayConfig, fileName); + Utils.ToJsonFile(v2rayConfig, fileName, false); msg = string.Format(UIRes.I18N("SuccessfulConfiguration"), config.getSummary()); } diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index 09c023de..22c37976 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -119,16 +119,22 @@ namespace v2rayN /// /// /// - public static int ToJsonFile(Object obj, string filePath) + public static int ToJsonFile(Object obj, string filePath, bool nullValue = true) { int result; try { using (StreamWriter file = File.CreateText(filePath)) { - //JsonSerializer serializer = new JsonSerializer(); - JsonSerializer serializer = new JsonSerializer() { Formatting = Formatting.Indented }; - //JsonSerializer serializer = new JsonSerializer() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; + JsonSerializer serializer; + if (nullValue) + { + serializer = new JsonSerializer() { Formatting = Formatting.Indented }; + } + else + { + serializer = new JsonSerializer() { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }; + } serializer.Serialize(file, obj); } @@ -800,7 +806,7 @@ namespace v2rayN public static string GetTempPath(string filename) { return Path.Combine(GetTempPath(), filename); - } + } public static string UnGzip(byte[] buf) {