Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithUnwrapOption(false) still serializes None as null #171

Open
cmeeren opened this issue Aug 21, 2023 · 5 comments
Open

WithUnwrapOption(false) still serializes None as null #171

cmeeren opened this issue Aug 21, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Aug 21, 2023

When using WithUnwrapOption(false), None still serializes to null.

(I am serializing for logging/assertion/debugging purposes and want None/ValueNone to serialize to the strings "None"/"ValueNone", as I expect is the intended functionality with WithUnwrapOption(false).)

open System.Text.Json
open System.Text.Json.Serialization
open Xunit

let options = JsonSerializerOptions()
options.Converters.Add(
    JsonFSharpOptions
        .Default()
        .WithUnwrapOption(false)
    |> JsonFSharpConverter
)
Assert.Equal("\"None\"", JsonSerializer.Serialize(None))

This fails with:

Xunit.Sdk.EqualException
Assert.Equal() Failure
          ↓ (pos 0)
Expected: "None"
Actual:   null
          ↑ (pos 0)
@cmeeren cmeeren changed the title WithUnwrapOption(false) has no effect (None still serializes to null) WithUnwrapOption(false) still serializes None as null Aug 21, 2023
@cmeeren
Copy link
Contributor Author

cmeeren commented Aug 21, 2023

Could this be related to Option<_> using [<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue)>]?

@Tarmil
Copy link
Owner

Tarmil commented Aug 21, 2023

Yes, it is on purpose that None and ValueNone are serialized as null. I am open to adding an option to customize this though!

@Tarmil Tarmil added the enhancement New feature or request label Aug 21, 2023
@cmeeren
Copy link
Contributor Author

cmeeren commented Aug 21, 2023

Would be great! I am not serializing for idiomatic JSON, but for displaying structures of F# data (in a better way than %A), so displaying None instead of null would help my users.

@cmeeren
Copy link
Contributor Author

cmeeren commented Aug 22, 2023

Note by the way that the docs for WithUnwrapOption are misleading:

/// If set, `None` is represented as null,
/// and `Some x` is represented the same as `x`.
/// This flag is included in Default.
member this.WithUnwrapOption([<Optional; DefaultParameterValue true>] set: bool) =
this.WithUnionEncodingFlag(JsonUnionEncoding.UnwrapOption, set)

It says that if set, None is serialized as null. While this technically doesn't say that if it's not set, then None is not serialized as null, it does give a strong impression that that's the intended meaning.

Also relevant here:

/// If set, `None` is represented as null,
/// and `Some x` is represented the same as `x`.
/// This flag is included in Default.
| UnwrapOption = 0x00_00_04_00

@cmeeren
Copy link
Contributor Author

cmeeren commented Sep 10, 2023

Hi! Just wondering if it would be possible to implement this soon-ish (say, during the coming week or two)? I need to decide whether to work around this, but the point is moot if this could be supported upstream. 🙂

Yes, it is on purpose that None and ValueNone are serialized as null.

By the way, ValueNone is serialized as ValueNone, not null (regardless of WithUnwrapOption).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants