Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Ardalis.Result/PagedInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ardalis.Result
using System.Text.Json.Serialization;

namespace Ardalis.Result
{
public class PagedInfo
{
Expand All @@ -11,9 +13,13 @@ public PagedInfo(long pageNumber, long pageSize, long totalPages, long totalReco
TotalRecords = totalRecords;
}

[JsonInclude]
public long PageNumber { get; private set; }
[JsonInclude]
public long PageSize { get; private set; }
[JsonInclude]
public long TotalPages { get; private set; }
[JsonInclude]
public long TotalRecords { get; private set; }

public PagedInfo SetPageNumber(long pageNumber)
Expand Down
7 changes: 5 additions & 2 deletions src/Ardalis.Result/PagedResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Ardalis.Result
using System.Text.Json.Serialization;

namespace Ardalis.Result
{
public class PagedResult<T> : Result<T>
{
Expand All @@ -7,6 +9,7 @@ public PagedResult(PagedInfo pagedInfo, T value) : base(value)
PagedInfo = pagedInfo;
}

public PagedInfo PagedInfo { get; }
[JsonInclude]
public PagedInfo PagedInfo { get; init; }
}
}
8 changes: 7 additions & 1 deletion src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ protected Result(ResultStatus status)
ValidationErrors = result.ValidationErrors,
};

public T Value { get; }
[JsonInclude]
public T Value { get; init; }

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

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions tests/Ardalis.Result.UnitTests/SystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public void ShouldDeserializeResultOfReferenceType()

Assert.Equivalent(expected, result);
}


[Fact]
public void ShouldDeserializeCorrectResultType()
{
var obj = Result.NotFound("NotFound");

var a = JsonSerializer.Serialize(obj);
var b = JsonSerializer.Deserialize<Result>(a);
var c = JsonSerializer.Serialize(b);

Assert.Equivalent(a, c);
}

private class Foo
{
Expand Down