-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathDataSerializationHelper.cs
143 lines (121 loc) · 8.15 KB
/
DataSerializationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
internal static class DataSerializationHelper
{
private static readonly ConcurrentDictionary<string, DataContractJsonSerializer> SerializerCache = new();
private static readonly DataContractJsonSerializerSettings SerializerSettings = new()
{
UseSimpleDictionaryFormat = true,
EmitTypeInformation = System.Runtime.Serialization.EmitTypeInformation.Always,
DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("O", System.Globalization.CultureInfo.InvariantCulture),
};
/// <summary>
/// Serializes the date in such a way that won't throw exceptions during deserialization in Test Platform.
/// The result can be deserialized using <see cref="Deserialize(string[])"/> method.
/// </summary>
/// <param name="data">Data array to serialize.</param>
/// <returns>Serialized array.</returns>
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
public static string?[]? Serialize(object?[]? data)
{
if (data == null)
{
return null;
}
string?[] serializedData = new string?[data.Length * 2];
for (int i = 0; i < data.Length; i++)
{
int typeIndex = i * 2;
int dataIndex = typeIndex + 1;
if (data[i] == null)
{
serializedData[typeIndex] = null;
serializedData[dataIndex] = null;
continue;
}
Type type = data[i]!.GetType();
string? typeName = type.AssemblyQualifiedName;
serializedData[typeIndex] = typeName;
DataContractJsonSerializer serializer = GetSerializer(type);
using var memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, data[i]);
byte[] serializerData = memoryStream.ToArray();
serializedData[dataIndex] = Encoding.UTF8.GetString(serializerData, 0, serializerData.Length);
}
return serializedData;
}
/// <summary>
/// Deserializes the data serialized by <see cref="Serialize(object[])" /> method.
/// </summary>
/// <param name="serializedData">Serialized data array to deserialize.</param>
/// <returns>Deserialized array.</returns>
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
public static object?[]? Deserialize(string?[]? serializedData)
{
if (serializedData == null || serializedData.Length % 2 != 0)
{
return null;
}
int length = serializedData.Length / 2;
object?[] data = new object?[length];
for (int i = 0; i < length; i++)
{
int typeIndex = i * 2;
string? assemblyQualifiedName = serializedData[typeIndex];
string? serializedValue = serializedData[typeIndex + 1];
if (serializedValue == null || assemblyQualifiedName == null)
{
data[i] = null;
continue;
}
DataContractJsonSerializer serializer = GetSerializer(assemblyQualifiedName);
byte[] serializedDataBytes = Encoding.UTF8.GetBytes(serializedValue);
using var memoryStream = new MemoryStream(serializedDataBytes);
data[i] = serializer.ReadObject(memoryStream);
}
return data;
}
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
private static DataContractJsonSerializer GetSerializer(string assemblyQualifiedName)
=> SerializerCache.GetOrAdd(
assemblyQualifiedName,
_ => new DataContractJsonSerializer(PlatformServiceProvider.Instance.ReflectionOperations.GetType(assemblyQualifiedName) ?? typeof(object), SerializerSettings));
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "This should be safe as long as our generator mentions getting fields / properties of the target type. https://github.com/dotnet/runtime/issues/71350#issuecomment-1168140551. Not the best solution, maybe we can replace this with System.Text.Json, but the we need one generator calling the other.")]
private static DataContractJsonSerializer GetSerializer(Type type)
=> SerializerCache.GetOrAdd(
type.AssemblyQualifiedName!,
_ => new DataContractJsonSerializer(type, SerializerSettings));
}