optimiz<Net48>: remove dependency Newtonsoft.Json

This commit is contained in:
Jai 2021-08-05 11:05:18 +08:00
parent 316344939a
commit 701d18216c
6 changed files with 4 additions and 69 deletions

View file

@ -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

View file

@ -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 {
/// <summary>
/// 崩溃日志上报
/// </summary>
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
/// <summary>
/// 创建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();

View file

@ -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<Preference>(File.ReadAllText(SAVE_PATH));
#else
instance = JsonSerializer.Deserialize<Preference>(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

View file

@ -1,10 +1,5 @@
using System.Collections.Generic;
#if NET48
using Newtonsoft.Json;
#else
using System.Text.Json.Serialization;
#endif
namespace SourceGit.Models {

View file

@ -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信息格式
/// </summary>
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<Version>(raw);
#else
var ver = JsonSerializer.Deserialize<Version>(raw);
#endif
var cur = Assembly.GetExecutingAssembly().GetName().Version;
var matches = Regex.Match(ver.TagName, @"^v(\d+)\.(\d+).*");

View file

@ -20,6 +20,6 @@
<AssemblyName>SourceGit</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>
</Project>