System.Text.Json conversion for template engine project and tests#54022
Merged
NikolaMilosavljevic merged 3 commits intodotnet:mainfrom Apr 24, 2026
Merged
System.Text.Json conversion for template engine project and tests#54022NikolaMilosavljevic merged 3 commits intodotnet:mainfrom
NikolaMilosavljevic merged 3 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Migrates Microsoft.TemplateEngine.Cli and its unit tests off the transitive Newtonsoft.Json dependency and onto System.Text.Json, including replacing JObject/JToken usage with JsonNode/JsonObject and updating serialization/deserialization paths to be AOT-safe.
Changes:
- Rewrote CLI JSON helper extensions (
JExtensions) forSystem.Text.Json.Nodesand updated consumers. - Updated host-specific template data loading/serialization and CLI host cache parsing to use STJ nodes.
- Updated alias persistence and unit tests to use STJ serialization APIs.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Microsoft.TemplateEngine.Cli.UnitTests/TemplateSearchCoordinatorTests.cs | Switches cache JSON creation from JObject.FromObject to JsonSerializer.Serialize. |
| test/Microsoft.TemplateEngine.Cli.UnitTests/HostDataLoaderTests.cs | Updates serialization/assertions to use JsonSerializer.SerializeToNode and JsonObject APIs. |
| test/Microsoft.TemplateEngine.Cli.UnitTests/AliasAssignmentTests.cs | Moves test parsing/assertions from JObject/JValue to JsonNode + typed GetValue<T>(). |
| src/Cli/Microsoft.TemplateEngine.Cli/TemplateSearch/CliHostSearchCacheData.cs | Ports cache additional-data reader from JObject to JsonObject. |
| src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/PostActionProcessorBase.cs | Ports JSON parsing for targetFiles from JToken/JArray to JsonNode/JsonArray. |
| src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/ChmodPostActionProcessor.cs | Ports chmod args parsing from JArray.Parse to JsonNode.Parse().AsArray(). |
| src/Cli/Microsoft.TemplateEngine.Cli/JExtensions.cs | Replaces Newtonsoft-based helper extensions with System.Text.Json node-based helpers and file read/write. |
| src/Cli/Microsoft.TemplateEngine.Cli/HostSpecificTemplateData.cs | Ports host data model parsing and custom converter to System.Text.Json and AOT-safe writing. |
| src/Cli/Microsoft.TemplateEngine.Cli/HostSpecificDataLoader.cs | Switches host JSON reading from JsonTextReader/JObject.Load to JsonNode.Parse. |
| src/Cli/Microsoft.TemplateEngine.Cli/Alias/AliasRegistry.cs | Updates alias file read/write to use JsonObject and writes JSON via JExtensions.WriteObject. |
| src/Cli/Microsoft.TemplateEngine.Cli/Alias/AliasModel.cs | Replaces [JsonProperty] with [JsonInclude] (STJ attribute). |
MiYanni
approved these changes
Apr 24, 2026
Member
Author
|
Failures are unrelated and known issues. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Newtonsoft.Json → System.Text.Json Migration: Microsoft.TemplateEngine.Cli
Overview
Migrated
src/Cli/Microsoft.TemplateEngine.Clifrom Newtonsoft.Json (JObject,JToken,JArray, etc.) to System.Text.Json (JsonNode,JsonObject,JsonArray,JsonValue). The project has<IsAotCompatible>true</IsAotCompatible>, so all serialization avoids reflection-based APIs.Files Changed
Source (8 files)
JExtensions.csReadObject()returnsJsonObject,WriteObject()usesUtf8JsonWriter.TryParse()returnsJsonNode?. AddedGetPropertyCaseInsensitive()helper.AliasModel.cs[JsonProperty]→[JsonInclude]on public properties.AliasRegistry.csSave()buildsJsonObject/JsonArraymanually (AOT-safe).ToStringListDictionary()uses case-insensitive property lookup matching original Newtonsoft behavior.HostSpecificTemplateData.csJsonObject?. Boolean values handled viaJsonValueKind.True/Falseswitch. CustomJsonConverterrewritten withUtf8JsonWriter.UsageExamplesparsing filters byJsonValueKind.Stringbefore callingGetValue<string>().HostSpecificDataLoader.csJObject.Parse→JsonNode.Parse()?.AsObject(). Both string and stream parse paths useJsonDocumentOptionswithCommentHandling = SkipandAllowTrailingCommas = true.CliHostSearchCacheData.csJObject→JsonObject. Property detection usesHashSet<string>withOrdinalIgnoreCasefor O(1) lookup instead of nested LINQ.ChmodPostActionProcessor.csJArray.Parse→JsonNode.Parse().AsArray().PostActionProcessorBase.csJsonValueKindenum.Tests (3 files)
AliasAssignmentTests.csJObject.Parse→JsonNode.Parse. TypedGetValue<string>()/GetValue<bool>()for value extraction.HostDataLoaderTests.csJObject.FromObject→JsonSerializer.SerializeToNode. Property assertions rewritten forJsonObject.TemplateSearchCoordinatorTests.csJObject.FromObject(cache).ToString()→JsonSerializer.Serialize(cache).Notable Design Decisions
No
JsonSerializer.Serialize<T>()in production code — all serialization usesUtf8JsonWritermanual writes orJsonNode.WriteTo()to remain AOT-compatible.JsonArray.Add((JsonNode)JsonValue.Create(item)!)pattern — genericAdd<T>triggers AOT warnings; explicit cast avoids them.Case-insensitive property access — Newtonsoft property access was case-insensitive by default. STJ's
JsonObjectis case-sensitive, soGetPropertyCaseInsensitive()helpers and manual iteration were added where backward compatibility matters (e.g.,AliasRegistry.ToStringListDictionary,HostSpecificTemplateDataconstructor).JsonDocumentOptionsfor lenient parsing —HostSpecificDataLoaderandJExtensions.TryParse()explicitly opt into comment skipping and trailing commas, matching the permissive parsing behavior of Newtonsoft's defaults.Defensive value extraction —
UsageExamplesfilters elements byJsonValueKind.Stringbefore callingGetValue<string>()to avoidInvalidOperationExceptionon unexpected element types.O(1) property detection —
CliHostSearchCacheDatabuilds aHashSet<string>(StringComparer.OrdinalIgnoreCase)from cache keys once, replacing a nested O(n×m) LINQ query.Verification