G-Research / fsharp-analyzers

Analyzers for F#
https://g-research.github.io/fsharp-analyzers/
Apache License 2.0
14 stars 1 forks source link

JsonSerializerOptionsAnalyzer should also mention .Default #44

Open bartelink opened 6 months ago

bartelink commented 6 months ago

While I don't disagree with the advice in https://g-research.github.io/fsharp-analyzers/analyzers/JsonSerializerOptionsAnalyzer.html#JsonSerializerOptionsAnalyzer, I'd add a mention of JsonSerializerOptions.Default,

Going further, I believe the better overall pattern is using a Serdes wrapper with a mandatory options arg a la https://github.com/jet/FsCodec/blob/master/src/FsCodec.SystemTextJson/Serdes.fs#L7, but I can appreciate that shoehorning that advice/guidance into an Analyzer might be a stretch!

(Aside: this can be taken even futher by having a lazily-implemented .Default associated with each suite of Options a la https://github.com/jet/FsCodec/blob/master/src/FsCodec.SystemTextJson/Options.fs#L14 and following the general pattern outlined in https://github.com/jet/fscodec?tab=readme-ov-file#examples-of-using-serdes-to-define-a-contract )

nojaf commented 6 months ago

Hello,

While I don't disagree with the advice

This is interesting. I can't say that we based the analyzer on anything other than the feedback of the developers over at G-Research. Could you elaborate on why you disagree with this guidance? Your perspective would be insightful.

JsonSerializerOptions.Default seems like a good addition to mention indeed.

bartelink commented 6 months ago

I DONT disagree with the guidance, but, depending on how it's worded, it might prompt people to skip a potentially simpler solution.

My point is solely that the most common resolution for the literal example provided (creating default settings via the default ctor) already has an even better solution - use the in-the-box .Default property added in STJ V7.

The rest is just me saying "and if you have any Options builders that vary from STJ's Options.Default, then consider following that pattern in your impl:

nojaf commented 6 months ago

Whoops, I misread that. Thanks for elaborating.