8000 Convert VMR tasks from Newtonsoft.Json to System.Text.Json by Copilot · Pull Request #715 · dotnet/dotnet · GitHub
[go: up one dir, main page]

Skip to content

Convert VMR tasks from Newtonsoft.Json to System.Text.Json #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert UpdateJson.cs and WriteUsageBurndownData.cs to use System.Tex…
…t.Json

Co-authored-by: ViktorHofer <7412651+ViktorHofer@users.noreply.github.com>
  • Loading branch information
Copilot and ViktorHofer committed May 22, 2025
commit 454e20d05934abd6a71423f66792c2c935dd9aae
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="NuGet.Protocol" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="NuGet.ProjectModel" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="Newtonsoft.Json" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="System.Text.Json" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
</ItemGroup>

</Project>
29 changes: 19 additions & 10 deletions eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand Down Expand Up @@ -37,23 +37,26 @@ public override bool Execute()

string json = File.ReadAllText(JsonFilePath);
string newLineChars = FileUtilities.DetectNewLineChars(json);
JObject jsonObj = JObject.Parse(json);
JsonNode jsonNode = JsonNode.Parse(json);

string[] escapedPathToAttributeParts = PathToAttribute.Split(Delimiter);
for (int i = 0; i < escapedPathToAttributeParts.Length; ++i)
{
escapedPathToAttributeParts[i] = escapedPathToAttributeParts[i];
}
UpdateAttribute(jsonObj, escapedPathToAttributeParts, NewAttributeValue);
UpdateAttribute(jsonNode, escapedPathToAttributeParts, NewAttributeValue);

File.WriteAllText(JsonFilePath, FileUtilities.NormalizeNewLineChars(jsonObj.ToString(), newLineChars));
var options = new JsonSerializerOptions { WriteIndented = true };
File.WriteAllText(JsonFilePath, FileUtilities.NormalizeNewLineChars(jsonNode.ToJsonString(options), newLineChars));
return true;
}

private void UpdateAttribute(JToken jsonObj, string[] path, string newValue)
private void UpdateAttribute(JsonNode jsonNode, string[] path, string newValue)
{
string pathItem = path[0];
if (jsonObj[pathItem] == null)
JsonNode current = jsonNode as JsonObject;

if (current is JsonObject jsonObject && !jsonObject.ContainsKey(pathItem))
{
string message = $"Path item [{nameof(PathToAttribute)}] not found in json file.";
if (SkipUpdateIfMissingKey)
Expand All @@ -68,16 +71,22 @@ private void UpdateAttribute(JToken jsonObj, string[] path, string newValue)
{
if (newValue == null)
{
jsonObj[pathItem].Parent.Remove();
if (current is JsonObject obj && obj.ContainsKey(pathItem))
{
obj.Remove(pathItem);
}
}
else
{
jsonObj[pathItem] = newValue;
if (current is JsonObject obj)
{
obj[pathItem] = JsonValue.Create(newValue);
}
}
return;
}

UpdateAttribute(jsonObj[pathItem], path.Skip(1).ToArray(), newValue);
UpdateAttribute(((JsonObject)current)[pathItem], path.Skip(1).ToArray(), newValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@
#nullable disable

using Microsoft.Build.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down
Loading
0