Reputation: 1172
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?
Upvotes: 2
Views: 608
Reputation: 55184
As I said in the comments, you want to use the assignment operator (<-
) instead of the equality operator (=
). Note that once you do this, the compiler will also apply the delegate conversion for you automatically (and there's no result to ignore), so your code can just become:
JsonConvert.DefaultSettings <-
fun () ->
let settings = new JsonSerializerSettings()
settings.Formatting <- Formatting.Indented
settings.Converters.Add(new DuConverter())
settings
Upvotes: 3