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