Ditch Newtonsoft.Json in favor of System.Text.Json

This commit is contained in:
Sardelka9515
2023-03-19 15:13:24 +08:00
parent 0cb1098819
commit 713e005975
22 changed files with 56 additions and 76 deletions

View File

@ -1,9 +1,9 @@
using Newtonsoft.Json; using RageCoop.Core.Scripting;
using RageCoop.Core.Scripting;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RageCoop.Client.Scripting namespace RageCoop.Client.Scripting
@ -14,7 +14,7 @@ namespace RageCoop.Client.Scripting
GetStream = GetStreamMethod; GetStream = GetStreamMethod;
} }
[JsonProperty] [JsonInclude]
public string FullPath { get; internal set; } public string FullPath { get; internal set; }
Stream GetStreamMethod() Stream GetStreamMethod()
{ {

View File

@ -1,11 +1,11 @@
using Newtonsoft.Json; using RageCoop.Core.Scripting;
using RageCoop.Core.Scripting;
using SHVDN; using SHVDN;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RageCoop.Client.Scripting namespace RageCoop.Client.Scripting
@ -17,25 +17,25 @@ namespace RageCoop.Client.Scripting
/// <summary> /// <summary>
/// Name of the resource /// Name of the resource
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public string Name { get; internal set; } public string Name { get; internal set; }
/// <summary> /// <summary>
/// Directory where the scripts is loaded from /// Directory where the scripts is loaded from
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public string ScriptsDirectory { get; internal set; } public string ScriptsDirectory { get; internal set; }
/// <summary> /// <summary>
/// A resource-specific folder that can be used to store your files. /// A resource-specific folder that can be used to store your files.
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public string DataFolder { get; internal set; } public string DataFolder { get; internal set; }
/// <summary> /// <summary>
/// Get the <see cref="ClientFile" /> where this script is loaded from. /// Get the <see cref="ClientFile" /> where this script is loaded from.
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public Dictionary<string, ClientFile> Files { get; internal set; } = new Dictionary<string, ClientFile>(); public Dictionary<string, ClientFile> Files { get; internal set; } = new Dictionary<string, ClientFile>();
/// <summary> /// <summary>

View File

@ -5,7 +5,6 @@ using GTA;
using GTA.Native; using GTA.Native;
using GTA.UI; using GTA.UI;
using LemonUI.Menus; using LemonUI.Menus;
using Newtonsoft.Json;
using RageCoop.Core; using RageCoop.Core;

View File

@ -7,7 +7,6 @@ using System.Net.Http;
using System.Threading; using System.Threading;
using GTA.UI; using GTA.UI;
using LemonUI.Menus; using LemonUI.Menus;
using Newtonsoft.Json;
using RageCoop.Client.Scripting; using RageCoop.Client.Scripting;
using RageCoop.Core; using RageCoop.Core;

View File

@ -1,11 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text.Json.Serialization;
using GTA; using GTA;
using GTA.Math; using GTA.Math;
using GTA.Native; using GTA.Native;
using Lidgren.Network; using Lidgren.Network;
using Newtonsoft.Json;
using RageCoop.Client.Menus; using RageCoop.Client.Menus;
using RageCoop.Client.Scripting; using RageCoop.Client.Scripting;
using RageCoop.Core; using RageCoop.Core;

View File

@ -37,7 +37,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NAudio" Version="2.1.0" /> <PackageReference Include="NAudio" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.0" /> <PackageReference Include="SharpZipLib" Version="1.4.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.3.0" /> <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.3.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,4 @@
using Newtonsoft.Json; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;

View File

@ -1,6 +1,5 @@
#undef DEBUG #undef DEBUG
using Lidgren.Network; using Lidgren.Network;
using Newtonsoft.Json;
using RageCoop.Client.Menus; using RageCoop.Client.Menus;
using RageCoop.Core; using RageCoop.Core;
using RageCoop.Core.Scripting; using RageCoop.Core.Scripting;

View File

@ -8,7 +8,6 @@ using GTA.Math;
using GTA.Native; using GTA.Native;
using GTA.UI; using GTA.UI;
using LemonUI.Elements; using LemonUI.Elements;
using Newtonsoft.Json;
using RageCoop.Core; using RageCoop.Core;
using static RageCoop.Client.Shared; using static RageCoop.Client.Shared;
using Font = GTA.UI.Font; using Font = GTA.UI.Font;

View File

@ -3,7 +3,6 @@ using System.IO;
using GTA; using GTA;
using GTA.Math; using GTA.Math;
using GTA.Native; using GTA.Native;
using Newtonsoft.Json;
using RageCoop.Client.Scripting; using RageCoop.Client.Scripting;
using RageCoop.Core; using RageCoop.Core;

View File

@ -12,11 +12,11 @@ using System.Runtime.InteropServices;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Xml; using System.Xml;
using GTA; using GTA;
using GTA.Math; using GTA.Math;
using Lidgren.Network; using Lidgren.Network;
using Newtonsoft.Json;
using RageCoop.Core.Scripting; using RageCoop.Core.Scripting;
[assembly: InternalsVisibleTo("RageCoop.Server")] [assembly: InternalsVisibleTo("RageCoop.Server")]
@ -390,9 +390,9 @@ namespace RageCoop.Core
internal class IpInfo internal class IpInfo
{ {
[JsonProperty("ip")] public string Address { get; set; } [JsonPropertyName("ip")] public string Address { get; set; }
[JsonProperty("country")] public string Country { get; set; } [JsonPropertyName("country")] public string Country { get; set; }
} }
internal static class Extensions internal static class Extensions

View File

@ -1,58 +1,54 @@
using Newtonsoft.Json.Linq; using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RageCoop.Core namespace RageCoop.Core
{ {
class IPAddressConverter : JsonConverter class IPAddressConverter : JsonConverter<IPAddress>
{ {
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return (objectType == typeof(IPAddress)); return objectType == typeof(IPAddress);
} }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) public override IPAddress Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
writer.WriteValue(value.ToString()); if (reader.TokenType == JsonTokenType.Null) return null;
return IPAddress.Parse(reader.GetString());
} }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options)
{ {
if (reader.TokenType == JsonToken.Null) return null; writer.WriteStringValue(value?.ToString());
return IPAddress.Parse((string)reader.Value);
} }
} }
class IPEndPointConverter : JsonConverter class IPEndPointConverter : JsonConverter<IPEndPoint>
{ {
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return (objectType == typeof(IPEndPoint)); return objectType == typeof(IPEndPoint);
}
public override IPEndPoint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return null;
var jo = JsonNode.Parse(ref reader);
return new IPEndPoint(IPAddress.Parse(jo["Address"].ToString()), int.Parse(jo["Port"].ToString()));
} }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) public override void Write(Utf8JsonWriter writer, IPEndPoint value, JsonSerializerOptions options)
{ {
IPEndPoint ep = (IPEndPoint)value; new JsonObject()
JObject jo = new()
{ {
{ "Address", JToken.FromObject(ep.Address, serializer) }, { "Address", value.Address?.ToString() },
{ "Port", ep.Port } { "Port", value.Port }
}; }.WriteTo(writer);
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
JObject jo = JObject.Load(reader);
IPAddress address = jo["Address"].ToObject<IPAddress>(serializer);
int port = (int)jo["Port"];
return new IPEndPoint(address, port);
} }
} }

View File

@ -19,7 +19,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.0" /> <PackageReference Include="SharpZipLib" Version="1.4.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,5 @@
using Newtonsoft.Json; using System.Runtime.InteropServices;
using System.Runtime.InteropServices; using System.Text.Json.Serialization;
namespace RageCoop.Core.Scripting namespace RageCoop.Core.Scripting
{ {
@ -44,10 +44,10 @@ namespace RageCoop.Core.Scripting
[JsonIgnore] [JsonIgnore]
private CustomEventHandlerDelegate _managedHandler; // Used to keep GC reference private CustomEventHandlerDelegate _managedHandler; // Used to keep GC reference
[JsonProperty] [JsonInclude]
public ulong FunctionPtr { get; private set; } public ulong FunctionPtr { get; private set; }
[JsonProperty] [JsonInclude]
public string Directory { get; private set; } public string Directory { get; private set; }
/// <summary> /// <summary>

View File

@ -1,6 +1,6 @@
using Newtonsoft.Json; using System;
using System;
using System.IO; using System.IO;
using System.Text.Json.Serialization;
namespace RageCoop.Core.Scripting namespace RageCoop.Core.Scripting
{ {
@ -11,13 +11,13 @@ namespace RageCoop.Core.Scripting
/// <summary> /// <summary>
/// Full name with relative path of this file /// Full name with relative path of this file
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public string Name { get; internal set; } public string Name { get; internal set; }
/// <summary> /// <summary>
/// Whether this is a directory /// Whether this is a directory
/// </summary> /// </summary>
[JsonProperty] [JsonInclude]
public bool IsDirectory { get; internal set; } public bool IsDirectory { get; internal set; }
/// <summary> /// <summary>

View File

@ -1,8 +1,7 @@
global using static RageCoop.Core.Shared; global using static RageCoop.Core.Shared;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System; using System;
using System.Reflection; using System.Reflection;
using System.Text.Json;
namespace RageCoop.Core namespace RageCoop.Core
{ {
@ -24,22 +23,23 @@ namespace RageCoop.Core
JsonTypeCheck(obj?.GetType()); JsonTypeCheck(obj?.GetType());
return obj; return obj;
} }
public static readonly JsonSerializerSettings JsonSettings = new(); public static readonly JsonSerializerOptions JsonSettings = new();
static Shared() static Shared()
{ {
JsonSettings.Converters.Add(new IPAddressConverter()); JsonSettings.Converters.Add(new IPAddressConverter());
JsonSettings.Converters.Add(new IPEndPointConverter()); JsonSettings.Converters.Add(new IPEndPointConverter());
JsonSettings.Formatting = Formatting.Indented; JsonSettings.WriteIndented = true;
JsonSettings.IncludeFields = true;
} }
public static object JsonDeserialize(string text, Type type) public static object JsonDeserialize(string text, Type type)
{ {
return JsonConvert.DeserializeObject(text, JsonTypeCheck(type), JsonSettings); return JsonSerializer.Deserialize(text, JsonTypeCheck(type), JsonSettings);
} }
public static T JsonDeserialize<T>(string text) => (T)JsonDeserialize(text, typeof(T)); public static T JsonDeserialize<T>(string text) => (T)JsonDeserialize(text, typeof(T));
public static string JsonSerialize(object obj) => JsonConvert.SerializeObject(JsonTypeCheck(obj), JsonSettings); public static string JsonSerialize(object obj) => JsonSerializer.Serialize(JsonTypeCheck(obj), JsonSettings);
/// <summary> /// <summary>
/// Shortcut to <see cref="BufferReader.ThreadLocal"/> /// Shortcut to <see cref="BufferReader.ThreadLocal"/>

View File

@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Xml; using System.Xml;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;
namespace RageCoop.Core namespace RageCoop.Core
{ {

View File

@ -9,7 +9,6 @@ using System.Text;
using System.Threading; using System.Threading;
using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip;
using Lidgren.Network; using Lidgren.Network;
using Newtonsoft.Json;
using RageCoop.Core; using RageCoop.Core;
namespace RageCoop.Server; namespace RageCoop.Server;

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Net;
using System.Threading; using System.Threading;
using RageCoop.Core; using RageCoop.Core;

View File

@ -60,7 +60,6 @@
</PackageReference> </PackageReference>
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.4.0" /> <PackageReference Include="McMaster.NETCore.Plugins" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.8" /> <PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.0" /> <PackageReference Include="SharpZipLib" Version="1.4.0" />
</ItemGroup> </ItemGroup>

View File

@ -8,10 +8,6 @@
<OutDir>$(SolutionDir)bin\Tools\DataDumper</OutDir> <OutDir>$(SolutionDir)bin\Tools\DataDumper</OutDir>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Core\RageCoop.Core.csproj" /> <ProjectReference Include="..\..\Core\RageCoop.Core.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,8 +1,7 @@
using System.Text; global using static RageCoop.Core.Shared;
using System.Text;
using System.Xml; using System.Xml;
using Newtonsoft.Json;
using RageCoop.Core; using RageCoop.Core;
using Formatting = Newtonsoft.Json.Formatting;
namespace DataDumper; namespace DataDumper;
@ -33,7 +32,7 @@ public static class Program
{ {
Dictionary<uint, WeaponInfo> weapons = new(); Dictionary<uint, WeaponInfo> weapons = new();
foreach (var f in Directory.GetFiles("meta", "*.meta")) Parse(f, weapons); foreach (var f in Directory.GetFiles("meta", "*.meta")) Parse(f, weapons);
File.WriteAllText("out/Weapons.json", JsonConvert.SerializeObject(weapons, Formatting.Indented)); File.WriteAllText("out/Weapons.json", JsonSerialize(weapons));
if (ToGenerate.HasFlag(GenFlags.WeaponHash)) DumpWeaponHash(weapons, true); if (ToGenerate.HasFlag(GenFlags.WeaponHash)) DumpWeaponHash(weapons, true);
} }