Json Serlization of Maybe
See original GitHub issueHow do I Json serialize a class with Maybe property?
Repro: Update the current unit test. CSharpFunctionalExtensions\CSharpFunctionalExtensions.Tests\MaybeTests
[Fact]
public void Deserialize_sets_no_value_on_maybe_none()
{
Maybe<MyClass> maybe = Maybe<MyClass>.None;
JsonSerializer.Serialize(maybe);
}
It throws.
System.InvalidOperationException : Maybe has no value.
Thanks.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Serialization fails · Issue #4 · pluralsight/maybe-dotnet
But it obviously fails when trying to return a Maybe from a WebAPI for example, since the result is serialized as Json.
Read more >Functional.Maybe.Json 2.0.0
Json converter for error-less serialization of Maybe type when using Newtonsoft.Json. Just add MaybeConverter to your list of Json converters.
Read more >Efficient JSON serialization with Jackson and Java
Conclusion. Jackson is a popular JSON serialization library written in Java, which retains great backwards compatibility at the expense of a ...
Read more >On the complexity of JSON serialization - ample code
In my experience, a JSON serializer is just a function that converts between a string and a data structure, and “data model mapping”...
Read more >On the complexity of JSON serialization (2020)
In practice, JSON serialization libraries are used to map between domain objects and a JSON representation.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The gist: Maybe class does not support serialization so we have to use a DTO class.
In the domain, it’s better to have
Maybe<Email> AdditionalEmail;instead ofEMail? AdditionalEmail;. If you want to serialize the class somewhere, then you’d need to map it to a DTO, which you’d serialize instead of the domain class.Regarding the enum class, there should be 2 versions, 1 with
IdandNameand the other one with just anId. I haven’t looked into the current implementation in-depth yet, it probably needs some refactoring.