Skip to content

Commit

Permalink
Fix System.Text.Json Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMcMaster committed Oct 6, 2023
1 parent e4f7f5e commit afec07c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="System.Runtime" Version="4.3.1" />
<PackageVersion Include="System.Text.Json" Version="7.0.3" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Ardalis.Result/Ardalis.Result.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
<ItemGroup>
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="System.Text.Json" />
</ItemGroup>
</Project>
10 changes: 3 additions & 7 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Ardalis.Result
{
Expand All @@ -10,10 +11,6 @@ protected Result() { }
public Result(T value)
{
Value = value;
if (Value != null)
{
ValueType = Value.GetType();
}
}

protected internal Result(T value, string successMessage) : this(value)
Expand All @@ -40,16 +37,15 @@ protected Result(ResultStatus status)

public T Value { get; }

public Type ValueType { get; private set; }
[JsonIgnore]
public Type ValueType => typeof(T);
public ResultStatus Status { get; protected set; } = ResultStatus.Ok;
public bool IsSuccess => Status == ResultStatus.Ok;
public string SuccessMessage { get; protected set; } = string.Empty;
public string CorrelationId { get; protected set; } = string.Empty;
public IEnumerable<string> Errors { get; protected set; } = new List<string>();
public List<ValidationError> ValidationErrors { get; protected set; } = new List<ValidationError>();

public void ClearValueType() => ValueType = null;

/// <summary>
/// Returns the current value.
/// </summary>
Expand Down
57 changes: 57 additions & 0 deletions tests/Ardalis.Result.UnitTests/SystemTextJsonSerializer.cs
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; }
}
}
}

0 comments on commit afec07c

Please sign in to comment.