From 701d18216c54c843842f0d1e64879f62822e0d8a Mon Sep 17 00:00:00 2001 From: Jai <814683@qq.com> Date: Thu, 5 Aug 2021 11:05:18 +0800 Subject: [PATCH] optimiz: remove dependency Newtonsoft.Json --- build.bat | 2 +- src/Models/Issue.cs | 23 +---------------------- src/Models/Preference.cs | 14 -------------- src/Models/Repository.cs | 5 ----- src/Models/Version.cs | 27 +-------------------------- src/SourceGit_48.csproj | 2 +- 6 files changed, 4 insertions(+), 69 deletions(-) diff --git a/build.bat b/build.bat index b055284c..f055fd4c 100644 --- a/build.bat +++ b/build.bat @@ -6,7 +6,7 @@ cd src rmdir /s /q bin rmdir /s /q obj dotnet publish SourceGit_48.csproj --nologo -c Release -r win-x86 -o ..\publish\net48 -ilrepack /ndebug /out:..\publish\SourceGit.exe ..\publish\net48\SourceGit.exe ..\publish\net48\Newtonsoft.Json.dll +ilrepack /ndebug /wildcards /out:..\publish\SourceGit.exe ..\publish\net48\SourceGit.exe ..\publish\net48\*.dll cd ..\publish ren SourceGit.exe SourceGit_48.exe rmdir /s /q net48 diff --git a/src/Models/Issue.cs b/src/Models/Issue.cs index 834e1f6c..9278e2d2 100644 --- a/src/Models/Issue.cs +++ b/src/Models/Issue.cs @@ -5,29 +5,14 @@ using System.Net; using System.Text; using System.Threading.Tasks; using System.Reflection; - -#if NET48 -using Newtonsoft.Json; -#else using System.Text.Json; using System.Text.Json.Serialization; -#endif namespace SourceGit.Models { /// /// 崩溃日志上报 /// public class Issue { -#if NET48 - [JsonProperty(PropertyName = "access_token")] - public string AccessToken { get; set; } - [JsonProperty(PropertyName = "repo")] - public string Repo { get; set; } - [JsonProperty(PropertyName = "title")] - public string Title { get; set; } - [JsonProperty(PropertyName = "body")] - public string Body { get; set; } -#else [JsonPropertyName("access_token")] public string AccessToken { get; set; } [JsonPropertyName("repo")] @@ -36,7 +21,6 @@ namespace SourceGit.Models { public string Title { get; set; } [JsonPropertyName("body")] public string Body { get; set; } -#endif /// /// 创建Gitee平台ISSUE @@ -64,14 +48,9 @@ namespace SourceGit.Models { req.Timeout = 1000; using (var writer = req.GetRequestStream()) { -#if NET48 - var data = JsonConvert.SerializeObject(issue); + var data = JsonSerializer.Serialize(issue); var raw = Encoding.UTF8.GetBytes(data); writer.Write(raw, 0, raw.Length); -#else - var data = JsonSerializer.Serialize(issue); - writer.Write(Encoding.UTF8.GetBytes(data)); -#endif } req.GetResponse(); diff --git a/src/Models/Preference.cs b/src/Models/Preference.cs index f0dcc505..823b97a4 100644 --- a/src/Models/Preference.cs +++ b/src/Models/Preference.cs @@ -2,13 +2,8 @@ using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; - -#if NET48 -using Newtonsoft.Json; -#else using System.Text.Json; using System.Text.Json.Serialization; -#endif namespace SourceGit.Models { @@ -193,11 +188,7 @@ namespace SourceGit.Models { instance = new Preference(); } else { try { -#if NET48 - instance = JsonConvert.DeserializeObject(File.ReadAllText(SAVE_PATH)); -#else instance = JsonSerializer.Deserialize(File.ReadAllText(SAVE_PATH)); -#endif } catch { instance = new Preference(); } @@ -219,12 +210,7 @@ namespace SourceGit.Models { public static void Save() { var dir = Path.GetDirectoryName(SAVE_PATH); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); - -#if NET48 - var data = JsonConvert.SerializeObject(instance, Formatting.Indented); -#else var data = JsonSerializer.Serialize(instance, new JsonSerializerOptions() { WriteIndented = true }); -#endif File.WriteAllText(SAVE_PATH, data); } #endregion diff --git a/src/Models/Repository.cs b/src/Models/Repository.cs index 36df309b..7b90f08f 100644 --- a/src/Models/Repository.cs +++ b/src/Models/Repository.cs @@ -1,10 +1,5 @@ using System.Collections.Generic; - -#if NET48 -using Newtonsoft.Json; -#else using System.Text.Json.Serialization; -#endif namespace SourceGit.Models { diff --git a/src/Models/Version.cs b/src/Models/Version.cs index 0567c85b..19da6cfb 100644 --- a/src/Models/Version.cs +++ b/src/Models/Version.cs @@ -4,13 +4,8 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; - -#if NET48 -using Newtonsoft.Json; -#else using System.Text.Json; using System.Text.Json.Serialization; -#endif namespace SourceGit.Models { @@ -18,22 +13,6 @@ namespace SourceGit.Models { /// Gitee开放API中Release信息格式 /// public class Version { -#if NET48 - [JsonProperty(PropertyName = "id")] - public ulong Id { get; set; } - [JsonProperty(PropertyName = "tag_name")] - public string TagName { get; set; } - [JsonProperty(PropertyName = "target_commitish")] - public string CommitSHA { get; set; } - [JsonProperty(PropertyName = "prerelease")] - public bool PreRelease { get; set; } - [JsonProperty(PropertyName = "name")] - public string Name { get; set; } - [JsonProperty(PropertyName = "body")] - public string Body { get; set; } - [JsonProperty(PropertyName = "created_at")] - public DateTime CreatedAt { get; set; } -#else [JsonPropertyName("id")] public ulong Id { get; set; } [JsonPropertyName("tag_name")] @@ -48,7 +27,7 @@ namespace SourceGit.Models { public string Body { get; set; } [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } -#endif + public string PublishTime { get { return CreatedAt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"); } } @@ -68,11 +47,7 @@ namespace SourceGit.Models { try { var web = new WebClient() { Encoding = Encoding.UTF8 }; var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/sourcegit/releases/latest"); -#if NET48 - var ver = JsonConvert.DeserializeObject(raw); -#else var ver = JsonSerializer.Deserialize(raw); -#endif var cur = Assembly.GetExecutingAssembly().GetName().Version; var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*"); diff --git a/src/SourceGit_48.csproj b/src/SourceGit_48.csproj index 0731cf71..1f1bc156 100644 --- a/src/SourceGit_48.csproj +++ b/src/SourceGit_48.csproj @@ -20,6 +20,6 @@ SourceGit - + \ No newline at end of file