-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/FsCodec.NewtonsoftJson.Tests/SomeNullHandlingTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module FsCodec.NewtonsoftJson.Tests.SomeNullHandlingTests | ||
|
||
open FsCodec.NewtonsoftJson | ||
open Swensen.Unquote | ||
open Xunit | ||
|
||
let def = Settings.CreateDefault() | ||
|
||
let [<Fact>] ``Settings.CreateDefault roundtrips null string option, but rendering is ugly`` () = | ||
let value : string option = Some null | ||
let ser = Serdes.Serialize(value, def) | ||
test <@ ser = "{\"Case\":\"Some\",\"Fields\":[null]}" @> | ||
test <@ value = Serdes.Deserialize(ser, def) @> | ||
|
||
let [<Fact>] ``Settings.Create does not roundtrip Some null`` () = | ||
let value : string option = Some null | ||
let ser = Serdes.Serialize value | ||
"null" =! ser | ||
// But it doesn't roundtrip | ||
value <>! Serdes.Deserialize ser | ||
|
||
let hasSomeNull value = TypeShape.Generic.exists(fun (x : string option) -> x = Some null) value | ||
let replaceSomeNullsWithNone value = TypeShape.Generic.map (function Some (null : string) -> None | x -> x) value | ||
|
||
let [<Fact>] ``Workaround is to detect and/or substitute such non-roundtrippable values`` () = | ||
|
||
let value : string option = Some null | ||
// So we detect the condition (we could e.g. exclude such cases from the tests) | ||
test <@ hasSomeNull value @> | ||
// Or we can plough on, replacing the input with a roundtrippable value | ||
let value : string option = replaceSomeNullsWithNone value | ||
None =! value | ||
test <@ (not << hasSomeNull) value @> | ||
let ser = Serdes.Serialize value | ||
ser =! "null" | ||
// ... and validate that the [substituted] value did roundtrip | ||
test <@ value = Serdes.Deserialize ser @> | ||
|
||
type RecordWithStringOptions = { x : int; y : Nested } | ||
and Nested = { z : string option } | ||
|
||
let [<Fact>] ``Can detect and/or substitute null string option when using Settings.Create`` () = | ||
let value : RecordWithStringOptions = { x = 9; y = { z = Some null } } | ||
test <@ hasSomeNull value @> | ||
let value = replaceSomeNullsWithNone value | ||
test <@ (not << hasSomeNull) value @> | ||
let ser = Serdes.Serialize value | ||
ser =! """{"x":9,"y":{"z":null}}""" | ||
test <@ value = Serdes.Deserialize ser @> | ||
|
||
// As one might expect, the ignoreNulls setting is also honored | ||
let ignoreNullsSettings = Settings.Create(ignoreNulls=true) | ||
let ser = Serdes.Serialize(value,ignoreNullsSettings) | ||
ser =! """{"x":9,"y":{}}""" | ||
test <@ value = Serdes.Deserialize ser @> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters