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

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