|
1 | | -namespace FsCodec.SystemTextJson.Serialization |
| 1 | +namespace FsCodec.SystemTextJson |
2 | 2 |
|
| 3 | +open FsCodec.SystemTextJson.Serialization |
| 4 | +open System |
| 5 | +open System.Runtime.InteropServices |
3 | 6 | open System.Text.Json |
| 7 | +open System.Text.Json.Serialization |
4 | 8 |
|
5 | | -[<AutoOpen>] |
6 | | -module JsonSerializerOptionExtensions = |
7 | | - type JsonSerializerOptions with |
8 | | - static member Create() = |
9 | | - let options = JsonSerializerOptions() |
10 | | - options.Converters.Add(new JsonRecordConverter()) |
11 | | - options |
| 9 | +type Options private () = |
12 | 10 |
|
13 | | -module JsonSerializer = |
14 | | - let defaultOptions = JsonSerializerOptions.Create() |
| 11 | + static let defaultConverters : JsonConverterFactory[] = [| JsonOptionConverter(); JsonRecordConverter() |] |
| 12 | + |
| 13 | + /// Creates a default set of serializer options used by Json serialization. When used with no args, same as `JsonSerializerOptions()` |
| 14 | + static member CreateDefault |
| 15 | + ( [<Optional; ParamArray>] converters : JsonConverterFactory[], |
| 16 | + /// Use multi-line, indented formatting when serializing JSON; defaults to false. |
| 17 | + [<Optional; DefaultParameterValue(null)>] ?indent : bool, |
| 18 | + /// Render idiomatic camelCase for PascalCase items by using `PropertyNamingPolicy = CamelCase`. Defaults to false. |
| 19 | + [<Optional; DefaultParameterValue(null)>] ?camelCase : bool, |
| 20 | + /// Ignore null values in input data; defaults to false. |
| 21 | + [<Optional; DefaultParameterValue(null)>] ?ignoreNulls : bool) = |
| 22 | + |
| 23 | + let indent = defaultArg indent false |
| 24 | + let camelCase = defaultArg camelCase false |
| 25 | + let ignoreNulls = defaultArg ignoreNulls false |
| 26 | + let options = JsonSerializerOptions() |
| 27 | + if converters <> null then converters |> Array.iter options.Converters.Add |
| 28 | + if indent then options.WriteIndented <- true |
| 29 | + if camelCase then options.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase; options.DictionaryKeyPolicy <- JsonNamingPolicy.CamelCase |
| 30 | + if ignoreNulls then options.IgnoreNullValues <- true |
| 31 | + options |
| 32 | + |
| 33 | + /// Opinionated helper that creates serializer settings that provide good defaults for F# |
| 34 | + /// - Always prepends `[JsonOptionConverter(); JsonRecordConverter()]` to any converters supplied |
| 35 | + /// - no camel case conversion - assumption is you'll use records with camelCased names |
| 36 | + /// Everything else is as per CreateDefault:- i.e. emit nulls instead of omitting fields, no indenting, no camelCase conversion |
| 37 | + static member Create |
| 38 | + ( /// List of converters to apply. Implicit [JsonOptionConverter(); JsonRecordConverter()] will be prepended and/or be used as a default |
| 39 | + [<Optional; ParamArray>] converters : JsonConverterFactory[], |
| 40 | + /// Use multi-line, indented formatting when serializing JSON; defaults to false. |
| 41 | + [<Optional; DefaultParameterValue(null)>] ?indent : bool, |
| 42 | + /// Render idiomatic camelCase for PascalCase items by using `PropertyNamingPolicy = CamelCase`. |
| 43 | + /// Defaults to false on basis that you'll use record and tuple field names that are camelCase (but thus not `CLSCompliant`). |
| 44 | + [<Optional; DefaultParameterValue(null)>] ?camelCase : bool, |
| 45 | + /// Ignore null values in input data; defaults to `false`. |
| 46 | + [<Optional; DefaultParameterValue(null)>] ?ignoreNulls : bool) = |
| 47 | + |
| 48 | + Options.CreateDefault( |
| 49 | + converters = (match converters with null | [||] -> defaultConverters | xs -> Array.append defaultConverters xs), |
| 50 | + ?ignoreNulls = ignoreNulls, |
| 51 | + ?indent = indent, |
| 52 | + ?camelCase = camelCase) |
0 commit comments