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
Refactor, improve the WebAPIClient tutorial page to use GetFromJsonAsync instead of DeserializeAsync (#48202)
* Use Net.Http.Json extension method GetFromJsonAsync instead of JsonSerializer
* Remove configuring the deserialization
* Capitalize the Name property
* Update Repository class
* Remove unused Text.Json.Serialization
* Correct the orders of points
* WriteLine instead of Write in early phase to improve output readability
* Revert "Correct the orders of points"
This reverts commit bb708ac.
* Remove leftover sentence
* Mention that GetFromJsonAsync is case-insensitive
This is mentioned in two places - with the C# naming convention, where
it's worth to mention why we keep on having uppercase, and in the Repository
class defining, where we would normally had to make a conversion, but
now we don't because we have case-insensitive method as a tool.
Thefollowingstepssimplifytheapproachtofetchingthedataandprocessingit. Youwillusethe <xref:System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsync%2A>extensionmethodthat's part of the [📦 System.Net.Http.Json](https://www.nuget.org/packages/System.Net.Http.Json) NuGet package to fetch and deserialize the JSON results into objects.
TheC# conventionisto [capitalizethefirstletterofpropertynames](../../standard/design-guidelines/capitalization-conventions.md), butthe `name` propertyherestartswithalowercaseletterbecausethatmatchesexactlywhat's in the JSON. Later you'llseehowtouseC# propertynamesthatdon't match the JSON property names.
136
+
Althoughthe `GetFromJsonAsync` methodyouwilluseinthenextpointhasabenefitofbeingcase-insensitive when it comes to property names, the C# convention is to [capitalize the first letter of property names](../../standard/design-guidelines/capitalization-conventions.md).
Thefirstargumentto<xref:System.Text.Json.JsonSerializer.DeserializeAsync%60%601(System.IO.Stream,System.Text.Json.JsonSerializerOptions,System.Threading.CancellationToken)?displayProperty=nameWithType>isan `await` expression. `await` expressionscanappearalmostanywhereinyourcode, eventhoughuptonow, you've only seen them as part of an assignment statement. The other two parameters, `JsonSerializerOptions` and `CancellationToken`, are optional and are omitted in the code snippet.
146
+
Thefirstargumentto`GetFromJsonAsync` methodisan `await` expression. `await` expressionscanappearalmostanywhereinyourcode, eventhoughuptonow, you've only seen them as part of an assignment statement. The next parameter, `requestUri` is optional and doesn'thavetobeprovidedifwasalreadyspecifiedwhencreatingthe `client` object. Youdidn't provide the `client` object with the URI to send request to, so you specified the URI now. The last optional parameter, the `CancellationToken` is omitted in the code snippet.
151
147
152
-
The `DeserializeAsync` methodis [*generic*](../fundamentals/types/generics.md), whichmeansyousupplytypeargumentsforwhatkindofobjectsshouldbecreatedfromtheJSONtext. Inthisexample, you'redeserializingtoa `List<Repository>`, whichisanothergenericobject, a <xref:System.Collections.Generic.List%601?displayProperty=nameWithType>. The `List<T>` classstoresacollectionofobjects. Thetypeargumentdeclaresthetypeofobjectsstoredinthe `List<T>`. Thetypeargumentisyour `Repository` record, becausetheJSONtextrepresentsacollectionofrepositoryobjects.
148
+
The `GetFromJsonAsync` methodis [*generic*](../fundamentals/types/generics.md), whichmeansyousupplytypeargumentsforwhatkindofobjectsshouldbecreatedfromthefetchedJSONtext. Inthisexample, you'redeserializingtoa `List<Repository>`, whichisanothergenericobject, a <xref:System.Collections.Generic.List%601?displayProperty=nameWithType>. The `List<T>` classstoresacollectionofobjects. Thetypeargumentdeclaresthetypeofobjectsstoredinthe `List<T>`. Thetypeargumentisyour `Repository` record, becausetheJSONtextrepresentsacollectionofrepositoryobjects.
The `ProcessRepositoriesAsync` method can do the async work and return a collection of the repositories. Change that method to return `Task<List<Repository>>`, and move the code that writes to the console near its caller.
@@ -219,10 +188,7 @@ The `ProcessRepositoriesAsync` method can do the async work and return a collect
219
188
1. Return the repositories after processing the JSON response:
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.
230
+
263
231
1. Update the `foreach` loop in the *Program.cs* file to display the property values:
0 commit comments