From 5d511f310d74f2ac950ee711d896d77d7896fc2d Mon Sep 17 00:00:00 2001 From: Timothy Miller Date: Tue, 10 Feb 2026 19:04:17 +0900 Subject: [PATCH] Use JsonContext everywhere for netstandard --- src/CarpaNet.SourceGen/LexiconGenerator.cs | 10 ---------- src/CarpaNet/Http/XrpcHttpHandler.cs | 9 ++------- src/CarpaNet/Identity/DidDocument.cs | 17 ----------------- src/CarpaNet/OAuthSession.cs | 13 ++----------- 4 files changed, 4 insertions(+), 45 deletions(-) diff --git a/src/CarpaNet.SourceGen/LexiconGenerator.cs b/src/CarpaNet.SourceGen/LexiconGenerator.cs index 280e541..e069008 100644 --- a/src/CarpaNet.SourceGen/LexiconGenerator.cs +++ b/src/CarpaNet.SourceGen/LexiconGenerator.cs @@ -674,8 +674,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator { var sb = new SourceBuilder(); sb.WriteHeader(); - sb.AppendLine("#if NET8_0_OR_GREATER"); - sb.AppendLine(); var generatedTypes = new HashSet(); var collectedPropertyTypes = new HashSet(); @@ -845,7 +843,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator sb.CloseBrace(); // class sb.AppendLine(); - sb.AppendLine("#endif"); return (sb.ToString(), concreteTypes); } @@ -865,8 +862,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator var sb = new SourceBuilder(); sb.WriteHeader(); - sb.AppendLine("#if NET8_0_OR_GREATER"); - sb.AppendLine(); // Group types by namespace var byNamespace = concreteTypes @@ -928,8 +923,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator } } - sb.AppendLine("#endif"); - return sb.ToString(); } @@ -1134,8 +1127,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator { var sb = new SourceBuilder(); sb.WriteHeader(); - sb.AppendLine("#if NET8_0_OR_GREATER"); - sb.AppendLine(); sb.WriteUsing("System"); sb.WriteUsing("System.Collections.Generic"); sb.WriteUsing("System.Net.Http"); @@ -1246,7 +1237,6 @@ public sealed class LexiconGenerator : IIncrementalGenerator sb.CloseBrace(); // class sb.AppendLine(); - sb.AppendLine("#endif"); return sb.ToString(); } diff --git a/src/CarpaNet/Http/XrpcHttpHandler.cs b/src/CarpaNet/Http/XrpcHttpHandler.cs index 72b4ed7..6c19427 100644 --- a/src/CarpaNet/Http/XrpcHttpHandler.cs +++ b/src/CarpaNet/Http/XrpcHttpHandler.cs @@ -5,9 +5,7 @@ using System.Net; using System.Net.Http; using System.Text; using System.Text.Json; -#if NET8_0_OR_GREATER using System.Text.Json.Serialization.Metadata; -#endif using System.Threading; using System.Threading.Tasks; using CarpaNet.Xrpc; @@ -119,12 +117,8 @@ internal static class XrpcHttpHandler if (input != null) { -#if NET8_0_OR_GREATER var typeInfo = (JsonTypeInfo)jsonOptions.GetTypeInfo(typeof(TInput)); var json = JsonSerializer.Serialize(input, typeInfo); -#else - var json = JsonSerializer.Serialize(input, jsonOptions); -#endif request.Content = new StringContent(json, Encoding.UTF8, ContentTypeJson); } @@ -183,7 +177,8 @@ internal static class XrpcHttpHandler var result = await JsonSerializer.DeserializeAsync(content, typeInfo, cancellationToken).ConfigureAwait(false); #else var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - var result = JsonSerializer.Deserialize(content, jsonOptions); + var typeInfo = (JsonTypeInfo)jsonOptions.GetTypeInfo(typeof(TOutput)); + var result = JsonSerializer.Deserialize(content, typeInfo); #endif if (result == null) diff --git a/src/CarpaNet/Identity/DidDocument.cs b/src/CarpaNet/Identity/DidDocument.cs index 2eb6a77..90ada17 100644 --- a/src/CarpaNet/Identity/DidDocument.cs +++ b/src/CarpaNet/Identity/DidDocument.cs @@ -116,13 +116,8 @@ public sealed class DidDocument /// public static DidDocument FromJson(string json) { -#if NET8_0_OR_GREATER return JsonSerializer.Deserialize(json, DidDocumentJsonContext.Default.DidDocument) ?? throw new InvalidOperationException("Failed to parse DID document"); -#else - return JsonSerializer.Deserialize(json, JsonOptions) - ?? throw new InvalidOperationException("Failed to parse DID document"); -#endif } /// @@ -130,20 +125,8 @@ public sealed class DidDocument /// public string ToJson() { -#if NET8_0_OR_GREATER return JsonSerializer.Serialize(this, DidDocumentJsonContext.Default.DidDocument); -#else - return JsonSerializer.Serialize(this, JsonOptions); -#endif } - -#if !NET8_0_OR_GREATER - private static readonly JsonSerializerOptions JsonOptions = new() - { - PropertyNameCaseInsensitive = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; -#endif } /// diff --git a/src/CarpaNet/OAuthSession.cs b/src/CarpaNet/OAuthSession.cs index ef08fbe..27d1380 100644 --- a/src/CarpaNet/OAuthSession.cs +++ b/src/CarpaNet/OAuthSession.cs @@ -2,9 +2,7 @@ using System; using System.Collections.Generic; using System.Net.Http; using System.Text.Json; -#if NET8_0_OR_GREATER using System.Text.Json.Serialization.Metadata; -#endif using System.Threading; using System.Threading.Tasks; using CarpaNet; @@ -124,12 +122,8 @@ public sealed class OAuthSession : IATProtoClient, IDisposable if (input != null) { -#if NET8_0_OR_GREATER var typeInfo = (JsonTypeInfo)_jsonOptions.GetTypeInfo(typeof(TInput)); var json = JsonSerializer.Serialize(input, typeInfo); -#else - var json = JsonSerializer.Serialize(input, _jsonOptions); -#endif request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); } @@ -152,12 +146,8 @@ public sealed class OAuthSession : IATProtoClient, IDisposable if (input != null) { -#if NET8_0_OR_GREATER var typeInfo = (JsonTypeInfo)_jsonOptions.GetTypeInfo(typeof(TInput)); var json = JsonSerializer.Serialize(input, typeInfo); -#else - var json = JsonSerializer.Serialize(input, _jsonOptions); -#endif request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); } @@ -281,7 +271,8 @@ public sealed class OAuthSession : IATProtoClient, IDisposable var result = await JsonSerializer.DeserializeAsync(content, typeInfo, cancellationToken).ConfigureAwait(false); #else var contentString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - var result = JsonSerializer.Deserialize(contentString, _jsonOptions); + var typeInfo = (JsonTypeInfo)_jsonOptions.GetTypeInfo(typeof(TOutput)); + var result = JsonSerializer.Deserialize(contentString, typeInfo); #endif if (result == null) -- knotmirror 0.0.1