-
Notifications
You must be signed in to change notification settings - Fork 546
Switch more over to System.Text.Json #9819
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7fda7e5
Switch more over to System.Text.Json
dellis1972 a763c57
Remove Newtonsoft.Json package reference
dellis1972 e9ac79d
Formatting
dellis1972 7d5fba5
Formatting
dellis1972 b0f9e6e
Fix some build errors
dellis1972 07ebf8a
Try JsonSerializerContext
dellis1972 9e3bdab
back out copilot changes
dellis1972 b219dcf
Back out more nullable changes
dellis1972 45f7b62
Remove redundant attributes
dellis1972 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, is System.Text.Json in the pack?
It would be needed for .NET framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea. Probably. We need to figure out how to get #9727 and #8746 working so we can test under .NET framework to catch all this stuff.