Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 43 forks source link

Serializing member properties on record types #92

Closed jchannon closed 2 years ago

jchannon commented 3 years ago

With something like this:

type Pitstop =
    { 
          ATime: DateTimeOffset
          BTime:DateTimeOffset
        }
    member this.Name = "Dave"
    member this.Result = (this.BTime - this.ATime).TotalSeconds

And execute the serializer:

    let options = JsonSerializerOptions()
    options.Converters.Add(JsonFSharpConverter())
    options.PropertyNameCaseInsensitive <- true
    printfn "%s" (JsonSerializer.Serialize({ATime=DateTimeOffset.UtcNow
                                            BTime = DateTimeOffset.UtcNow.AddMinutes(1.0)}, options))

The output does not contain the member properties. Remove the converter in the options and it does get printed out.

severisv commented 2 years ago

Any workaround for this?

Tarmil commented 2 years ago

Not currently, no. But you're the third person now asking for it; I'll look into adding it as an optional behavior.

Tarmil commented 2 years ago

This was added in v1.0 with the option includeRecordProperties: bool.