question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Json Serlization of Maybe

See original GitHub issue

How 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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
xavierjohncommented, Nov 24, 2021

The gist: Maybe class does not support serialization so we have to use a DTO class.

0reactions
vkhorikovcommented, Nov 24, 2021

In the domain, it’s better to have Maybe<Email> AdditionalEmail; instead of EMail? 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 Id and Name and the other one with just an Id. I haven’t looked into the current implementation in-depth yet, it probably needs some refactoring.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found