|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections; |
5 | 6 | using System.Collections.Generic; |
6 | 7 | using System.IO; |
7 | 8 | using System.Linq; |
8 | | -using System.Text.Encodings.Web; |
9 | | -using System.Text.Json; |
10 | | -using System.Text.Json.Serialization; |
11 | 9 | using Coverlet.Core.Abstractions; |
12 | 10 | using Coverlet.Core.Helpers; |
13 | 11 | using Coverlet.Core.Instrumentation; |
14 | 12 | using Coverlet.Core.Symbols; |
15 | 13 | using Moq; |
| 14 | +using Newtonsoft.Json; |
16 | 15 | using Xunit; |
17 | 16 |
|
18 | 17 | namespace Coverlet.Core.Tests |
19 | 18 | { |
20 | 19 | public partial class CoverageTests |
21 | 20 | { |
22 | 21 | private readonly Mock<ILogger> _mockLogger = new(); |
23 | | - readonly JsonSerializerOptions _options = new() |
24 | | - { |
25 | | - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
26 | | - IncludeFields = true, |
27 | | - WriteIndented = true, |
28 | | - Converters = |
29 | | - { |
30 | | - new BranchDictionaryConverterFactory() |
31 | | - } |
32 | | - }; |
33 | 22 |
|
34 | 23 | [Fact] |
35 | 24 | public void TestCoverage() |
@@ -102,7 +91,7 @@ public void TestCoverageWithTestAssembly() |
102 | 91 | new SourceRootTranslator(module, _mockLogger.Object, new FileSystem(), new AssemblyAdapter()), new CecilSymbolHelper()); |
103 | 92 | coverage.PrepareModules(); |
104 | 93 |
|
105 | | - string result = JsonSerializer.Serialize(coverage.GetCoverageResult(), _options); |
| 94 | + string result = JsonConvert.SerializeObject(coverage.GetCoverageResult(), Formatting.Indented, new BranchDictionaryConverter()); |
106 | 95 |
|
107 | 96 | Assert.Contains("coverlet.core.tests.dll", result); |
108 | 97 |
|
@@ -141,7 +130,7 @@ public void TestCoverageMergeWithParameter() |
141 | 130 | var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), parameters, _mockLogger.Object, instrumentationHelper, new FileSystem(), new SourceRootTranslator(_mockLogger.Object, new FileSystem()), new CecilSymbolHelper()); |
142 | 131 | coverage.PrepareModules(); |
143 | 132 |
|
144 | | - string result = JsonSerializer.Serialize(coverage.GetCoverageResult(), _options); |
| 133 | + string result = JsonConvert.SerializeObject(coverage.GetCoverageResult(), Formatting.Indented, new BranchDictionaryConverter()); |
145 | 134 |
|
146 | 135 | Assert.Contains("DeepThought.cs", result); |
147 | 136 |
|
@@ -182,51 +171,44 @@ public void TestCoverageMergeWithWrongParameter() |
182 | 171 | var coverage = new Coverage(Path.Combine(directory.FullName, Path.GetFileName(module)), parameters, _mockLogger.Object, instrumentationHelper, new FileSystem(), new SourceRootTranslator(_mockLogger.Object, new FileSystem()), new CecilSymbolHelper()); |
183 | 172 | coverage.PrepareModules(); |
184 | 173 |
|
185 | | - string result = JsonSerializer.Serialize(coverage.GetCoverageResult(), _options); |
| 174 | + JsonConvert.SerializeObject(coverage.GetCoverageResult()); |
186 | 175 |
|
187 | 176 | _mockLogger.Verify(l => l.LogInformation(It.Is<string>(v => v.Equals("MergeWith: file 'FileDoesNotExist.json' does not exist.")), It.IsAny<bool>()), Times.Once); |
188 | 177 |
|
189 | 178 | directory.Delete(true); |
190 | 179 | } |
191 | 180 | } |
192 | 181 | } |
193 | | -public class BranchDictionaryConverterFactory : JsonConverterFactory |
194 | | -{ |
195 | | - public override bool CanConvert(Type typeToConvert) |
196 | | - { |
197 | | - return typeof(Dictionary<BranchKey, Branch>).IsAssignableFrom(typeToConvert); |
198 | | - } |
199 | 182 |
|
200 | | - public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| 183 | +public class BranchDictionaryConverter: JsonConverter |
| 184 | +{ |
| 185 | + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
201 | 186 | { |
202 | | - Type[] genericArgs = typeToConvert.GetGenericArguments(); |
203 | | - Type keyType = genericArgs[0]; |
204 | | - Type valueType = genericArgs[1]; |
| 187 | + Type type = value.GetType(); |
| 188 | + var keys = (IEnumerable)type.GetProperty("Keys")?.GetValue(value, null); |
| 189 | + var values = (IEnumerable)type.GetProperty("Values")?.GetValue(value, null); |
| 190 | + IEnumerator valueEnumerator = values.GetEnumerator(); |
205 | 191 |
|
206 | | - JsonConverter converter = (JsonConverter)Activator.CreateInstance( |
207 | | - typeof(BranchDictionaryConverter<,>).MakeGenericType(new Type[] { keyType, valueType })); |
| 192 | + writer.WriteStartArray(); |
| 193 | + foreach (object key in keys) |
| 194 | + { |
| 195 | + valueEnumerator.MoveNext(); |
208 | 196 |
|
209 | | - return converter; |
| 197 | + writer.WriteStartArray(); |
| 198 | + serializer.Serialize(writer, key); |
| 199 | + serializer.Serialize(writer, valueEnumerator.Current); |
| 200 | + writer.WriteEndArray(); |
| 201 | + } |
| 202 | + writer.WriteEndArray(); |
210 | 203 | } |
211 | | -} |
212 | 204 |
|
213 | | -public class BranchDictionaryConverter<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>> |
214 | | -{ |
215 | | - public override Dictionary<TKey, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 205 | + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) |
216 | 206 | { |
217 | 207 | throw new NotImplementedException(); |
218 | 208 | } |
219 | 209 |
|
220 | | - public override void Write(Utf8JsonWriter writer, Dictionary<TKey, TValue> value, JsonSerializerOptions options) |
| 210 | + public override bool CanConvert(Type objectType) |
221 | 211 | { |
222 | | - writer.WriteStartObject(); |
223 | | - |
224 | | - foreach (KeyValuePair<TKey, TValue> pair in value) |
225 | | - { |
226 | | - writer.WritePropertyName(pair.Key.ToString()); |
227 | | - JsonSerializer.Serialize(writer, pair.Value, options); |
228 | | - } |
229 | | - |
230 | | - writer.WriteEndObject(); |
| 212 | + return typeof(Dictionary<BranchKey, Branch>).IsAssignableFrom(objectType); |
231 | 213 | } |
232 | 214 | } |
0 commit comments