Skip to content

Commit 06ba5ef

Browse files
authored
Added segment about JSON codegen
1 parent 451cb30 commit 06ba5ef

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/06-authentication-and-authorization.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,50 @@ builder.Services.AddHttpClient<OrdersClient>(client => client.BaseAddress = new
263263
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
264264
```
265265

266+
### Optional: Optimize JSON interactions with .NET 6 JSON CodeGeneration
267+
268+
Starting with .NET 6, the System.Text.Json.JsonSerializer supports working with optimized code generated for serializing and deserializing JSON payloads. Code is generated at build time, resulting in a significant performance improvement for the serialization and deserialization of JSON data. This is configured by taking the following steps:
269+
270+
1. Create a partial Context class that inherits from `System.Text.Json.Serialization.JsonSerializerContext`
271+
2. Decorate the class with the `System.Text.Json.JsonSourceGenerationOptions` attribute
272+
3. Add `JsonSerializable` attributes to the class definition for each type that you would like to have code generated
273+
274+
We have already written this context for you and it is located in the `BlazingPizza.Shared.Order.cs" file
275+
276+
```csharp
277+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
278+
[JsonSerializable(typeof(Order))]
279+
[JsonSerializable(typeof(OrderWithStatus))]
280+
[JsonSerializable(typeof(List<OrderWithStatus>))]
281+
[JsonSerializable(typeof(Pizza))]
282+
[JsonSerializable(typeof(List<PizzaSpecial>))]
283+
[JsonSerializable(typeof(List<Topping>))]
284+
[JsonSerializable(typeof(Topping))]
285+
public partial class OrderContext : JsonSerializerContext {}
286+
```
287+
288+
You can now optimize the calls to the HttpClient in the `OrdersClient` class by passing an `OrderContext.Default` parameter pointing to the type sought as the second parameter. Updating the methods in the `OrdersClient` class should look like the following:
289+
290+
```csharp
291+
public async Task<IEnumerable<OrderWithStatus>> GetOrders() =>
292+
await httpClient.GetFromJsonAsync("orders", OrderContext.Default.ListOrderWithStatus);
293+
294+
295+
public async Task<OrderWithStatus> GetOrder(int orderId) =>
296+
await httpClient.GetFromJsonAsync($"orders/{orderId}", OrderContext.Default.OrderWithStatus);
297+
298+
299+
public async Task<int> PlaceOrder(Order order)
300+
{
301+
var response = await httpClient.PostAsJsonAsync("orders", order, OrderContext.Default.Order);
302+
response.EnsureSuccessStatusCode();
303+
var orderId = await response.Content.ReadFromJsonAsync<int>();
304+
return orderId;
305+
}
306+
```
307+
308+
### Deploy OrdersClient to pages
309+
266310
Update each page where an `HttpClient` is used to manage orders to use the new typed `OrdersClient`. Inject an `OrdersClient` instead of an `HttpClient` and use the new client to make the API call. Wrap each call in a `try-catch` that handles exceptions of type `AccessTokenNotAvailableException` by calling the provided `Redirect()` method.
267311

268312
*Checkout.razor*

0 commit comments

Comments
 (0)