From dd5cd09f9b6ef8f92a92df469b4e10d635cadf59 Mon Sep 17 00:00:00 2001 From: Vineeth Thomas Alex Date: Fri, 9 Aug 2024 18:12:56 -0500 Subject: [PATCH] Fix JsonSerializerOptions --- .../LoginUI/EnterpriseServerPATPage.cs | 6 ++--- .../LoginUI/EnterpriseServerPage.cs | 8 +++---- .../LoginUI/JsonSourceGenerationContext.cs | 22 +++++++++++++++++++ .../DeveloperId/LoginUI/LoginFailedPage.cs | 2 +- .../DeveloperId/LoginUI/LoginPage.cs | 6 ++--- .../DeveloperId/LoginUI/LoginSucceededPage.cs | 2 +- .../DeveloperId/LoginUI/LoginUIPage.cs | 10 +++++++++ .../DeveloperId/LoginUI/WaitingPage.cs | 2 +- src/GitHubExtension/Helpers/Json.cs | 4 ++-- 9 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/GitHubExtension/DeveloperId/LoginUI/JsonSourceGenerationContext.cs diff --git a/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPATPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPATPage.cs index 8c80e28..bd931a8 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPATPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPATPage.cs @@ -11,7 +11,7 @@ internal sealed class EnterpriseServerPATPage : LoginUIPage public EnterpriseServerPATPage(Uri hostAddress, string errorText, SecureString inputPAT) : base(LoginUIState.EnterpriseServerPATPage) { - Data = new PageData() + Data = new EnterpriseServerPATPageData() { EnterpriseServerPATPageInputValue = new System.Net.NetworkCredential(string.Empty, inputPAT).Password ?? string.Empty, EnterpriseServerPATPageErrorValue = errorText ?? string.Empty, @@ -22,7 +22,7 @@ public EnterpriseServerPATPage(Uri hostAddress, string errorText, SecureString i }; } - internal sealed class PageData : ILoginUIPageData + internal sealed class EnterpriseServerPATPageData : ILoginUIPageData { public string EnterpriseServerPATPageInputValue { get; set; } = string.Empty; @@ -36,7 +36,7 @@ internal sealed class PageData : ILoginUIPageData public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } diff --git a/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPage.cs index b5e0990..123b322 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/EnterpriseServerPage.cs @@ -10,7 +10,7 @@ internal sealed class EnterpriseServerPage : LoginUIPage public EnterpriseServerPage(Uri? hostAddress, string errorText) : base(LoginUIState.EnterpriseServerPage) { - Data = new PageData() + Data = new EnterpriseServerPageData() { EnterpriseServerInputValue = hostAddress?.ToString() ?? string.Empty, EnterpriseServerPageErrorValue = errorText ?? string.Empty, @@ -21,7 +21,7 @@ public EnterpriseServerPage(Uri? hostAddress, string errorText) public EnterpriseServerPage(string hostAddress, string errorText) : base(LoginUIState.EnterpriseServerPage) { - Data = new PageData() + Data = new EnterpriseServerPageData() { EnterpriseServerInputValue = hostAddress, EnterpriseServerPageErrorValue = errorText ?? string.Empty, @@ -29,7 +29,7 @@ public EnterpriseServerPage(string hostAddress, string errorText) }; } - internal sealed class PageData : ILoginUIPageData + internal sealed class EnterpriseServerPageData : ILoginUIPageData { public string EnterpriseServerInputValue { get; set; } = string.Empty; @@ -40,7 +40,7 @@ internal sealed class PageData : ILoginUIPageData public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } diff --git a/src/GitHubExtension/DeveloperId/LoginUI/JsonSourceGenerationContext.cs b/src/GitHubExtension/DeveloperId/LoginUI/JsonSourceGenerationContext.cs new file mode 100644 index 0000000..618aa42 --- /dev/null +++ b/src/GitHubExtension/DeveloperId/LoginUI/JsonSourceGenerationContext.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System.Text.Json.Serialization; +using static GitHubExtension.DeveloperId.LoginUI.EnterpriseServerPage; +using static GitHubExtension.DeveloperId.LoginUI.EnterpriseServerPATPage; +using static GitHubExtension.DeveloperId.LoginUI.LoginFailedPage; +using static GitHubExtension.DeveloperId.LoginUI.LoginPage; +using static GitHubExtension.DeveloperId.LoginUI.LoginSucceededPage; +using static GitHubExtension.DeveloperId.LoginUI.WaitingPage; + +namespace GitHubExtension.DeveloperId; + +[JsonSerializable(typeof(EnterpriseServerPageData))] +[JsonSerializable(typeof(EnterpriseServerPATPageData))] +[JsonSerializable(typeof(LoginFailedPageData))] +[JsonSerializable(typeof(LoginPageData))] +[JsonSerializable(typeof(LoginSucceededPageData))] +[JsonSerializable(typeof(WaitingPageData))] +internal sealed partial class JsonSourceGenerationContext : JsonSerializerContext +{ +} diff --git a/src/GitHubExtension/DeveloperId/LoginUI/LoginFailedPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/LoginFailedPage.cs index cda9985..8d3818f 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/LoginFailedPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/LoginFailedPage.cs @@ -17,7 +17,7 @@ internal sealed class LoginFailedPageData : ILoginUIPageData { public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } } diff --git a/src/GitHubExtension/DeveloperId/LoginUI/LoginPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/LoginPage.cs index 53b542a..54dec8e 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/LoginPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/LoginPage.cs @@ -10,14 +10,14 @@ internal sealed class LoginPage : LoginUIPage public LoginPage() : base(LoginUIState.LoginPage) { - Data = new PageData(); + Data = new LoginPageData(); } - internal sealed class PageData : ILoginUIPageData + internal sealed class LoginPageData : ILoginUIPageData { public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } diff --git a/src/GitHubExtension/DeveloperId/LoginUI/LoginSucceededPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/LoginSucceededPage.cs index 511c178..2808618 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/LoginSucceededPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/LoginSucceededPage.cs @@ -23,7 +23,7 @@ internal sealed class LoginSucceededPageData : ILoginUIPageData public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } } diff --git a/src/GitHubExtension/DeveloperId/LoginUI/LoginUIPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/LoginUIPage.cs index 063cee7..1543480 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/LoginUIPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/LoginUIPage.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using System.Text.Json; +using System.Text.Json.Serialization; using GitHubExtension.Helpers; using Microsoft.Windows.DevHome.SDK; @@ -12,6 +14,14 @@ internal class LoginUIPage private readonly LoginUIState _state; private ILoginUIPageData? _data; + public static readonly JsonSerializerOptions _optionsWithContext = new() + { + PropertyNameCaseInsensitive = true, + DefaultIgnoreCondition = JsonIgnoreCondition.Never, + IncludeFields = true, + TypeInfoResolver = JsonSourceGenerationContext.Default, + }; + public interface ILoginUIPageData { public abstract string GetJson(); diff --git a/src/GitHubExtension/DeveloperId/LoginUI/WaitingPage.cs b/src/GitHubExtension/DeveloperId/LoginUI/WaitingPage.cs index d03a592..46bbf36 100644 --- a/src/GitHubExtension/DeveloperId/LoginUI/WaitingPage.cs +++ b/src/GitHubExtension/DeveloperId/LoginUI/WaitingPage.cs @@ -17,7 +17,7 @@ internal sealed class WaitingPageData : ILoginUIPageData { public string GetJson() { - return Json.Stringify(this); + return Json.Stringify(this, _optionsWithContext); } } } diff --git a/src/GitHubExtension/Helpers/Json.cs b/src/GitHubExtension/Helpers/Json.cs index eff1433..c700ad6 100644 --- a/src/GitHubExtension/Helpers/Json.cs +++ b/src/GitHubExtension/Helpers/Json.cs @@ -42,14 +42,14 @@ public static async Task StringifyAsync(T value) }); } - public static string Stringify(T value) + public static string Stringify(T value, JsonSerializerOptions? options = null) { if (typeof(T) == typeof(bool)) { return value!.ToString()!.ToLowerInvariant(); } - return System.Text.Json.JsonSerializer.Serialize(value, _options); + return System.Text.Json.JsonSerializer.Serialize(value, options ?? _options); } public static T? ToObject(string json)