-
Notifications
You must be signed in to change notification settings - Fork 731
/
TagTransformerJsonHelper.cs
53 lines (46 loc) · 1.67 KB
/
TagTransformerJsonHelper.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#nullable enable
using System.Text.Json;
#if NET6_0_OR_GREATER
using System.Text.Json.Serialization;
#endif
namespace OpenTelemetry.Internal;
/// <summary>
/// This class has to be partial so that JSON source generator can provide code for the JsonSerializerContext.
/// </summary>
internal static partial class TagTransformerJsonHelper
{
#if NET6_0_OR_GREATER
// In net6.0 or higher ships System.Text.Json "in box" as part of the base class libraries;
// meaning the consumer automatically got upgraded to use v6.0 System.Text.Json
// which has support for using source generators for JSON serialization.
// The source generator makes the serialization faster and also AOT compatible.
internal static string JsonSerializeArrayTag(Array array)
{
return JsonSerializer.Serialize(array, typeof(Array), ArrayTagJsonContext.Default);
}
[JsonSerializable(typeof(Array))]
[JsonSerializable(typeof(char))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(byte))]
[JsonSerializable(typeof(sbyte))]
[JsonSerializable(typeof(short))]
[JsonSerializable(typeof(ushort))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(uint))]
[JsonSerializable(typeof(long))]
[JsonSerializable(typeof(ulong))]
[JsonSerializable(typeof(float))]
[JsonSerializable(typeof(double))]
private sealed partial class ArrayTagJsonContext : JsonSerializerContext
{
}
#else
internal static string JsonSerializeArrayTag(Array array)
{
return JsonSerializer.Serialize(array);
}
#endif
}