Replace JSON.NET with System.Text.json (#840)

This commit is contained in:
RobertBeekman authored and GitHub committed 2024-02-28 19:31:38 +01:00
1 parent e112ca9da8
commit a340f8822e
63 files changed
+466 -584

No files matched your search

@@ -1,32 +0,0 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Artemis.Core.JsonConverters;
/// <summary>
/// An int converter that, if required, will round float values
/// </summary>
internal class ForgivingIntConverter : JsonConverter<int>
{
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, int value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override int ReadJson(JsonReader reader, Type objectType, int existingValue, bool hasExistingValue, JsonSerializer serializer)
{
JValue? jsonValue = serializer.Deserialize<JValue>(reader);
if (jsonValue == null)
throw new JsonReaderException("Failed to deserialize forgiving int value");
if (jsonValue.Type == JTokenType.Float)
return (int) Math.Round(jsonValue.Value<double>());
if (jsonValue.Type == JTokenType.Integer)
return jsonValue.Value<int>();
throw new JsonReaderException("Failed to deserialize forgiving int value");
}
}
@@ -1,26 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
namespace Artemis.Core.JsonConverters
{
/// <summary>
/// Version converter that is forgiving of missing parts of the version string,
/// setting them to zero instead of -1.
/// </summary>
internal class ForgivingVersionConverter : VersionConverter
{
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
object? obj = base.ReadJson(reader, objectType, existingValue, serializer);
if (obj is not Version v)
return obj;
int major = v.Major == -1 ? 0 : v.Major;
int minor = v.Minor == -1 ? 0 : v.Minor;
int build = v.Build == -1 ? 0 : v.Build;
int revision = v.Revision == -1 ? 0 : v.Revision;
return new Version(major, minor, build, revision);
}
}
}
@@ -1,24 +1,26 @@
using System;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Artemis.Core.JsonConverters;
internal class NumericJsonConverter : JsonConverter<Numeric>
namespace Artemis.Core.JsonConverters
{
#region Overrides of JsonConverter<Numeric>
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, Numeric value, JsonSerializer serializer)
internal class NumericJsonConverter : JsonConverter<Numeric>
{
float floatValue = value;
writer.WriteValue(floatValue);
}
public override void Write(Utf8JsonWriter writer, Numeric value, JsonSerializerOptions options)
{
float floatValue = value;
writer.WriteNumberValue(floatValue);
}
/// <inheritdoc />
public override Numeric ReadJson(JsonReader reader, Type objectType, Numeric existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return new Numeric(reader.Value);
}
public override Numeric Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.Number)
{
throw new JsonException($"Expected a number token, but got {reader.TokenType}.");
}
#endregion
float floatValue = reader.GetSingle();
return new Numeric(floatValue);
}
}
}
@@ -1,21 +1,26 @@
using System;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using SkiaSharp;
namespace Artemis.Core.JsonConverters;
internal class SKColorConverter : JsonConverter<SKColor>
namespace Artemis.Core.JsonConverters
{
public override void WriteJson(JsonWriter writer, SKColor value, JsonSerializer serializer)
internal class SKColorConverter : JsonConverter<SKColor>
{
writer.WriteValue(value.ToString());
}
public override void Write(Utf8JsonWriter writer, SKColor value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
public override SKColor ReadJson(JsonReader reader, Type objectType, SKColor existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value is string value && !string.IsNullOrWhiteSpace(value))
return SKColor.Parse(value);
public override SKColor Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException($"Expected a string token, but got {reader.TokenType}.");
}
return SKColor.Empty;
string colorString = reader.GetString() ?? string.Empty;
return SKColor.TryParse(colorString, out SKColor color) ? color : SKColor.Empty;
}
}
}
@@ -1,44 +0,0 @@
using System;
using System.IO;
using Newtonsoft.Json;
namespace Artemis.Core.JsonConverters;
/// <inheritdoc />
public class StreamConverter : JsonConverter<Stream>
{
#region Overrides of JsonConverter<Stream>
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, Stream? value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
using MemoryStream memoryStream = new();
value.Position = 0;
value.CopyTo(memoryStream);
writer.WriteValue(memoryStream.ToArray());
}
/// <inheritdoc />
public override Stream? ReadJson(JsonReader reader, Type objectType, Stream? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value is not string base64)
return null;
if (existingValue == null || !hasExistingValue || !existingValue.CanRead)
return new MemoryStream(Convert.FromBase64String(base64));
using MemoryStream memoryStream = new(Convert.FromBase64String(base64));
existingValue.Position = 0;
memoryStream.CopyTo(existingValue);
existingValue.Position = 0;
return existingValue;
}
#endregion
}