-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core - Replaced JSON.NET with System.Text.Json
- Loading branch information
1 parent
e112ca9
commit 6d8572c
Showing
53 changed files
with
282 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Artemis.Core.JsonConverters; | ||
|
||
/// <summary> | ||
/// An int converter that, if required, will round float values | ||
/// </summary> | ||
internal class ForgivingIntConverter : JsonConverter<int> | ||
namespace Artemis.Core.JsonConverters | ||
{ | ||
public override bool CanWrite => false; | ||
|
||
public override void WriteJson(JsonWriter writer, int value, JsonSerializer serializer) | ||
/// <summary> | ||
/// An int converter that, if required, will round float values | ||
/// </summary> | ||
internal class ForgivingIntConverter : JsonConverter<int> | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
throw new JsonException("Cannot convert null value."); | ||
|
||
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 (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
if (reader.TryGetInt32(out int intValue)) | ||
return intValue; | ||
|
||
if (reader.TryGetDouble(out double doubleValue)) | ||
return (int)Math.Round(doubleValue); | ||
} | ||
|
||
if (jsonValue.Type == JTokenType.Float) | ||
return (int) Math.Round(jsonValue.Value<double>()); | ||
if (jsonValue.Type == JTokenType.Integer) | ||
return jsonValue.Value<int>(); | ||
throw new JsonException("Failed to deserialize forgiving int value"); | ||
} | ||
|
||
throw new JsonReaderException("Failed to deserialize forgiving int value"); | ||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
src/Artemis.Core/JsonConverters/ForgivingVersionConverter.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,31 @@ | ||
using System; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Artemis.Core.JsonConverters; | ||
|
||
/// <inheritdoc /> | ||
public class StreamConverter : JsonConverter<Stream> | ||
namespace Artemis.Core.JsonConverters | ||
{ | ||
#region Overrides of JsonConverter<Stream> | ||
|
||
/// <inheritdoc /> | ||
public override void WriteJson(JsonWriter writer, Stream? value, JsonSerializer serializer) | ||
internal class StreamConverter : JsonConverter<Stream> | ||
{ | ||
if (value == null) | ||
public override void Write(Utf8JsonWriter writer, Stream value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteNull(); | ||
return; | ||
using MemoryStream memoryStream = new(); | ||
value.Position = 0; | ||
value.CopyTo(memoryStream); | ||
writer.WriteBase64StringValue(memoryStream.ToArray()); | ||
} | ||
|
||
using MemoryStream memoryStream = new(); | ||
value.Position = 0; | ||
value.CopyTo(memoryStream); | ||
writer.WriteValue(memoryStream.ToArray()); | ||
} | ||
public override Stream Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.String) | ||
throw new JsonException($"Expected a string token, but got {reader.TokenType}."); | ||
|
||
/// <inheritdoc /> | ||
public override Stream? ReadJson(JsonReader reader, Type objectType, Stream? existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.Value is not string base64) | ||
return null; | ||
string base64 = reader.GetString() ?? string.Empty; | ||
|
||
if (existingValue == null || !hasExistingValue || !existingValue.CanRead) | ||
return new MemoryStream(Convert.FromBase64String(base64)); | ||
if (typeToConvert == typeof(MemoryStream)) | ||
return new MemoryStream(Convert.FromBase64String(base64)); | ||
|
||
using MemoryStream memoryStream = new(Convert.FromBase64String(base64)); | ||
existingValue.Position = 0; | ||
memoryStream.CopyTo(existingValue); | ||
existingValue.Position = 0; | ||
return existingValue; | ||
throw new InvalidOperationException("StreamConverter only supports reading to MemoryStream"); | ||
} | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.