You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`UnionOrTypeSafeEnumConverterFactory`: Global converter that can apply `TypeSafeEnumConverter` to all Discriminated Unions that do not have cases with values, and `UnionConverter` to ones that have values. See [this `System.Text.Json` issue](https://github.com/dotnet/runtime/issues/55744) for background information as to the reasoning behind and tradeoffs involved in applying such a policy.
102
+
-`RejectNullStringConverter`: Global converter that can reject `null` as a valid string value
102
103
103
104
## `FsCodec.NewtonsoftJson.Options`
104
105
@@ -119,6 +120,7 @@ The respective concrete Codec packages include relevant `Converter`/`JsonConvert
119
120
-`(camelCase = true)`: opts into camel case conversion for `PascalCased` properties and `Dictionary` keys
120
121
-`(autoTypeSafeEnumToJsonString = true)`: triggers usage of `TypeSafeEnumConverter` for any F# Discriminated Unions that only contain nullary cases. See [`AutoUnionTests.fs`](https://github.com/jet/FsCodec/blob/master/tests/FsCodec.SystemTextJson.Tests/AutoUnionTests.fs) for examples
121
122
-`(autoUnionToJsonObject = true)`: triggers usage of a `UnionConverter` to round-trip F# Discriminated Unions (with at least a single case that has a body) as JSON Object structures. See [`AutoUnionTests.fs`](https://github.com/jet/FsCodec/blob/master/tests/FsCodec.SystemTextJson.Tests/AutoUnionTests.fs) for examples
123
+
-`(rejectNullStrings = true)`: triggers usage of [`RejectNullStringConverter`](https://github.com/jet/FsCodec/blob/master/src/FsCodec.SystemTextJson/RejectNullStringConverter.fs) to reject `null` as a value for strings (`string option` is still supported).
122
124
-`Default`: Default settings; same as calling `Create()` produces (same intent as [`JsonSerializerOptions.Default`](https://github.com/dotnet/runtime/pull/61434))
123
125
124
126
## `Serdes`
@@ -229,18 +231,18 @@ The default settings for FsCodec applies Json.NET's default behavior, which is t
229
231
230
232
The recommendations here apply particularly to Event Contracts - the data in your store will inevitably outlast your code, so being conservative in the complexity of one's encoding scheme is paramount. Explicit is better than Implicit.
231
233
232
-
| Type kind | TL;DR | Notes | Example input | Example output |
|`'t[]`| As per C# | Don't forget to handle `null`|`[ 1; 2; 3]`|`[1,2,3]`|
235
-
|`DateTimeOffset`| Roundtrips cleanly | The default `Options.Create` requests `RoundtripKind`|`DateTimeOffset.Now`|`"2019-09-04T20:30:37.272403+01:00"`|
236
-
|`Nullable<'t>`| As per C#; `Nullable()` -> `null`, `Nullable x` -> `x`| OOTB Json.NET and STJ roundtrip cleanly. Works with `Options.CreateDefault()`. Worth considering if your contract does not involve many `option` types |`Nullable 14`|`14`|
237
-
|`'t option`|`Some null`,`None` -> `null`, `Some x` -> `x`_with the converter `Options.Create()` adds_| OOTB Json.NET does not roundtrip `option` types cleanly; `Options.Create` wires in an `OptionConverter` by default in `FsCodec.NewtonsoftJson`<br/> NOTE `Some null` will produce `null`, but deserialize as `None` - i.e., it's not round-trippable |`Some 14`|`14`|
238
-
|`string`| As per C#; need to handle `null`| One can use a `string option` to map `null` and `Some null` to `None`|`"Abc"`|`"Abc"`|
239
-
| types with unit of measure | Works well (doesnt encode the unit) | Unit of measure tags are only known to the compiler; Json.NET does not process the tags and treats it as the underlying primitive type |`54<g>`|`54`|
240
-
|[`FSharp.UMX`](https://github.com/fsprojects/FSharp.UMX) tagged `string`, `DateTimeOffset`| Works well |[`FSharp.UMX`](https://github.com/fsprojects/FSharp.UMX) enables one to type-tag `string` and `DateTimeOffset` values using the units of measure compiler feature, which Json.NET will render as if they were unadorned |`SkuId.parse "54-321"`|`"000-054-321"`|
241
-
| records | Just work | For `System.Text.Json` v `4.x`, usage of `[<CLIMutable>]` or a custom `JsonRecordConverter` was once required |`{\| a = 1; b = Some "x" \|}`|`"{"a":1,"b":"x"}"`|
242
-
| Nullary unions (Enum-like DU's without bodies) | Tag `type` with `TypeSafeEnumConverter`| Works well - guarantees a valid mapping, as opposed to using a `System.Enum` and `StringEnumConverter`, which can map invalid values and/or silently map to `0` etc |`State.NotFound`|`"NotFound"`|
243
-
| Discriminated Unions (where one or more cases has a body) | Tag `type` with `UnionConverter`| This format can be readily consumed in Java, JavaScript and Swift. Nonetheless, exhaust all other avenues before considering encoding a union in JSON. The `"case"` label id can be overridden. |`Decision.Accepted { result = "54" }`|`{"case": "Accepted","result":"54"}`|
234
+
| Type kind | TL;DR | Notes | Example input | Example output |
|`'t[]`| As per C# | Don't forget to handle `null`|`[ 1; 2; 3]`|`[1,2,3]`|
237
+
|`DateTimeOffset`| Roundtrips cleanly | The default `Options.Create` requests `RoundtripKind`|`DateTimeOffset.Now`|`"2019-09-04T20:30:37.272403+01:00"`|
238
+
|`Nullable<'t>`| As per C#; `Nullable()` -> `null`, `Nullable x` -> `x`| OOTB Json.NET and STJ roundtrip cleanly. Works with `Options.CreateDefault()`. Worth considering if your contract does not involve many `option` types |`Nullable 14`|`14`|
239
+
|`'t option`|`Some null`,`None` -> `null`, `Some x` -> `x`_with the converter `Options.Create()` adds_| OOTB Json.NET does not roundtrip `option` types cleanly; `Options.Create` wires in an `OptionConverter` by default in `FsCodec.NewtonsoftJson`<br/> NOTE `Some null` will produce `null`, but deserialize as `None` - i.e., it's not round-trippable |`Some 14`|`14`|
240
+
|`string`| As per C#; need to handle `null`. Can opt into rejecting null values with `(rejectNullStrings = true)`| One can use a `string option` to map `null` and `Some null` to `None`|`"Abc"`|`"Abc"`|
241
+
| types with unit of measure | Works well (doesnt encode the unit) | Unit of measure tags are only known to the compiler; Json.NET does not process the tags and treats it as the underlying primitive type |`54<g>`|`54`|
242
+
|[`FSharp.UMX`](https://github.com/fsprojects/FSharp.UMX) tagged `string`, `DateTimeOffset`| Works well |[`FSharp.UMX`](https://github.com/fsprojects/FSharp.UMX) enables one to type-tag `string` and `DateTimeOffset` values using the units of measure compiler feature, which Json.NET will render as if they were unadorned |`SkuId.parse "54-321"`|`"000-054-321"`|
243
+
| records | Just work | For `System.Text.Json` v `4.x`, usage of `[<CLIMutable>]` or a custom `JsonRecordConverter` was once required |`{\| a = 1; b = Some "x" \|}`|`"{"a":1,"b":"x"}"`|
244
+
| Nullary unions (Enum-like DU's without bodies) | Tag `type` with `TypeSafeEnumConverter`| Works well - guarantees a valid mapping, as opposed to using a `System.Enum` and `StringEnumConverter`, which can map invalid values and/or silently map to `0` etc |`State.NotFound`|`"NotFound"`|
245
+
| Discriminated Unions (where one or more cases has a body) | Tag `type` with `UnionConverter`| This format can be readily consumed in Java, JavaScript and Swift. Nonetheless, exhaust all other avenues before considering encoding a union in JSON. The `"case"` label id can be overridden. |`Decision.Accepted { result = "54" }`|`{"case": "Accepted","result":"54"}`|
/// <summary>Apply <c>UnionConverter</c> for all Discriminated Unions, if <c>TypeSafeEnumConverter</c> not possible. Defaults to <c>false</c>.</summary>
/// <summary>Apply <c>RejectNullStringConverter</c> in order to have serialization throw on <c>null</c> strings. Use <c>string option</c> to represent strings that can potentially be <c>null</c>.
0 commit comments