Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
title: Streams | ||
permalink: /json/ | ||
layout: single | ||
classes: wide | ||
sidebar: | ||
nav: "sidebar" | ||
--- | ||
|
||
## JSON ## | ||
A lot (if not the vast majority) of applications use JSON (de)serialization. | ||
By doing so, how the serialization is performed is part of the public contract/API. | ||
To prevent unexpected changes, writing tests that ensure the behavior is important. | ||
|
||
### JSON serialization options | ||
To ensure that the assertions are done on the serialization options that are registered, it might be worth explicitly resolve those options: | ||
|
||
```csharp | ||
public class Serialization : Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<Program> | ||
{ | ||
private JsonSerializerOptions SerializerOptions => Services.GetRequiredService<JsonSerializerOptions>(); | ||
} | ||
``` | ||
|
||
Both a `Serialize()` and a `Deserialize()` can be performed: | ||
|
||
```csharp | ||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Serialize(someObject); | ||
options.Should().Deserialize(someJson); | ||
``` | ||
|
||
And because we specially interested in the results of these actions, we want to assert the outcomes to: | ||
|
||
```csharp | ||
public class Serializes | ||
{ | ||
[Fact] | ||
public void DateOnly_as_string_value() | ||
{ | ||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Serialize(new DateOnly(2017, 06, 11)) | ||
.And.Value.Should().BeString("2017-06-11"); | ||
} | ||
|
||
[Fact] | ||
public void Enum_as_number() | ||
{ | ||
|
||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Serialize(SomeEnum.SomeValue) | ||
.And.Value.Should().BeNumber(42); | ||
} | ||
|
||
[Fact] | ||
pulic void Default_of_custom_struct_as_null() | ||
{ | ||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Serialize(default(CustomStruct)) | ||
.And.Value.Should().BeNull(); | ||
} | ||
} | ||
``` | ||
|
||
And | ||
|
||
```csharp | ||
public class Deserializes | ||
{ | ||
[Fact] | ||
public void DateOnly_from_JSON_string() | ||
{ | ||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Deserialize<DateOnly>("2017-06-11") | ||
.And.Value.Should().Be(new DateOnly(2017-06-11)); | ||
} | ||
|
||
[Fact] | ||
public void Enum_from_number() | ||
{ | ||
|
||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Deserialize<SomeEnum>("42") | ||
.And.Value.Should().BeNumber(SomeEnum.SomeValue); | ||
} | ||
|
||
[Fact] | ||
public void Enum_from_string() | ||
{ | ||
|
||
JsonSerializerOptions options = GetOptions(); | ||
|
||
options.Should().Deserialize<SomeEnum>("\"SomeValue\"") | ||
.And.Value.Should().BeNumber(SomeEnum.SomeValue); | ||
} | ||
} | ||
``` |