Skip to content

Commit 8a2ad6d

Browse files
Fix JSON deserialization errors in C# 'REST Client' tutorial. Closes … (#49442)
* 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]>
1 parent 690503f commit 8a2ad6d

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

docs/csharp/tutorials/console-webapiclient.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,29 @@ The `ProcessRepositoriesAsync` method can do the async work and return a collect
209209

210210
## Deserialize more properties
211211

212-
The following steps add code to process more of the properties in the received JSON packet. You probably won't want to process every property, but adding a few more demonstrates other features of C#.
212+
In the following steps, we extend the code to process more properties from the JSON payload returned by the GitHub API. You probably won't need to process every property, but adding a few demonstrates additional C# features.
213213

214-
1. Replace the contents of `Repository` class, with the following `record` definition:
214+
1. Replace the contents of the `Repository` class with 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.
215+
216+
```csharp
217+
using System.Text.Json.Serialization;
215218

216-
```csharp
217219
public record class Repository(
218-
string Name,
219-
string Description,
220-
Uri GitHubHomeUrl,
221-
Uri Homepage,
222-
int Watchers,
223-
DateTime LastPushUtc
224-
);
225-
```
220+
string Name,
221+
string Description,
222+
[property: JsonPropertyName("html_url")] Uri GitHubHomeUrl,
223+
Uri Homepage,
224+
int Watchers,
225+
[property: JsonPropertyName("pushed_at")] DateTime LastPushUtc
226+
);
227+
```
226228

227229
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.
228230

229-
JSON most often uses lowercase for 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.
230233

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:
232235

233236
```csharp
234237
foreach (var repo in repositories)
@@ -242,7 +245,7 @@ The following steps add code to process more of the properties in the received J
242245
}
243246
```
244247

245-
1. Run the app.
248+
3. Run the app.
246249

247250
The list now includes the additional properties.
248251

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
public record class Repository(
1+
using System.Text.Json.Serialization;
2+
3+
public record class Repository(
24
string Name,
35
string Description,
4-
Uri GitHubHomeUrl,
6+
[property: JsonPropertyName("html_url")] Uri GitHubHomeUrl,
57
Uri Homepage,
68
int Watchers,
7-
DateTime LastPushUtc
8-
)
9+
[property: JsonPropertyName("pushed_at")] DateTime LastPushUtc
10+
)
911
{
1012
public DateTime LastPush => LastPushUtc.ToLocalTime();
1113
}

0 commit comments

Comments
 (0)