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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,11 +12,11 @@ using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Xml;
using GTA;
using GTA.Math;
using Lidgren.Network;
using Newtonsoft.Json;
using RageCoop.Core.Scripting;
[assembly: InternalsVisibleTo("RageCoop.Server")]
@ -390,9 +390,9 @@ namespace RageCoop.Core
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

View File

@ -1,58 +1,54 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace RageCoop.Core
{
class IPAddressConverter : JsonConverter
class IPAddressConverter : JsonConverter<IPAddress>
{
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;
return IPAddress.Parse((string)reader.Value);
writer.WriteStringValue(value?.ToString());
}
}
class IPEndPointConverter : JsonConverter
class IPEndPointConverter : JsonConverter<IPEndPoint>
{
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;
JObject jo = new()
new JsonObject()
{
{ "Address", JToken.FromObject(ep.Address, serializer) },
{ "Port", ep.Port }
};
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);
{ "Address", value.Address?.ToString() },
{ "Port", value.Port }
}.WriteTo(writer);
}
}

View File

@ -19,7 +19,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.4.0" />
</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
{
@ -44,10 +44,10 @@ namespace RageCoop.Core.Scripting
[JsonIgnore]
private CustomEventHandlerDelegate _managedHandler; // Used to keep GC reference
[JsonProperty]
[JsonInclude]
public ulong FunctionPtr { get; private set; }
[JsonProperty]
[JsonInclude]
public string Directory { get; private set; }
/// <summary>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,8 +1,7 @@
using System.Text;
global using static RageCoop.Core.Shared;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using RageCoop.Core;
using Formatting = Newtonsoft.Json.Formatting;
namespace DataDumper;
@ -33,7 +32,7 @@ public static class Program
{
Dictionary<uint, WeaponInfo> weapons = new();
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);
}