Closed Tarmil closed 1 year ago
- The fluent syntax looks very C#-ish, even though it works perfectly fine in F#. Should we also add a more F#-ish syntax with module functions?
I'm a bit late to the party, but for the record, I don't think the "F# alternative" adds any value. Fluent syntax works great in F#. Indeed, object programing (using classes etc.) works great and is even recommended in the official F# coding guidelines (not OOO stuff like inheritance, but using interfaces and classes).
@cmeeren Yes, I ended up not implementing it.
Just to be safe, can you verify that the old-style
options.Converters.Add(
JsonFSharpConverter(
unionEncoding = (
JsonUnionEncoding.Default
||| JsonUnionEncoding.ExternalTag
||| JsonUnionEncoding.NamedFields
||| JsonUnionEncoding.UnwrapFieldlessTags
||| JsonUnionEncoding.UnwrapSingleFieldCases
||| JsonUnionEncoding.UnionFieldNamesFromTypes),
unionFieldNamingPolicy = JsonNamingPolicy.CamelCase
)
)
is equivalent to the new-style
options.Converters.Add(
JsonFSharpOptions
.Default()
.WithUnionExternalTag()
.WithUnionNamedFields()
.WithUnionUnwrapFieldlessTags()
.WithUnionUnwrapSingleFieldCases()
.WithUnionFieldNamesFromTypes()
.WithUnionFieldNamingPolicy(JsonNamingPolicy.CamelCase)
|> JsonFSharpConverter
)
?
Yes, it's equivalent. Plus, this:
fsOptions.AddToJsonSerializerOptions(options)
is equivalent to:
JsonFSharpConverter(fsOptions)
|> options.Converters.Add
This PR introduces a new fluent syntax to define the options of the converter.
New API:
.Default()
,.NewtonsoftLike()
,.ThothLike()
,.FSharpLuLike()
that create options with the correspondingJsonUnionEncoding
..WithUnionFoo(?bool)
for every enum caseJsonUnionEncoding.Foo
;.WithFoo(value)
for every optional argumentfoo
of theJsonFSharpConverter
constructor..ToJsonSerializerOptions()
creates aJsonSerializerOptions
with an F# converter;.AddToJsonSerializerOptions(options)
adds the F# converter to an existingJsonSerializerOptions
, useful to integrate with ASP.NET Core, Giraffe, or other libraries.Advantages:
JsonFSharpConverter
constructor is binary-breaking (see #132), whereas adding a fluent method isn't.JsonSerializerOptions
in a single expression, without having to create it and then mutate it. Especially useful when creating global options in a module.Easier to create a base of common options and then alter them slightly for overrides:
Unresolved questions:
JsonFSharpConverterAttribute
? Since it's an attribute, it can't be fluent. Perhaps it should have settable properties that do the same thing as corresponding fluent methods, eg: