Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
325 stars 44 forks source link

How would I serialize read-only property of a record? #66

Closed xperiandri closed 1 year ago

xperiandri commented 3 years ago

I have a type like this

type Mesago =
    { Dialogo : DialogoID
      From : UzantoID
      Content : MesagoContent
      SentAt : DateTime }
    with
        [<JsonPropertyName("id")>]
        member mesago.ID =
            { SentAt = mesago.SentAt
              From = mesago.From }
        [<JsonIgnore>]
        member mesago.IDValue =
            mesago.ID |> MesagoID.value

How can I serialize ID property along with others?

Tarmil commented 1 year ago

Since version 1.0, the attribute JsonInclude can be put on the property that should be serialized. Alternately, the option includeRecordProperties will include all record properties (unless they have the JsonIgnore attribute). https://github.com/Tarmil/FSharp.SystemTextJson/blob/master/docs/Customizing.md#include-record-properties