Skip to content

WithUnwrapOption(false) still serializes None as null #171

@cmeeren

Description

@cmeeren

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions