Replace Newtonsoft.Json with System.Text.Json in dotnet-user-secrets#65686
Conversation
…tool Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com>
|
@dotnet/aspnet-build, please provide feedback on if it is important to preserve Newtonsoft's default behavior of writing non-ASCII characters literally. Should we use the default System.Text.Json escaping? See https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-10-0#minimal-character-escaping for details. |
There was a problem hiding this comment.
Pull request overview
This PR removes the Newtonsoft.Json dependency from dotnet-user-secrets in favor of System.Text.Json, part of a broader SDK migration effort (dotnet/sdk#53287). The output format is preserved by using WriteIndented = true and JavaScriptEncoder.UnsafeRelaxedJsonEscaping to match Newtonsoft's default behavior of emitting non-ASCII characters literally.
Changes:
ListCommand.csandSecretsStore.csreplaceJObject/Formatting.IndentedwithJsonSerializer.Serializeusing equivalent options.dotnet-user-secrets.csprojremoves the<Reference Include="Newtonsoft.Json" />item.ListCommandTest.csadds unit tests covering JSON format, non-ASCII preservation,//BEGIN///ENDmarkers, and empty store output.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Internal/ListCommand.cs |
Replaces JObject + Newtonsoft formatting with JsonSerializer using UnsafeRelaxedJsonEscaping |
src/Internal/SecretsStore.cs |
Replaces JObject build loop and Newtonsoft.Json.Linq with direct JsonSerializer.Serialize(_secrets, options) |
src/dotnet-user-secrets.csproj |
Removes the Newtonsoft.Json framework reference |
test/ListCommandTest.cs |
Adds new unit tests for the --json output format to guard against regressions |
…s static field Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com>
|
@wtgodbe - Can you take a look at this? Also would like feedback on #65686 (comment). TIA |
I think the default System.Text.Json escaping should be fine. We usually use S.T.J to read these secrets via a |
…CII characters Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/48edeec7-7ad3-4117-ba13-1fcb361f202f
…ndTest Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/c4d3cc42-9115-4247-afd0-515bbd2bcfaf
…on plumbing Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/10b33337-7f61-4287-a7c5-84cb4e85797a
…re.Load() disk reading Co-authored-by: MichaelSimons <8290530+MichaelSimons@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/aspnetcore/sessions/f9992d14-6c0c-41cf-baa1-e0da01e46226
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Replaces the
Newtonsoft.Jsondependency indotnet-user-secretswithSystem.Text.Json, using the default STJ character escaping (non-ASCII characters are Unicode-escaped rather than written literally). Consolidates list command tests fromSecretManagerTestsinto the newListCommandTest, and adds integration tests to cover--jsonCLI option plumbing and nested JSON flattening behavior.Description
<Reference Include="Newtonsoft.Json" />fromdotnet-user-secrets.csproj.JObject/Formatting.IndentedinListCommand.csandSecretsStore.cswithJsonSerializer.SerializeusingWriteIndented = trueand the default STJ encoder (non-ASCII characters are Unicode-escaped).JsonSerializerOptionsinto astatic readonly SecretsStore.SerializerOptionsfield reused by bothSecretsStore.Save()andListCommand.ReportJson().ListCommandTestwith unit tests covering JSON output format, non-ASCII Unicode escaping,//BEGIN///ENDmarkers, empty store JSON output, non-JSONkey = valueformat, and non-JSON empty store error message.List_Flattens_Nested_Objects,List_Json, andList_Empty_Secrets_FilefromSecretManagerTests(all now covered byListCommandTestunit tests).List_Json_OptionIsWiredintegration test toSecretManagerTeststo verify that the--jsonCLI option is correctly wired throughCommandLineOptions.Parse→ListCommand.Configure→ListCommand.Execute.List_Flattens_Nested_Objectsintegration test toSecretManagerTeststo verify that nested JSON secrets files on disk (e.g.,{ "AzureAd": { "ClientSecret": "..." } }) are correctly flattened tokey:subkeyformat bySecretsStore.Load().💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.