mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
Replace Newtonsoft.Json with System.Text.Json (#869)
* Converted to use `System.Text.Json` * Remove Newtonsoft.Json Dependency Newtonsoft.Json dependency has been removed in favor of System.Text.Json * Treat MGFXO file as binary
This commit is contained in:
committed by
GitHub
parent
ee0883ebb4
commit
d008b1bd41
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended.Serialization;
|
||||
|
||||
namespace MonoGame.Extended.Tests.Serialization;
|
||||
|
||||
public sealed class ColorJsonConverterTests
|
||||
{
|
||||
private readonly ColorJsonConverter _converter = new ColorJsonConverter();
|
||||
|
||||
[Fact]
|
||||
public void CanConvert_ColorType_ReturnsTrue()
|
||||
{
|
||||
var colorType = typeof(Color);
|
||||
var result = _converter.CanConvert(colorType);
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanConvert_NonColorType_ReturnsFalse()
|
||||
{
|
||||
var otherType = typeof(string);
|
||||
var result = _converter.CanConvert(otherType);
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Red", 255, 0, 0, 255)]
|
||||
[InlineData("#FF0000FF", 255, 0, 0, 255)]
|
||||
public void Read_ValidColorJson_ReturnsExpectedColor(string jsonValue, byte r, byte g, byte b, byte a)
|
||||
{
|
||||
var json = $"\"{jsonValue}\"";
|
||||
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
|
||||
|
||||
reader.Read();
|
||||
var actual = _converter.Read(ref reader, typeof(Color), new JsonSerializerOptions());
|
||||
|
||||
var expected = new Color(r, g, b, a);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_ValidColor_WritesExpectedJson()
|
||||
{
|
||||
var expected = "#ff000000";
|
||||
var color = ColorHelper.FromHex(expected);
|
||||
using var stream = new MemoryStream();
|
||||
using var writer = new Utf8JsonWriter(stream);
|
||||
|
||||
_converter.Write(writer, color, new JsonSerializerOptions());
|
||||
writer.Flush();
|
||||
var actual = Encoding.UTF8.GetString(stream.ToArray());
|
||||
|
||||
Assert.Equal($"\"{expected}\"", actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Write_NullWrier_ThrowArgumentNullException()
|
||||
{
|
||||
var color = Color.MonoGameOrange;
|
||||
Assert.Throws<ArgumentNullException>(() => _converter.Write(null, color, new JsonSerializerOptions()));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using MonoGame.Extended.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Tests.Serialization;
|
||||
@@ -18,12 +18,15 @@ public class RectangleFJsonConverterTest
|
||||
{
|
||||
var jsonData = @"
|
||||
{
|
||||
box: ""1 1 10 10""
|
||||
""box"": ""1 1 10 10""
|
||||
}
|
||||
";
|
||||
var serializer = new JsonSerializer();
|
||||
serializer.Converters.Add(new RectangleFJsonConverter());
|
||||
var content = serializer.Deserialize<TestContent>(new JsonTextReader(new StringReader(jsonData)));
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
options.Converters.Add(new RectangleFJsonConverter());
|
||||
var content = JsonSerializer.Deserialize<TestContent>(jsonData, options);
|
||||
|
||||
Assert.Equal(1, content.Box.Left);
|
||||
Assert.Equal(1, content.Box.Top);
|
||||
|
||||
Reference in New Issue
Block a user