-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serialization support for Try and Unit (#168)
- Loading branch information
Showing
11 changed files
with
340 additions
and
8 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,70 @@ | ||
using System.Text.Json; | ||
using Xunit; | ||
|
||
namespace FuncSharp.Tests; | ||
|
||
public class UnitJsonConverterTests | ||
{ | ||
private record DummyObjectWithUnit(string PropertyBefore, Unit Unit, string PropertyAfter); | ||
|
||
[Fact] | ||
public void Serializes_unit_only() | ||
{ | ||
// Act | ||
var json = JsonSerializer.Serialize(Unit.Value); | ||
|
||
// Assert | ||
Assert.Equal("{}", json); | ||
} | ||
|
||
[Fact] | ||
public void Deserializes_unit_only() | ||
{ | ||
// Act | ||
var value = JsonSerializer.Deserialize<Unit>("{}"); | ||
|
||
// Assert | ||
Assert.Equal(Unit.Value, value); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_complex_object_including_unit() | ||
{ | ||
// Arrange | ||
var value = new DummyObjectWithUnit("before", Unit.Value, "after"); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""PropertyBefore"":""before"",""Unit"":{},""PropertyAfter"":""after""}", json); | ||
} | ||
|
||
[Fact] | ||
public void Deserializes_complex_object_including_unit() | ||
{ | ||
// Arrange | ||
var json = @"{""PropertyBefore"":""before"",""Unit"":{},""PropertyAfter"":""after""}"; | ||
|
||
// Act | ||
var value = JsonSerializer.Deserialize<DummyObjectWithUnit>(json); | ||
|
||
// Assert | ||
Assert.Equal("before", value.PropertyBefore); | ||
Assert.Equal(Unit.Value, value.Unit); | ||
Assert.Equal("after", value.PropertyAfter); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_try_with_success_as_unit() | ||
{ | ||
// Arrange | ||
var value = Try.Success<Unit, string>(Unit.Value); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""IsSuccess"":true,""Value"":{}}", json); | ||
} | ||
} |
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,121 @@ | ||
using System.Text.Json; | ||
using Xunit; | ||
|
||
namespace FuncSharp.Tests; | ||
|
||
public class TryConverterTests | ||
{ | ||
private sealed record DummySuccess(string SomeProperty); | ||
private sealed record DummyError(string Message); | ||
|
||
private sealed record ComplexObject(string PropertyBefore, Try<DummySuccess, DummyError> Value, string PropertyAfter); | ||
|
||
[Fact] | ||
public void Serializes_success_with_primitive_value() | ||
{ | ||
// Arrange | ||
var value = Try.Success<int, string>(42); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""IsSuccess"":true,""Value"":42}", json); | ||
} | ||
|
||
[Fact] | ||
public void Deserializes_success_with_primitive_value() | ||
{ | ||
// Arrange | ||
var json = @"{""IsSuccess"":true,""Value"":42}"; | ||
|
||
// Act | ||
var value = JsonSerializer.Deserialize<Try<int, string>>(json); | ||
|
||
// Assert | ||
Assert.True(value.IsSuccess); | ||
Assert.Equal(42, value.Success.Get()); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_success_with_reference_type() | ||
{ | ||
// Arrange | ||
var value = Try.Success<DummySuccess, string>(new DummySuccess("success!")); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""IsSuccess"":true,""Value"":{""SomeProperty"":""success!""}}", json); | ||
} | ||
|
||
[Fact] | ||
public void Deserializes_success_with_reference_type() | ||
{ | ||
// Arrange | ||
var json = @"{""IsSuccess"":true,""Value"":{""SomeProperty"":""success!""}}"; | ||
|
||
// Act | ||
var value = JsonSerializer.Deserialize<Try<DummySuccess, string>>(json); | ||
|
||
// Assert | ||
Assert.True(value.IsSuccess); | ||
Assert.Equal(new DummySuccess("success!"), value.Success.Get()); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_error_with_primitive_value() | ||
{ | ||
// Arrange | ||
var value = Try.Error<int, string>("error!"); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""IsSuccess"":false,""Value"":""error!""}", json); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_error_with_reference_type() | ||
{ | ||
// Arrange | ||
var value = Try.Error<int, DummyError>(new DummyError("error!")); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""IsSuccess"":false,""Value"":{""Message"":""error!""}}", json); | ||
} | ||
|
||
[Fact] | ||
public void Serializes_complex_object_including_try() | ||
{ | ||
// Arrange | ||
var value = new ComplexObject("before", Try.Success<DummySuccess, DummyError>(new DummySuccess("success!")), "after"); | ||
|
||
// Act | ||
var json = JsonSerializer.Serialize(value); | ||
|
||
// Assert | ||
Assert.Equal(@"{""PropertyBefore"":""before"",""Value"":{""IsSuccess"":true,""Value"":{""SomeProperty"":""success!""}},""PropertyAfter"":""after""}", json); | ||
} | ||
|
||
[Fact] | ||
public void Deserializes_complex_object_including_try() | ||
{ | ||
// Arrange | ||
var json = @"{""PropertyBefore"":""before"",""Value"":{""IsSuccess"":true,""Value"":{""SomeProperty"":""success!""}},""PropertyAfter"":""after""}"; | ||
|
||
// Act | ||
var value = JsonSerializer.Deserialize<ComplexObject>(json); | ||
|
||
// Assert | ||
Assert.Equal("before", value.PropertyBefore); | ||
Assert.True(value.Value.IsSuccess); | ||
Assert.Equal(new DummySuccess("success!"), value.Value.Success.Get()); | ||
Assert.Equal("after", value.PropertyAfter); | ||
} | ||
} |
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,20 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace FuncSharp; | ||
|
||
public sealed class UnitJsonConverter : JsonConverter<Unit> | ||
{ | ||
public override Unit Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
reader.Read(); | ||
return Unit.Value; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Unit value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteEndObject(); | ||
} | ||
} |
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
Oops, something went wrong.