-
Notifications
You must be signed in to change notification settings - Fork 668
Migrate to System.Text.Json - Phase 1: Low-risk components #16839
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
Open
Copilot
wants to merge
7
commits into
master
Choose a base branch
from
copilot/migrate-to-system-text-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+355
−70
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9289d27
Initial plan
Copilot c8467ca
Add JSON serialization helper infrastructure for System.Text.Json mig…
Copilot f4cadad
Migrate low-risk components to System.Text.Json (Phase 1)
Copilot e33d4b2
Remove unused Newtonsoft.Json usings from chart node models
Copilot bee2b54
Address code review feedback: Add JsonPropertyName to NotificationsMo…
Copilot 6c0b470
Restore Newtonsoft.Json using statements in chart files that use Json…
Copilot 66b1305
Fix deserialization issues: add null checks and case-insensitive opti…
Copilot 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
224 changes: 224 additions & 0 deletions
224
src/DynamoCore/Serialization/JsonSerializationHelper.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,224 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Dynamo.Serialization | ||
| { | ||
| /// <summary> | ||
| /// Helper class for JSON serialization using System.Text.Json. | ||
| /// Provides utilities to replace Newtonsoft.Json functionality. | ||
| /// </summary> | ||
| public static class JsonSerializationHelper | ||
| { | ||
| /// <summary> | ||
| /// Creates default JsonSerializerOptions for Dynamo serialization. | ||
| /// </summary> | ||
| /// <param name="converters">Optional custom converters to include</param> | ||
| /// <returns>Configured JsonSerializerOptions</returns> | ||
| public static JsonSerializerOptions CreateSerializerOptions(params JsonConverter[] converters) | ||
| { | ||
| var options = new JsonSerializerOptions | ||
| { | ||
| WriteIndented = true, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.Never, | ||
| ReferenceHandler = ReferenceHandler.IgnoreCycles, | ||
| PropertyNameCaseInsensitive = false, | ||
| AllowTrailingCommas = true, | ||
| ReadCommentHandling = JsonCommentHandling.Skip, | ||
| // Note: Using default encoder for security. If special characters need to be unescaped, | ||
| // evaluate security implications before changing to UnsafeRelaxedJsonEscaping. | ||
| }; | ||
|
|
||
| // Add custom converters | ||
| if (converters != null) | ||
| { | ||
| foreach (var converter in converters) | ||
| { | ||
| options.Converters.Add(converter); | ||
| } | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates JsonSerializerOptions for deserialization with backward compatibility. | ||
| /// </summary> | ||
| /// <param name="converters">Optional custom converters to include</param> | ||
| /// <returns>Configured JsonSerializerOptions</returns> | ||
| public static JsonSerializerOptions CreateDeserializerOptions(params JsonConverter[] converters) | ||
| { | ||
| var options = new JsonSerializerOptions | ||
| { | ||
| WriteIndented = false, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.Never, | ||
| ReferenceHandler = ReferenceHandler.IgnoreCycles, | ||
| PropertyNameCaseInsensitive = true, // More lenient for reading old files | ||
| AllowTrailingCommas = true, | ||
| ReadCommentHandling = JsonCommentHandling.Skip | ||
| }; | ||
|
|
||
| // Add custom converters | ||
| if (converters != null) | ||
| { | ||
| foreach (var converter in converters) | ||
| { | ||
| options.Converters.Add(converter); | ||
| } | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Safely gets a string value from a JsonElement. | ||
| /// </summary> | ||
| public static string GetStringOrDefault(JsonElement element, string defaultValue = "") | ||
| { | ||
| return element.ValueKind == JsonValueKind.String ? element.GetString() ?? defaultValue : defaultValue; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Safely gets an int value from a JsonElement. | ||
| /// </summary> | ||
| public static int GetInt32OrDefault(JsonElement element, int defaultValue = 0) | ||
| { | ||
| return element.ValueKind == JsonValueKind.Number && element.TryGetInt32(out var value) ? value : defaultValue; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Safely gets a double value from a JsonElement. | ||
| /// </summary> | ||
| public static double GetDoubleOrDefault(JsonElement element, double defaultValue = 0.0) | ||
| { | ||
| return element.ValueKind == JsonValueKind.Number && element.TryGetDouble(out var value) ? value : defaultValue; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Safely gets a bool value from a JsonElement. | ||
| /// </summary> | ||
| public static bool GetBooleanOrDefault(JsonElement element, bool defaultValue = false) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.True) return true; | ||
| if (element.ValueKind == JsonValueKind.False) return false; | ||
| return defaultValue; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Safely gets a Guid value from a JsonElement. | ||
| /// </summary> | ||
| public static Guid GetGuidOrDefault(JsonElement element, Guid defaultValue = default) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.String) | ||
| { | ||
| var str = element.GetString(); | ||
| if (Guid.TryParse(str, out var guid)) | ||
| { | ||
| return guid; | ||
| } | ||
| } | ||
| return defaultValue; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries to get a property from a JsonElement. | ||
| /// </summary> | ||
| public static bool TryGetProperty(JsonElement element, string propertyName, out JsonElement property) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Object) | ||
| { | ||
| return element.TryGetProperty(propertyName, out property); | ||
| } | ||
| property = default; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets an array of JsonElements from a property, or empty array if not found. | ||
| /// </summary> | ||
| public static JsonElement[] GetArrayOrEmpty(JsonElement element, string propertyName) | ||
| { | ||
| if (TryGetProperty(element, propertyName, out var property) && property.ValueKind == JsonValueKind.Array) | ||
| { | ||
| var list = new List<JsonElement>(); | ||
| foreach (var item in property.EnumerateArray()) | ||
| { | ||
| list.Add(item); | ||
| } | ||
| return list.ToArray(); | ||
| } | ||
| return Array.Empty<JsonElement>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Deserializes a JsonElement to a specific type using the provided options. | ||
| /// </summary> | ||
| /// <param name="element">The JsonElement to deserialize</param> | ||
| /// <param name="options">Optional serializer options</param> | ||
| /// <returns>The deserialized object, which may be null for reference types</returns> | ||
| public static T Deserialize<T>(JsonElement element, JsonSerializerOptions options = null) | ||
| { | ||
| var json = element.GetRawText(); | ||
| return JsonSerializer.Deserialize<T>(json, options); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parses a JSON string and returns a JsonDocument. | ||
| /// The caller is responsible for disposing the returned JsonDocument. | ||
| /// </summary> | ||
| /// <param name="json">The JSON string to parse</param> | ||
| /// <returns>A JsonDocument representing the parsed JSON</returns> | ||
| /// <exception cref="JsonException">Thrown when the JSON is malformed</exception> | ||
| /// <exception cref="ArgumentException">Thrown when json parameter is null or empty</exception> | ||
| public static JsonDocument ParseJson(string json) | ||
| { | ||
| if (string.IsNullOrEmpty(json)) | ||
| { | ||
| throw new ArgumentException("JSON string cannot be null or empty", nameof(json)); | ||
| } | ||
|
|
||
| return JsonDocument.Parse(json); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a JSON value with error handling. | ||
| /// </summary> | ||
| public static void WriteValue(Utf8JsonWriter writer, string propertyName, string value) | ||
| { | ||
| writer.WriteString(propertyName, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a JSON value with error handling. | ||
| /// </summary> | ||
| public static void WriteValue(Utf8JsonWriter writer, string propertyName, int value) | ||
| { | ||
| writer.WriteNumber(propertyName, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a JSON value with error handling. | ||
| /// </summary> | ||
| public static void WriteValue(Utf8JsonWriter writer, string propertyName, double value) | ||
| { | ||
| writer.WriteNumber(propertyName, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a JSON value with error handling. | ||
| /// </summary> | ||
| public static void WriteValue(Utf8JsonWriter writer, string propertyName, bool value) | ||
| { | ||
| writer.WriteBoolean(propertyName, value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes a JSON value with error handling. | ||
| /// </summary> | ||
| public static void WriteValue(Utf8JsonWriter writer, string propertyName, Guid value) | ||
| { | ||
| writer.WriteString(propertyName, value.ToString()); | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
The
Deserialize<T>method does not handle the case whereJsonSerializer.Deserialize<T>returns null for reference types. This could lead to unexpected null returns. Consider documenting this behavior or adding validation.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.
Fixed in commit 66b1305. Updated documentation to clarify that Deserialize may return null for reference types when deserialization fails.