Newtonsoft's Json library has the ability to set global settings to apply custom converters and other settings. I've got a custom converter that works as long as I call it explicitly for each object I serialize, but I would like to set it globally so I don't have to do that. This can be done as shown here in C#:
https://stackoverflow.com/a/19121653/2506634
And the official signature of the DefaultSettings property is:
public static Func<JsonSerializerSettings> DefaultSettings { get; set; }
I've tried to translate this to F# like so:
JsonConvert.DefaultSettings =
System.Func<JsonSerializerSettings>
(fun () ->
let settings = new JsonSerializerSettings()
settings.Formatting <- Formatting.Indented
settings.Converters.Add(new DuConverter())
settings
)
|> ignore
This compiles, and executes without error, but the custom converter is not applied when serializing. Also, for some reason setting the property returns a boolean (hence the |> ignore
) and I've noted that this boolean is false.
So, is something wrong with my translation to F#? Or is Newtonsoft perhaps ignoring my custom converter because the built in converter is being applied with precedence?