-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] Remove use of Newtonsoft.Json (#9819)
Fixes: #9229 Remove use of Newtonsoft.Json from `Xamarin.Android.Build.Tasks.dll`. Notes: * `JsonElement` is read only. This is why we convert them to `JsonNode`. * `JsonNode.Parse()` does NOT have the same features as `JsonDocument.Parse()`, e.g you CANNOT tell it to ignore trailing comma's; it will just throw an exception. This is the reason why we have to load the `.json` via `JsonDocument` and then call the extension method `.ToNode ()` on the Root `JsonElement` to convert it to a `JsonNode`. (We use `JsonNode` to do the merging).
- Loading branch information
1 parent
cf62814
commit 21e69b4
Showing
6 changed files
with
122 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
87 changes: 87 additions & 0 deletions
87
src/Xamarin.Android.Build.Tasks/Utilities/JsonExtensions.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
public static class JsonExtensions | ||
{ | ||
public static JsonNode Merge (this JsonNode jsonBase, JsonNode jsonMerge) | ||
{ | ||
if (jsonBase == null || jsonMerge == null) | ||
return jsonBase; | ||
|
||
switch (jsonBase) | ||
{ | ||
case JsonObject jsonBaseObj when jsonMerge is JsonObject jsonMergeObj: { | ||
var mergeNodesArray = new KeyValuePair<string, JsonNode?> [jsonMergeObj.Count]; | ||
int index = 0; | ||
foreach (var prop in jsonMergeObj) { | ||
mergeNodesArray [index++] = prop; | ||
} | ||
jsonMergeObj.Clear (); | ||
|
||
foreach (var prop in mergeNodesArray) { | ||
jsonBaseObj [prop.Key] = jsonBaseObj [prop.Key] switch { | ||
JsonObject jsonBaseChildObj when prop.Value is JsonObject jsonMergeChildObj => jsonBaseChildObj.Merge (jsonMergeChildObj), | ||
JsonArray jsonBaseChildArray when prop.Value is JsonArray jsonMergeChildArray => jsonBaseChildArray.Merge (jsonMergeChildArray), | ||
_ => prop.Value | ||
}; | ||
} | ||
break; | ||
} | ||
case JsonArray jsonBaseArray when jsonMerge is JsonArray jsonMergeArray: { | ||
var mergeNodesArray = new JsonNode? [jsonMergeArray.Count]; | ||
int index = 0; | ||
foreach (var mergeNode in jsonMergeArray) { | ||
mergeNodesArray [index++] = mergeNode; | ||
} | ||
jsonMergeArray.Clear (); | ||
foreach (var mergeNode in mergeNodesArray) { | ||
jsonBaseArray.Add (mergeNode); | ||
} | ||
break; | ||
} | ||
default: | ||
throw new ArgumentException ($"The JsonNode type [{jsonBase.GetType ().Name}] is incompatible for merging with the target/base " + | ||
$"type [{jsonMerge.GetType ().Name}]; merge requires the types to be the same."); | ||
} | ||
return jsonBase; | ||
} | ||
|
||
public static JsonNode? ToNode (this JsonElement element) | ||
{ | ||
switch (element.ValueKind) { | ||
case JsonValueKind.Object: | ||
var obj = new JsonObject (); | ||
foreach (JsonProperty prop in element.EnumerateObject()) { | ||
obj [prop.Name] = prop.Value.ToNode (); | ||
} | ||
return obj; | ||
|
||
case JsonValueKind.Array: | ||
var arr = new JsonArray(); | ||
foreach (JsonElement item in element.EnumerateArray ()) { | ||
arr.Add (item.ToNode ()); | ||
} | ||
return arr; | ||
|
||
case JsonValueKind.String: | ||
return element.GetString (); | ||
|
||
case JsonValueKind.Number: | ||
return element.TryGetInt32 (out int intValue) ? intValue : element.GetDouble (); | ||
|
||
case JsonValueKind.True: | ||
return true; | ||
|
||
case JsonValueKind.False: | ||
return false; | ||
|
||
case JsonValueKind.Null: | ||
return null; | ||
|
||
default: | ||
throw new NotSupportedException ($"Unsupported JSON value kind: {element.ValueKind}"); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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