Commit 9419d16
authored
Newtonsoft.Json → System.Text.Json Migration (#9956)
# Summary of changes
## 1. Core Shared Infrastructure (1 file)
- **JExtensions.cs** (`src/Shared/JExtensions.cs`) — **Most significant
change.** This shared file (linked into multiple projects) was the
central Newtonsoft.Json helper. Completely rewritten to use
`System.Text.Json.Nodes` (`JsonNode`, `JsonObject`, `JsonArray`,
`JsonValue`) instead of `JObject`/`JToken`/`JArray`. Key changes:
- All extension methods now operate on
`JsonObject`/`JsonNode`/`JsonArray` instead of Newtonsoft types
- `ParseJsonNode()` / `ParseJsonObject()` helpers with
`JsonDocumentOptions` configured for comment handling and trailing
commas
- `ToJsonString()` for serialization using `JsonSerializer`
- String/int/bool/enum/array/object accessor methods (`ToString()`,
`ToInt32()`, `Get<T>()`, etc.) rewritten for STJ APIs
- `JsonNodeComparer` and `JsonNodeEqualityComparer` utility classes
added
## 2. Package References (5 files)
- **Directory.Packages.props** — Removed `Newtonsoft.Json` package
reference; added `System.Text.Json` and `JsonSchema.Net` (for JSON
schema validation in tests).
- 4 csproj files — Replaced `Newtonsoft.Json` with `System.Text.Json`,
conditioned with `Condition="'$(TargetFrameworkIdentifier)' !=
'.NETCoreApp'"` to avoid NU1510 package pruning warnings
(System.Text.Json is inbox for .NET 10+, but still needed for
`net472`/`netstandard2.0`):
-
`src/Microsoft.TemplateEngine.Edge/Microsoft.TemplateEngine.Edge.csproj`
-
`src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/Microsoft.TemplateEngine.Orchestrator.RunnableProjects.csproj`
-
`src/Microsoft.TemplateEngine.Utils/Microsoft.TemplateEngine.Utils.csproj`
-
`src/Microsoft.TemplateSearch.Common/Microsoft.TemplateSearch.Common.csproj`
- **Microsoft.TemplateSearch.TemplateDiscovery.csproj** — Removed
`Newtonsoft.Json` reference.
- **RunnableProjects.UnitTests.csproj** — Replaced `Newtonsoft.Json`
with `JsonSchema.Net` for schema validation tests.
## 3. Production Source Files (62 files)
### Microsoft.TemplateEngine.Edge (6 files)
- All converted from `JObject`/`JToken` to `JsonObject`/`JsonNode`
### Microsoft.TemplateEngine.Orchestrator.RunnableProjects (32 files)
### Microsoft.TemplateSearch.Common (11 files)
### Tools/TemplateDiscovery (12 files)
### JsonEncodeValueFormFactory.cs
Added `JavaScriptEncoder.UnsafeRelaxedJsonEscaping` so `"` encodes as
`\"` (matching Newtonsoft behavior) instead of STJ's default `\u0022`.
## 4. Test Files (24 files)
All test files updated to use `System.Text.Json` APIs instead of
Newtonsoft:
## 5. Test Data Fixes (4 files)
Removed duplicate `$schema` properties from template.json files that
Newtonsoft silently tolerated but STJ/JsonSchema.Net rejects:
-
`test_templates/TemplateConditionalProcessing/.template.config/template.json`
-
`test_templates/Invalid/InvalidHostData/.template.config/template.json`
-
`test_templates/SourceWithExcludeAndWithout/With/.template.config/template.json`
-
`test_templates/SourceWithExcludeAndWithout/Without/.template.config/template.json`
## 6. Behavioral Differences Addressed
| Issue | Newtonsoft Behavior | STJ Behavior | Fix Applied |
|-------|-------------------|--------------|-------------|
| Single-quoted JSON | Allowed | Rejected | Fixed test data to use
double quotes |
| Unquoted property names | Allowed | Rejected | Fixed test data to use
quoted names |
| Boolean serialization | `"True"` / `"False"` | `"true"` / `"false"` |
Updated test assertions |
| `"` encoding in JSON strings | `\"` | `\u0022` | Added
`UnsafeRelaxedJsonEscaping` |
| Duplicate JSON keys | Last value wins | Throws `ArgumentException` |
Removed duplicates from test data |
| `JsonNode.ToString()` format | Indented | Indented (but
`ToJsonString()` is compact) | Used `ToJsonString()` in assertions |
## 7. Validation
[VMR
build](https://dev.azure.com/dnceng/internal/_build/results?buildId=2922641&view=results)113 files changed
Lines changed: 1174 additions & 1083 deletions
File tree
- src
- Microsoft.TemplateEngine.Edge
- BuiltInManagedProvider
- Constraints
- Settings
- Microsoft.TemplateEngine.Orchestrator.RunnableProjects
- ConfigModel
- Macros
- OperationConfig
- ValueForms
- Microsoft.TemplateEngine.Utils
- Microsoft.TemplateSearch.Common
- TemplateDiscoveryMetadata
- TemplateSearchCache
- test
- Microsoft.TemplateEngine.Authoring.CLI.IntegrationTests
- Microsoft.TemplateEngine.Edge.UnitTests
- Microsoft.TemplateEngine.Mocks
- Microsoft.TemplateEngine.Orchestrator.RunnableProjects.UnitTests
- MacroTests
- SchemaTests
- Serialization
- TemplateConfigTests
- Microsoft.TemplateEngine.TestTemplates/test_templates
- Invalid/InvalidHostData/.template.config
- SourceWithExcludeAndWithout
- Without/.template.config
- With/.template.config
- TemplateConditionalProcessing/.template.config
- Microsoft.TemplateSearch.Common.UnitTests
- Microsoft.TemplateSearch.TemplateDiscovery.IntegrationTests
- tools/Microsoft.TemplateSearch.TemplateDiscovery
- AdditionalData
- Filters
- NuGet
- PackChecking
- Results
- TestProvider
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
| 43 | + | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
| |||
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
65 | | - | |
| 64 | + | |
66 | 65 | | |
67 | 66 | | |
68 | 67 | | |
| |||
Lines changed: 9 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
8 | | - | |
9 | | - | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | | - | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | | - | |
89 | | - | |
90 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
95 | 96 | | |
96 | | - | |
| 97 | + | |
97 | 98 | | |
98 | | - | |
| 99 | + | |
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | | - | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
Lines changed: 25 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | | - | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | | - | |
| 29 | + | |
29 | 30 | | |
30 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
34 | 41 | | |
35 | | - | |
| 42 | + | |
36 | 43 | | |
37 | 44 | | |
38 | 45 | | |
| |||
42 | 49 | | |
43 | 50 | | |
44 | 51 | | |
45 | | - | |
| 52 | + | |
46 | 53 | | |
47 | | - | |
48 | | - | |
| 54 | + | |
| 55 | + | |
49 | 56 | | |
50 | 57 | | |
51 | 58 | | |
52 | | - | |
| 59 | + | |
53 | 60 | | |
54 | 61 | | |
55 | 62 | | |
| |||
103 | 110 | | |
104 | 111 | | |
105 | 112 | | |
106 | | - | |
| 113 | + | |
107 | 114 | | |
108 | 115 | | |
109 | 116 | | |
110 | 117 | | |
111 | 118 | | |
112 | 119 | | |
113 | | - | |
| 120 | + | |
114 | 121 | | |
115 | 122 | | |
116 | | - | |
| 123 | + | |
117 | 124 | | |
118 | 125 | | |
119 | 126 | | |
120 | 127 | | |
121 | 128 | | |
122 | 129 | | |
123 | | - | |
| 130 | + | |
124 | 131 | | |
125 | 132 | | |
126 | | - | |
| 133 | + | |
127 | 134 | | |
128 | | - | |
| 135 | + | |
129 | 136 | | |
130 | 137 | | |
131 | 138 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
Lines changed: 26 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | | - | |
7 | | - | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
| 21 | + | |
21 | 22 | | |
22 | | - | |
| 23 | + | |
23 | 24 | | |
24 | | - | |
| 25 | + | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | | - | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | | - | |
| 35 | + | |
35 | 36 | | |
36 | | - | |
| 37 | + | |
37 | 38 | | |
38 | | - | |
| 39 | + | |
39 | 40 | | |
40 | | - | |
| 41 | + | |
41 | 42 | | |
42 | | - | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
48 | | - | |
| 49 | + | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
| 58 | + | |
58 | 59 | | |
59 | | - | |
| 60 | + | |
60 | 61 | | |
61 | | - | |
| 62 | + | |
62 | 63 | | |
63 | | - | |
| 64 | + | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
75 | | - | |
| 76 | + | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | | - | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| |||
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | | - | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
0 commit comments