Inspired by dotnet show (#386)

* Changed the order of the projects, Web first to infer as startup.

* Added encapsulated JSON serialization - and moved to System.Text.Json

* Refactored a few minor updates out

Co-authored-by: Eric Fleming <eric-fleming18@hotmail.com>
This commit is contained in:
David Pine
2020-06-11 14:47:05 -05:00
committed by GitHub
parent 70a919e145
commit 0af21d22f5
8 changed files with 105 additions and 13 deletions

View File

@@ -9,6 +9,7 @@
<PackageReference Include="Ardalis.GuardClauses" Version="1.5.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,18 @@
using System.Text.Json;
namespace Microsoft.eShopWeb
{
public static class JsonExtensions
{
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
public static T FromJson<T>(this string json) =>
JsonSerializer.Deserialize<T>(json, _jsonOptions);
public static string ToJson<T>(this T obj) =>
JsonSerializer.Serialize<T>(obj, _jsonOptions);
}
}

View File

@@ -172,16 +172,15 @@ namespace Microsoft.eShopWeb.Web
{
ResponseWriter = async (context, report) =>
{
var result = JsonConvert.SerializeObject(
new
var result = new
{
status = report.Status.ToString(),
errors = report.Entries.Select(e => new
{
status = report.Status.ToString(),
errors = report.Entries.Select(e => new
{
key = e.Key,
value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
})
});
key = e.Key,
value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
})
}.ToJson();
context.Response.ContentType = MediaTypeNames.Application.Json;
await context.Response.WriteAsync(result);
}