File tree Expand file tree Collapse file tree 8 files changed +36
-12
lines changed Expand file tree Collapse file tree 8 files changed +36
-12
lines changed Original file line number Diff line number Diff line change 1
1
using System . Net . Http . Json ;
2
+ using System . Text . Json ;
2
3
3
4
namespace BlazingPizza . Client ;
4
5
@@ -12,16 +13,16 @@ public OrdersClient(HttpClient httpClient)
12
13
}
13
14
14
15
public async Task < IEnumerable < OrderWithStatus > > GetOrders ( ) =>
15
- await httpClient . GetFromJsonAsync < IEnumerable < OrderWithStatus > > ( "orders" ) ;
16
+ await httpClient . GetFromJsonAsync ( "orders" , OrderContext . Default . ListOrderWithStatus ) ;
16
17
17
18
18
19
public async Task < OrderWithStatus > GetOrder ( int orderId ) =>
19
- await httpClient . GetFromJsonAsync < OrderWithStatus > ( $ "orders/{ orderId } ") ;
20
+ await httpClient . GetFromJsonAsync ( $ "orders/{ orderId } ", OrderContext . Default . OrderWithStatus ) ;
20
21
21
22
22
23
public async Task < int > PlaceOrder ( Order order )
23
24
{
24
- var response = await httpClient . PostAsJsonAsync ( "orders" , order ) ;
25
+ var response = await httpClient . PostAsJsonAsync ( "orders" , order , OrderContext . Default . Order ) ;
25
26
response . EnsureSuccessStatusCode ( ) ;
26
27
var orderId = await response . Content . ReadFromJsonAsync < int > ( ) ;
27
28
return orderId ;
Original file line number Diff line number Diff line change 61
61
62
62
protected override async Task OnInitializedAsync ()
63
63
{
64
- specials = await HttpClient .GetFromJsonAsync < List < PizzaSpecial >> (" specials" );
64
+ specials = await HttpClient .GetFromJsonAsync (" specials" , BlazingPizza . OrderContext . Default . ListPizzaSpecial );
65
65
}
66
66
67
67
async Task RemovePizza (Pizza configuredPizza )
Original file line number Diff line number Diff line change 1
1
using Microsoft . AspNetCore . Components . WebAssembly . Authentication ;
2
2
using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
3
3
using Microsoft . Extensions . DependencyInjection ;
4
- using BlazingPizza . client
4
+ using BlazingPizza . Client ;
5
5
6
6
var builder = WebAssemblyHostBuilder . CreateDefault ( args ) ;
7
7
builder . RootComponents . Add < App > ( "#app" ) ;
Original file line number Diff line number Diff line change 65
65
66
66
protected async override Task OnInitializedAsync ()
67
67
{
68
- toppings = await HttpClient .GetFromJsonAsync < List < Topping >> (" toppings" );
68
+ toppings = await HttpClient .GetFromJsonAsync (" toppings" , BlazingPizza . OrderContext . Default . ListTopping );
69
69
}
70
70
71
71
void ToppingSelected (ChangeEventArgs e )
Original file line number Diff line number Diff line change 4
4
5
5
var builder = WebApplication . CreateBuilder ( args ) ;
6
6
7
- builder . Services . AddControllersWithViews ( ) ;
7
+ builder . Services . AddControllersWithViews ( )
8
+ . AddJsonOptions ( options => {
9
+ options . JsonSerializerOptions . AddContext < BlazingPizza . OrderContext > ( ) ;
10
+ } ) ;
8
11
builder . Services . AddRazorPages ( ) ;
9
12
10
13
builder . Services . AddDbContext < PizzaStoreContext > ( options =>
Original file line number Diff line number Diff line change 1
- namespace BlazingPizza ;
1
+ using System . Text . Json . Serialization ;
2
+
3
+ namespace BlazingPizza ;
2
4
3
5
public class Order
4
6
{
@@ -17,4 +19,14 @@ public class Order
17
19
public decimal GetTotalPrice ( ) => Pizzas . Sum ( p => p . GetTotalPrice ( ) ) ;
18
20
19
21
public string GetFormattedTotalPrice ( ) => GetTotalPrice ( ) . ToString ( "0.00" ) ;
20
- }
22
+ }
23
+
24
+ [ JsonSourceGenerationOptions ( GenerationMode = JsonSourceGenerationMode . Default , PropertyNamingPolicy = JsonKnownNamingPolicy . CamelCase ) ]
25
+ [ JsonSerializable ( typeof ( Order ) ) ]
26
+ [ JsonSerializable ( typeof ( OrderWithStatus ) ) ]
27
+ [ JsonSerializable ( typeof ( List < OrderWithStatus > ) ) ]
28
+ [ JsonSerializable ( typeof ( Pizza ) ) ]
29
+ [ JsonSerializable ( typeof ( List < PizzaSpecial > ) ) ]
30
+ [ JsonSerializable ( typeof ( List < Topping > ) ) ]
31
+ [ JsonSerializable ( typeof ( Topping ) ) ]
32
+ public partial class OrderContext : JsonSerializerContext { }
Original file line number Diff line number Diff line change 1
- using BlazingPizza . ComponentsLibrary . Map ;
1
+ using System . Text . Json . Serialization ;
2
+ using BlazingPizza . ComponentsLibrary . Map ;
3
+
2
4
3
5
namespace BlazingPizza ;
4
6
Original file line number Diff line number Diff line change 1
- namespace BlazingPizza ;
1
+ using System . Text . Json . Serialization ;
2
+
3
+ namespace BlazingPizza ;
2
4
3
5
/// <summary>
4
6
/// /// Represents a customized pizza as part of an order
@@ -35,4 +37,8 @@ public string GetFormattedTotalPrice()
35
37
{
36
38
return GetTotalPrice ( ) . ToString ( "0.00" ) ;
37
39
}
38
- }
40
+ }
41
+
42
+ [ JsonSourceGenerationOptions ( GenerationMode = JsonSourceGenerationMode . Serialization ) ]
43
+ [ JsonSerializable ( typeof ( Pizza ) ) ]
44
+ public partial class PizzaContext : JsonSerializerContext { }
You can’t perform that action at this time.
0 commit comments