-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJsonCreateDefaultRegistryEnricher.cs
42 lines (38 loc) · 2.08 KB
/
JsonCreateDefaultRegistryEnricher.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
using System;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Yardarm.Enrichment.Registration;
using Yardarm.SystemTextJson.Internal;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Yardarm.SystemTextJson
{
public class JsonCreateDefaultRegistryEnricher : ReturnValueRegistrationEnricher, ICreateDefaultRegistryEnricher
{
private readonly IJsonSerializationNamespace _jsonSerializationNamespace;
public JsonCreateDefaultRegistryEnricher(IJsonSerializationNamespace jsonSerializationNamespace)
{
ArgumentNullException.ThrowIfNull(jsonSerializationNamespace);
_jsonSerializationNamespace = jsonSerializationNamespace;
}
protected override ExpressionSyntax EnrichReturnValue(ExpressionSyntax target) =>
// Don't use the Add<T> overload here because it will cause trimming to retain
// all constructors. This will then cause IL2026 warnings if trimming is enabled.
// Instead create a new instance directly using the default constructor and add it.
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
target,
IdentifierName("Add")),
ArgumentList(SeparatedList(
[
Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
_jsonSerializationNamespace.JsonTypeSerializer,
IdentifierName("SupportedMediaTypes"))),
Argument(ObjectCreationExpression(_jsonSerializationNamespace.JsonTypeSerializer,
ArgumentList(SingletonSeparatedList(
Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
QualifiedName(_jsonSerializationNamespace.Name, IdentifierName(JsonSerializerContextGenerator.TypeName)),
IdentifierName("Default"))))),
null))
])));
}
}