|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Drawing; |
| 3 | +using System.Linq; |
3 | 4 | using System.Reflection; |
4 | 5 | using System.Text.Json; |
5 | 6 | using System.Text.Json.Serialization; |
6 | 7 |
|
7 | 8 | using BizHawk.Client.Common; |
| 9 | +using BizHawk.Emulation.Common.Json; |
8 | 10 |
|
9 | 11 | namespace BizHawk.Tests.Client.Common.config |
10 | 12 | { |
@@ -94,5 +96,75 @@ public void TestRoundTripSerialization() |
94 | 96 | Assert.AreEqual(s, Ser(Deser(s, type)), $"{type} failed serialization round-trip"); |
95 | 97 | } |
96 | 98 | } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + [DataRow("0.8")] |
| 102 | + [DataRow("1.00000036")] |
| 103 | + [DataRow("1.8")] |
| 104 | + public void TestRoundTripSerializationFloatConverter(string floatValue) |
| 105 | + { |
| 106 | + float deserialized = JsonSerializer.Deserialize<float>(floatValue, ConfigService.SerializerOptions); |
| 107 | + string serialized = JsonSerializer.Serialize(deserialized, ConfigService.SerializerOptions); |
| 108 | + Assert.AreEqual(floatValue, serialized); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + [DataRow("[1,2,3]")] |
| 113 | + [DataRow("[]")] |
| 114 | + [DataRow("null")] |
| 115 | + [DataRow("[255,0,127,128,1]")] |
| 116 | + public void TestRoundTripSerializationByteArrayConverter(string byteArrayValue) |
| 117 | + { |
| 118 | + byte[]? deserialized = JsonSerializer.Deserialize<byte[]>(byteArrayValue, ConfigService.SerializerOptions); |
| 119 | + string serialized = JsonSerializer.Serialize(deserialized, ConfigService.SerializerOptions); |
| 120 | + Assert.AreEqual(byteArrayValue, serialized); |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void TestSerializationTypeConverter() |
| 125 | + { |
| 126 | + var color = Color.FromArgb(200, 255, 13, 42); |
| 127 | + string serialized = JsonSerializer.Serialize(color, ConfigService.SerializerOptions); |
| 128 | + Assert.AreEqual("\"200; 255; 13; 42\"", serialized); |
| 129 | + |
| 130 | + var newColor = JsonSerializer.Deserialize<Color>(serialized, ConfigService.SerializerOptions); |
| 131 | + Assert.AreEqual(color, newColor); |
| 132 | + } |
| 133 | + |
| 134 | + private static bool Equals<T>(T[,] array1, T[,] array2) |
| 135 | + { |
| 136 | + return array1.Rank == array2.Rank |
| 137 | + && Enumerable.Range(0, array1.Rank).All(dimension => array1.GetLength(dimension) == array2.GetLength(dimension)) |
| 138 | + && array1.Cast<T>().SequenceEqual(array2.Cast<T>()); |
| 139 | + } |
| 140 | + |
| 141 | + [TestMethod] |
| 142 | + public void TestSerialization2DArrayConverter() |
| 143 | + { |
| 144 | + var options = new JsonSerializerOptions |
| 145 | + { |
| 146 | + Converters = { new Array2DJsonConverter<byte>() }, |
| 147 | + }; |
| 148 | + var optionsWithByteArrayConverter = new JsonSerializerOptions |
| 149 | + { |
| 150 | + Converters = { new Array2DJsonConverter<byte>(), new ByteArrayAsNormalArrayJsonConverter() }, |
| 151 | + }; |
| 152 | + |
| 153 | + byte[,] testByteArray = |
| 154 | + { |
| 155 | + { 1, 2, 3 }, |
| 156 | + { 255, 0, 128 }, |
| 157 | + }; |
| 158 | + |
| 159 | + string serialized = JsonSerializer.Serialize(testByteArray, options); |
| 160 | + Assert.AreEqual("[\"AQID\",\"/wCA\"]", serialized); |
| 161 | + byte[,] deserialized = JsonSerializer.Deserialize<byte[,]>(serialized, options)!; |
| 162 | + Assert.IsTrue(Equals(testByteArray, deserialized)); |
| 163 | + |
| 164 | + string serialized2 = JsonSerializer.Serialize(testByteArray, optionsWithByteArrayConverter); |
| 165 | + Assert.AreEqual("[[1,2,3],[255,0,128]]", serialized2); |
| 166 | + byte[,] deserialized2 = JsonSerializer.Deserialize<byte[,]>(serialized2, optionsWithByteArrayConverter)!; |
| 167 | + Assert.IsTrue(Equals(testByteArray, deserialized2)); |
| 168 | + } |
97 | 169 | } |
98 | 170 | } |
0 commit comments