Skip to content

Commit 06ec5dd

Browse files
committed
Switch more over to System.Text.Json
1 parent 1d1bf33 commit 06ec5dd

File tree

3 files changed

+93
-21
lines changed

3 files changed

+93
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
/// <summary>
9+
/// Merges the specified Json Node into the base JsonNode for which this method is called.
10+
/// It is null safe and can be easily used with null-check & null coalesce operators for fluent calls.
11+
/// NOTE: JsonNodes are context aware and track their parent relationships therefore to merge the values both JsonNode objects
12+
/// specified are mutated. The Base is mutated with new data while the source is mutated to remove reverences to all
13+
/// fields so that they can be added to the base.
14+
///
15+
/// Source taken directly from the open-source Gist here:
16+
/// https://gist.github.com/cajuncoding/bf78bdcf790782090d231590cbc2438f
17+
///
18+
/// </summary>
19+
/// <param name="jsonBase"></param>
20+
/// <param name="jsonMerge"></param>
21+
/// <returns></returns>
22+
/// <exception cref="ArgumentException"></exception>
23+
public static JsonNode Merge(this JsonNode jsonBase, JsonNode jsonMerge)
24+
{
25+
if (jsonBase == null || jsonMerge == null)
26+
return jsonBase;
27+
28+
switch (jsonBase)
29+
{
30+
case JsonObject jsonBaseObj when jsonMerge is JsonObject jsonMergeObj:
31+
{
32+
var mergeNodesArray = new KeyValuePair<string, JsonNode?>[jsonMergeObj.Count];
33+
int index = 0;
34+
foreach (var prop in jsonMergeObj)
35+
{
36+
mergeNodesArray[index++] = prop;
37+
}
38+
jsonMergeObj.Clear();
39+
40+
foreach (var prop in mergeNodesArray)
41+
{
42+
jsonBaseObj[prop.Key] = jsonBaseObj[prop.Key] switch
43+
{
44+
JsonObject jsonBaseChildObj when prop.Value is JsonObject jsonMergeChildObj => jsonBaseChildObj.Merge(jsonMergeChildObj),
45+
JsonArray jsonBaseChildArray when prop.Value is JsonArray jsonMergeChildArray => jsonBaseChildArray.Merge(jsonMergeChildArray),
46+
_ => prop.Value
47+
};
48+
}
49+
break;
50+
}
51+
case JsonArray jsonBaseArray when jsonMerge is JsonArray jsonMergeArray:
52+
{
53+
var mergeNodesArray = new JsonNode?[jsonMergeArray.Count];
54+
int index = 0;
55+
foreach (var mergeNode in jsonMergeArray)
56+
{
57+
mergeNodesArray[index++] = mergeNode;
58+
}
59+
jsonMergeArray.Clear();
60+
foreach (var mergeNode in mergeNodesArray)
61+
{
62+
jsonBaseArray.Add(mergeNode);
63+
}
64+
break;
65+
}
66+
default:
67+
throw new ArgumentException($"The JsonNode type [{jsonBase.GetType().Name}] is incompatible for merging with the target/base " +
68+
$"type [{jsonMerge.GetType().Name}]; merge requires the types to be the same.");
69+
}
70+
71+
return jsonBase;
72+
}
73+
}

0 commit comments

Comments
 (0)