Skip to content

Commit

Permalink
Add SerializationContext with Source Gen
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Oct 6, 2023
1 parent 95003ae commit af5158d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace BlazorApp1.Server.Abstractions.Contracts.JsonContext;

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(WeatherForecastDto))]
[JsonSerializable(typeof(WeatherForecastDto[]))]
[JsonSerializable(typeof(IEnumerable<WeatherForecastDto>))]
[JsonSerializable(typeof(List<WeatherForecastDto>))]
[JsonSerializable(typeof(IEnumerable<WeatherForecastSummaryDto>))]
[JsonSerializable(typeof(List<WeatherForecastSummaryDto>))]
[JsonSerializable(typeof(WeatherForecastSummaryDto))]
[JsonSerializable(typeof(WeatherForecastSummaryDto[]))]
public partial class WeatherForecastDtoJsonContext : JsonSerializerContext
{
}
1 change: 1 addition & 0 deletions BlazorApp1/Client/BlazorApp1.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BlazorApp1.Server.Abstractions\BlazorApp1.Server.Abstractions.csproj" />
<ProjectReference Include="..\Shared\BlazorApp1.Data.csproj" />
</ItemGroup>

Expand Down
13 changes: 8 additions & 5 deletions BlazorApp1/Client/Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@page "/fetchdata"
@using BlazorApp1.Server.Abstractions.Contracts.JsonContext;
@using BlazorApp1.Shared
@using BlazorApp1.Domain
@using BlazorApp1.Server.Abstractions.Contracts
@using System.Text.Json.Serialization;
@inject HttpClient Http

<PageTitle>Weather forecast</PageTitle>
Expand Down Expand Up @@ -68,15 +71,15 @@ else
}

@code {
private WeatherForecast[]? forecasts;
private WeatherForecastDto[]? forecasts;
private WeatherForecastSummary[]? forecastSummaries;
private WeatherForecast? forecastToAdd;
private WeatherForecastDto? forecastToAdd;
private static System.Timers.Timer _timer;

protected override async Task OnInitializedAsync()
{
await ReloadDataAsync();
forecastToAdd = new WeatherForecast
forecastToAdd = new WeatherForecastDto
{
Date = DateTime.Now
};
Expand All @@ -99,15 +102,15 @@ else
{
if (forecastToAdd != null)
{
var response = await Http.PostAsJsonAsync<WeatherForecast>("WeatherForecast", forecastToAdd);
var response = await Http.PostAsJsonAsync<WeatherForecastDto>("WeatherForecast", forecastToAdd, WeatherForecastDtoJsonContext.Default.WeatherForecastDto);
}

await ReloadDataAsync();
}

private async Task ReloadDataAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
forecasts = await Http.GetFromJsonAsync<WeatherForecastDto[]>("WeatherForecast", WeatherForecastDtoJsonContext.Default.WeatherForecastDtoArray);
forecastSummaries = await Http.GetFromJsonAsync<WeatherForecastSummary[]>("WeatherForecastSummary");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<IEnumerable<WeatherForecastSummaryDto>> Get()
{
var forecastSummaries = await _weatherForecastSummaryRepository.GetAllForecastSummaries();

return forecastSummaries.Select(x => MapToDto(x));
return forecastSummaries.Select(x => MapToDto(x)).ToList();
}

private static WeatherForecastSummaryDto MapToDto(WeatherForecastSummary x)
Expand Down
6 changes: 5 additions & 1 deletion BlazorApp1/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using BlazorApp1.Application;
using BlazorApp1.Data;
using BlazorApp1.Server.Abstractions.Contracts;
using BlazorApp1.Server.Abstractions.Contracts.JsonContext;
using BlazorApp1.Server.BackgroundServices;
using BlazorApp1.Server.Profiles;
using Microsoft.EntityFrameworkCore;
Expand All @@ -8,7 +10,9 @@

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews()
// .AddJsonOptions(options => options.JsonSerializerOptions.AddContext<WeatherForecastDtoJsonContext>())
;
builder.Services.AddRazorPages();

builder.Services.AddWeatherForecastDataLayer();
Expand Down

0 comments on commit af5158d

Please sign in to comment.