-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix System.Text.Json Serialization (#151)
- Loading branch information
1 parent
60d55de
commit dae5fba
Showing
4 changed files
with
64 additions
and
7 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
57 changes: 57 additions & 0 deletions
57
tests/Ardalis.Result.UnitTests/SystemTextJsonSerializer.cs
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,57 @@ | ||
using System.Text.Json; | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.UnitTests | ||
{ | ||
public class SystemTextJsonSerializer | ||
{ | ||
[Fact] | ||
public void ShouldSerializeResultOfValueType() | ||
{ | ||
var result = Result.Success(5); | ||
string expected = "{\"Value\":5,\"Status\":0,\"IsSuccess\":true,\"SuccessMessage\":\"\",\"CorrelationId\":\"\",\"Errors\":[],\"ValidationErrors\":[]}"; | ||
|
||
var json = JsonSerializer.Serialize(result); | ||
|
||
Assert.Equal(expected, json); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSerializeResultOfReferenceType() | ||
{ | ||
var result = Result.Success(new Foo { Bar = "Result!" }); | ||
string expected = "{\"Value\":{\"Bar\":\"Result!\"},\"Status\":0,\"IsSuccess\":true,\"SuccessMessage\":\"\",\"CorrelationId\":\"\",\"Errors\":[],\"ValidationErrors\":[]}"; | ||
|
||
var json = JsonSerializer.Serialize(result); | ||
|
||
Assert.Equal(expected, json); | ||
} | ||
|
||
[Fact] | ||
public void ShouldDeserializeResultOfValueType() | ||
{ | ||
var expected = Result.Success(10); | ||
string json = "{\"Value\":10,\"Status\":0,\"IsSuccess\":true,\"SuccessMessage\":\"\",\"CorrelationId\":\"\",\"Errors\":[],\"ValidationErrors\":[]}"; | ||
|
||
var result = JsonSerializer.Deserialize<Result<int>>(json); | ||
|
||
Assert.Equivalent(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void ShouldDeserializeResultOfReferenceType() | ||
{ | ||
var expected = Result.Success(new Foo { Bar = "Result!" }); | ||
string json = "{\"Value\":{\"Bar\":\"Result!\"},\"Status\":0,\"IsSuccess\":true,\"SuccessMessage\":\"\",\"CorrelationId\":\"\",\"Errors\":[],\"ValidationErrors\":[]}"; | ||
|
||
var result = JsonSerializer.Deserialize<Result<Foo>>(json); | ||
|
||
Assert.Equivalent(expected, result); | ||
} | ||
|
||
private class Foo | ||
{ | ||
public string Bar { get; set; } | ||
} | ||
} | ||
} |