Allow specifying which attributes should apply to a given property or union case in the options. This is useful to override the format of a type that is not under the developer's control, or simply to keep DTOs lean.
Example:
type MyRecord = { x: int }
let options =
JsonFSharpOptions()
.WithOverrides(fun o -> dict [
typeof<MyRecord>, o.WithOverrideProperty("x", [JsonNameAttribute "y"])
])
.ToJsonSerializerOptions()
JsonSerializer.Serialize({ x = 1 }, o)
// --> {"y":1}
Example combined with #180 to fully control the format of the standard library's Result<'ok, 'error>:
let options =
JsonFSharpOptions()
.WithOverrides(fun o -> dict [
typedefof<Result<_, _>>, o
.WithUnionInternalTag()
.WithUnionNamedFields()
.WithUnionTagName("isSuccess")
.WithOverrideCase("Ok", [
JsonNameAttribute true
JsonNameAttribute("value", Field = "ResultValue")
])
.WithOverrideCase("Error", [
JsonNameAttribute false
JsonNameAttribute("error", Field = "ErrorValue")
])
])
JsonSerializer.Serialize(Ok 42, options)
// --> {"isSuccess":true,"value":42}
JsonSerializer.Serialize(Error "Internal error", options)
// --> {"isSuccess":false,"error":"Internal error"}
Allow specifying which attributes should apply to a given property or union case in the options. This is useful to override the format of a type that is not under the developer's control, or simply to keep DTOs lean.
Example:
Example combined with #180 to fully control the format of the standard library's
Result<'ok, 'error>
: