You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix JSON deserialization errors in C# 'REST Client' tutorial. Closes#49387
Fix the deserialization issue in the Repository model.
* Refactor Repository class to use record definition
Updated the Repository class definition to use a record type with additional properties for JSON deserialization.
* Add JSON property names to Repository record
* Remove LastPush property from model
Removed LastPush property and its implementation.
* Update console-webapiclient.md
* Update documentation text for Repository section
* Change step numbers
* Update console-webapiclient.md for JSON deserialization
Clarified JSON property name handling and added note on case insensitivity of GetFromJsonAsync.
* Update docs/csharp/tutorials/console-webapiclient.md
Co-authored-by: Bill Wagner <[email protected]>
* Update docs/csharp/tutorials/console-webapiclient.md
Co-authored-by: Bill Wagner <[email protected]>
---------
Co-authored-by: Bill Wagner <[email protected]>
Inthefollowingsteps, weextendthecodetoprocessmorepropertiesfromtheJSONpayloadreturnedbytheGitHubAPI. Youprobablywon't need to process every property, but adding a few demonstrates additional C# features.
213
213
214
-
1. Replacethecontentsof `Repository` class, with the following `record` definition:
214
+
1. Replacethecontentsofthe `Repository` classwith the following `record` definition. Make sure to import the `System.Text.Json.Serialization` namespace and apply the `[JsonPropertyName]` attribute to map JSON fields to C# properties explicitly.
The <xref:System.Uri> and `int` types have built-in functionality to convert to and from string representation. No extra code is needed to deserialize from JSON string format to those target types. If the JSON packet contains data that doesn't convert to a target type, the serialization action throws an exception.
228
230
229
-
JSONmostoftenuseslowercasefor names of it's objects, however we don't need to make any conversion and can keep the uppercase of the fields names, because, like mentioned in one of previous points, the `GetFromJsonAsync` extension method is case-insensitive when it comes to property names.
231
+
JSON often uses `lowercase` or `snake_case` for property names. Fields like `html_url` and `pushed_at` do not follow C# PascalCase naming conventions. Using `[JsonPropertyName]` ensures that these JSON keys are correctly bound to their corresponding C# properties, even when their names differ in case or contain underscores. This approach guarantees predictable and stable deserialization while allowing PascalCase property names in C#.
232
+
Additionally, the `GetFromJsonAsync` method is `case-insensitive` when matching property names, so no further conversion is necessary.
230
233
231
-
1. Update the `foreach` loop in the *Program.cs* file to display the property values:
234
+
2. Update the `foreach` loop in the *Program.cs* file to display the property values:
232
235
233
236
```csharp
234
237
foreach (varrepoinrepositories)
@@ -242,7 +245,7 @@ The following steps add code to process more of the properties in the received J
0 commit comments