Skip to content

Commit 7fda7e5

Browse files
committed
Switch more over to System.Text.Json
1 parent 1d1bf33 commit 7fda7e5

File tree

3 files changed

+78
-21
lines changed

3 files changed

+78
-21
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/BuildAppBundle.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.Build.Framework;
22
using Microsoft.Build.Utilities;
3-
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
3+
using System.Text.Json;
4+
using System.Text.Json.Nodes;
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
@@ -88,26 +88,24 @@ public override bool RunTask ()
8888
}
8989
}
9090

91-
var json = JObject.FromObject (new { });
91+
JsonNode json = JsonNode.Parse("{}");
9292
if (!string.IsNullOrEmpty (CustomBuildConfigFile) && File.Exists (CustomBuildConfigFile)) {
93-
using (StreamReader file = File.OpenText (CustomBuildConfigFile))
94-
using (JsonTextReader reader = new JsonTextReader (file)) {
95-
json = (JObject)JToken.ReadFrom(reader);
96-
}
93+
using Stream fs = File.OpenRead (CustomBuildConfigFile);
94+
json = JsonNode.Parse (fs);
9795
}
98-
var jsonAddition = JObject.FromObject (new {
96+
var jsonAddition = new {
9997
compression = new {
10098
uncompressedGlob = uncompressed,
10199
}
102-
});
103-
104-
var mergeSettings = new JsonMergeSettings () {
105-
MergeArrayHandling = MergeArrayHandling.Union,
106-
MergeNullValueHandling = MergeNullValueHandling.Ignore
107100
};
108-
json.Merge (jsonAddition, mergeSettings);
109-
Log.LogDebugMessage ("BundleConfig.json: {0}", json);
110-
File.WriteAllText (temp, json.ToString ());
101+
102+
var jsonAdditionDoc = JsonSerializer.SerializeToNode(jsonAddition);
103+
104+
var mergedJson = json.Merge(jsonAdditionDoc);
105+
var output = mergedJson.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
106+
107+
Log.LogDebugMessage ("BundleConfig.json: {0}", output);
108+
File.WriteAllText (temp, output);
111109

112110
//NOTE: bundletool will not overwrite
113111
if (File.Exists (Output))

src/Xamarin.Android.Build.Tasks/Tasks/JavaDependencyVerification.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
using Microsoft.Android.Build.Tasks;
1212
using Microsoft.Build.Framework;
1313
using Microsoft.Build.Utilities;
14-
using Newtonsoft.Json;
14+
using System.Text.Json;
15+
using System.Text.Json.Serialization;
1516
using NuGet.ProjectModel;
1617

1718
namespace Xamarin.Android.Tasks;
@@ -318,7 +319,7 @@ public MicrosoftNuGetPackageFinder (string? file, TaskLoggingHelper log)
318319

319320
try {
320321
var json = File.ReadAllText (file);
321-
package_list = JsonConvert.DeserializeObject<PackageListFile> (json);
322+
package_list = JsonSerializer.Deserialize<PackageListFile> (json);
322323
} catch (Exception ex) {
323324
log.LogMessage ("There was an error reading 'microsoft-packages.json', Android NuGet suggestions will not be provided: {0}", ex);
324325
}
@@ -331,16 +332,16 @@ public MicrosoftNuGetPackageFinder (string? file, TaskLoggingHelper log)
331332

332333
public class PackageListFile
333334
{
334-
[JsonProperty ("packages")]
335+
[JsonPropertyName ("packages")]
335336
public List<Package>? Packages { get; set; }
336337
}
337338

338339
public class Package
339340
{
340-
[JsonProperty ("javaId")]
341+
[JsonPropertyName ("javaId")]
341342
public string? JavaId { get; set; }
342343

343-
[JsonProperty ("nugetId")]
344+
[JsonPropertyName ("nugetId")]
344345
public string? NuGetId { get; set; }
345346
}
346347
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.Json;
4+
using System.Text.Json.Nodes;
5+
6+
public static class JsonExtensions
7+
{
8+
public static JsonNode Merge(this JsonNode jsonBase, JsonNode jsonMerge)
9+
{
10+
if (jsonBase == null || jsonMerge == null)
11+
return jsonBase;
12+
13+
switch (jsonBase)
14+
{
15+
case JsonObject jsonBaseObj when jsonMerge is JsonObject jsonMergeObj:
16+
{
17+
var mergeNodesArray = new KeyValuePair<string, JsonNode?>[jsonMergeObj.Count];
18+
int index = 0;
19+
foreach (var prop in jsonMergeObj)
20+
{
21+
mergeNodesArray[index++] = prop;
22+
}
23+
jsonMergeObj.Clear();
24+
25+
foreach (var prop in mergeNodesArray)
26+
{
27+
jsonBaseObj[prop.Key] = jsonBaseObj[prop.Key] switch
28+
{
29+
JsonObject jsonBaseChildObj when prop.Value is JsonObject jsonMergeChildObj => jsonBaseChildObj.Merge(jsonMergeChildObj),
30+
JsonArray jsonBaseChildArray when prop.Value is JsonArray jsonMergeChildArray => jsonBaseChildArray.Merge(jsonMergeChildArray),
31+
_ => prop.Value
32+
};
33+
}
34+
break;
35+
}
36+
case JsonArray jsonBaseArray when jsonMerge is JsonArray jsonMergeArray:
37+
{
38+
var mergeNodesArray = new JsonNode?[jsonMergeArray.Count];
39+
int index = 0;
40+
foreach (var mergeNode in jsonMergeArray)
41+
{
42+
mergeNodesArray[index++] = mergeNode;
43+
}
44+
jsonMergeArray.Clear();
45+
foreach (var mergeNode in mergeNodesArray)
46+
{
47+
jsonBaseArray.Add(mergeNode);
48+
}
49+
break;
50+
}
51+
default:
52+
throw new ArgumentException($"The JsonNode type [{jsonBase.GetType().Name}] is incompatible for merging with the target/base " +
53+
$"type [{jsonMerge.GetType().Name}]; merge requires the types to be the same.");
54+
}
55+
56+
return jsonBase;
57+
}
58+
}

0 commit comments

Comments
 (0)