|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | + |
| 11 | +namespace Macross.Json.Extensions.Tests |
| 12 | +{ |
| 13 | + [TestClass] |
| 14 | + public class JsonTypeConverterAdapterFactoryTests |
| 15 | + { |
| 16 | + [TestMethod] |
| 17 | + public void TypeConverterTest() |
| 18 | + { |
| 19 | + JsonSerializerOptions options = new JsonSerializerOptions(); |
| 20 | + options.Converters.Add(new JsonTypeConverterAdapterFactory()); |
| 21 | + |
| 22 | + TestClass testObj = new TestClass() |
| 23 | + { |
| 24 | + One = new ReferenceTest { X = 10, Y = "str" }, |
| 25 | + Many = new List<ReferenceTest> |
| 26 | + { |
| 27 | + new ReferenceTest { X = 20, Y = "abc" }, |
| 28 | + new ReferenceTest { X = 30, Y = "zyx" }, |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + string jsonExpected = "{\"One\":\"10,str\",\"Many\":[\"20,abc\",\"30,zyx\"],\"NonnullableStruct\":\"0,\",\"NullableStruct\":null}"; |
| 33 | + string json = JsonSerializer.Serialize(testObj, options); |
| 34 | + Assert.AreEqual(jsonExpected, json); |
| 35 | + |
| 36 | + TestClass? output = JsonSerializer.Deserialize<TestClass>(json, options); |
| 37 | + Assert.IsNotNull(output); |
| 38 | + |
| 39 | + Assert.AreEqual(testObj.One, output.One); |
| 40 | + CollectionAssert.AreEqual(testObj.Many, output.Many); |
| 41 | + Assert.AreEqual(testObj.NonnullableStruct, output.NonnullableStruct); |
| 42 | + Assert.AreEqual(testObj.NullableStruct, output.NullableStruct); |
| 43 | + |
| 44 | + testObj = new TestClass() |
| 45 | + { |
| 46 | + NonnullableStruct = new StructTest |
| 47 | + { |
| 48 | + A = 18, |
| 49 | + B = "str" |
| 50 | + }, |
| 51 | + NullableStruct = new StructTest |
| 52 | + { |
| 53 | + A = 18, |
| 54 | + B = "str" |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + jsonExpected = "{\"One\":null,\"Many\":null,\"NonnullableStruct\":\"18,str\",\"NullableStruct\":\"18,str\"}"; |
| 59 | + json = JsonSerializer.Serialize(testObj, options); |
| 60 | + Assert.AreEqual(jsonExpected, json); |
| 61 | + |
| 62 | + output = JsonSerializer.Deserialize<TestClass>(json, options); |
| 63 | + Assert.IsNotNull(output); |
| 64 | + Assert.AreEqual(testObj.NonnullableStruct, output.NonnullableStruct); |
| 65 | + Assert.AreEqual(testObj.NullableStruct, output.NullableStruct); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + [DataRow(typeof(ReferenceTest))] |
| 70 | + [DataRow(typeof(StructTest?))] |
| 71 | + [ExpectedException(typeof(JsonException))] |
| 72 | + public void InvalidReferenceValueTest(Type typeToConvert) |
| 73 | + { |
| 74 | + JsonSerializerOptions options = new JsonSerializerOptions(); |
| 75 | + options.Converters.Add(new JsonTypeConverterAdapterFactory()); |
| 76 | + |
| 77 | + JsonSerializer.Deserialize("1234", typeToConvert, options); |
| 78 | + } |
| 79 | + |
| 80 | + private class TestClass |
| 81 | + { |
| 82 | + public ReferenceTest? One { get; set; } |
| 83 | + |
| 84 | + public List<ReferenceTest>? Many { get; set; } |
| 85 | + |
| 86 | + public StructTest NonnullableStruct { get; set; } |
| 87 | + |
| 88 | + public StructTest? NullableStruct { get; set; } |
| 89 | + } |
| 90 | + |
| 91 | + [TypeConverter(typeof(ReferenceTypeConverter))] |
| 92 | + [JsonConverter(typeof(JsonTypeConverterAdapterFactory))] |
| 93 | + private record ReferenceTest |
| 94 | + { |
| 95 | + public int X { get; set; } |
| 96 | + |
| 97 | + public string? Y { get; set; } |
| 98 | + |
| 99 | + public override string ToString() => $"{X},{Y}"; |
| 100 | + } |
| 101 | + |
| 102 | + [TypeConverter(typeof(StructTypeConverter))] |
| 103 | + private struct StructTest : IEquatable<StructTest> |
| 104 | + { |
| 105 | + public int A { get; set; } |
| 106 | + |
| 107 | + public string? B { get; set; } |
| 108 | + |
| 109 | + public override bool Equals(object? obj) |
| 110 | + => obj is StructTest structTest && Equals(structTest); |
| 111 | + |
| 112 | + public bool Equals(StructTest other) |
| 113 | + => A == other.A && B == other.B; |
| 114 | + |
| 115 | + public override string ToString() => $"{A},{B}"; |
| 116 | + |
| 117 | + public override int GetHashCode() |
| 118 | + => HashCode.Combine(A, B); |
| 119 | + } |
| 120 | + |
| 121 | +#pragma warning disable CA1812 // Remove class never instantiated |
| 122 | + private class ReferenceTypeConverter : TypeConverter |
| 123 | +#pragma warning restore CA1812 // Remove class never instantiated |
| 124 | + { |
| 125 | + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => |
| 126 | + sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
| 127 | + |
| 128 | + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 129 | + { |
| 130 | + if (value is string str) |
| 131 | + { |
| 132 | + Match match = Regex.Match(str, @"^(\d+),((?:\w+)|$)"); |
| 133 | + return new ReferenceTest |
| 134 | + { |
| 135 | + X = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), |
| 136 | + Y = string.IsNullOrEmpty(match.Groups[2].Value) ? null : match.Groups[2].Value |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + return base.ConvertFrom(context, culture, value); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +#pragma warning disable CA1812 // Remove class never instantiated |
| 145 | + private class StructTypeConverter : TypeConverter |
| 146 | +#pragma warning restore CA1812 // Remove class never instantiated |
| 147 | + { |
| 148 | + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => |
| 149 | + sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
| 150 | + |
| 151 | + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 152 | + { |
| 153 | + if (value is string str) |
| 154 | + { |
| 155 | + Match match = Regex.Match(str, @"^(\d+),((?:\w+)|$)"); |
| 156 | + return new StructTest |
| 157 | + { |
| 158 | + A = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), |
| 159 | + B = string.IsNullOrEmpty(match.Groups[2].Value) ? null : match.Groups[2].Value |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + return base.ConvertFrom(context, culture, value); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments