Skip to content

Commit b424269

Browse files
[Resources.AWS] Use System.Text.Json source generator with .NET Standard 2.0 (open-telemetry#2185)
Co-authored-by: joegoldman2 <[email protected]> Co-authored-by: Mikel Blanchard <[email protected]>
1 parent 4ab83e0 commit b424269

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/OpenTelemetry.Resources.AWS/AWSEBSDetector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ internal static List<KeyValuePair<string, object>> ExtractResourceAttributes(AWS
8484

8585
internal static AWSEBSMetadataModel? GetEBSMetadata(string filePath)
8686
{
87-
#if NET
88-
return ResourceDetectorUtils.DeserializeFromFile(filePath, SourceGenerationContext.Default.AWSEBSMetadataModel);
89-
#else
87+
#if NETFRAMEWORK
9088
return ResourceDetectorUtils.DeserializeFromFile<AWSEBSMetadataModel>(filePath);
89+
#else
90+
return ResourceDetectorUtils.DeserializeFromFile(filePath, SourceGenerationContext.Default.AWSEBSMetadataModel);
9191
#endif
9292
}
9393
}

src/OpenTelemetry.Resources.AWS/AWSEC2Detector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ internal static List<KeyValuePair<string, object>> ExtractResourceAttributes(AWS
8383

8484
internal static AWSEC2IdentityDocumentModel? DeserializeResponse(string response)
8585
{
86-
#if NET
87-
return ResourceDetectorUtils.DeserializeFromString(response, SourceGenerationContext.Default.AWSEC2IdentityDocumentModel);
88-
#else
86+
#if NETFRAMEWORK
8987
return ResourceDetectorUtils.DeserializeFromString<AWSEC2IdentityDocumentModel>(response);
88+
#else
89+
return ResourceDetectorUtils.DeserializeFromString(response, SourceGenerationContext.Default.AWSEC2IdentityDocumentModel);
9090
#endif
9191
}
9292

src/OpenTelemetry.Resources.AWS/ResourceDetectorUtils.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#endif
77
using System.Text;
88
using System.Text.Json;
9-
#if NET
9+
#if !NETFRAMEWORK
1010
using System.Text.Json.Serialization.Metadata;
1111
#endif
1212

@@ -17,7 +17,7 @@ namespace OpenTelemetry.Resources.AWS;
1717
/// </summary>
1818
internal static class ResourceDetectorUtils
1919
{
20-
#if !NET
20+
#if NETFRAMEWORK
2121
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);
2222
#endif
2323

@@ -47,31 +47,31 @@ internal static async Task<string> SendOutRequestAsync(
4747
}
4848
}
4949

50-
#if NET
51-
internal static T? DeserializeFromFile<T>(string filePath, JsonTypeInfo<T> jsonTypeInfo)
50+
#if NETFRAMEWORK
51+
internal static T? DeserializeFromFile<T>(string filePath)
5252
{
5353
using (var stream = GetStream(filePath))
5454
{
55-
return JsonSerializer.Deserialize(stream, jsonTypeInfo);
55+
return JsonSerializer.Deserialize<T>(stream, JsonSerializerOptions);
5656
}
5757
}
5858

59-
internal static T? DeserializeFromString<T>(string json, JsonTypeInfo<T> jsonTypeInfo)
59+
internal static T? DeserializeFromString<T>(string json)
6060
{
61-
return JsonSerializer.Deserialize(json, jsonTypeInfo);
61+
return JsonSerializer.Deserialize<T>(json, JsonSerializerOptions);
6262
}
6363
#else
64-
internal static T? DeserializeFromFile<T>(string filePath)
64+
internal static T? DeserializeFromFile<T>(string filePath, JsonTypeInfo<T> jsonTypeInfo)
6565
{
6666
using (var stream = GetStream(filePath))
6767
{
68-
return JsonSerializer.Deserialize<T>(stream, JsonSerializerOptions);
68+
return JsonSerializer.Deserialize(stream, jsonTypeInfo);
6969
}
7070
}
7171

72-
internal static T? DeserializeFromString<T>(string json)
72+
internal static T? DeserializeFromString<T>(string json, JsonTypeInfo<T> jsonTypeInfo)
7373
{
74-
return JsonSerializer.Deserialize<T>(json, JsonSerializerOptions);
74+
return JsonSerializer.Deserialize(json, jsonTypeInfo);
7575
}
7676
#endif
7777

src/OpenTelemetry.Resources.AWS/SourceGenerationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#if NET
4+
#if !NETFRAMEWORK
55
using System.Text.Json.Serialization;
66
using OpenTelemetry.Resources.AWS.Models;
77

0 commit comments

Comments
 (0)