diff --git a/src/sitelets/WebSharper.Web/ClientSideJson.fs b/src/sitelets/WebSharper.Web/ClientSideJson.fs index ba943103d..722be2fd2 100644 --- a/src/sitelets/WebSharper.Web/ClientSideJson.fs +++ b/src/sitelets/WebSharper.Web/ClientSideJson.fs @@ -79,7 +79,7 @@ module Provider = let EncodeUnion (_: obj) (discr: string) (cases: (string * (string * string * (unit -> obj -> obj) * OptionalFieldKind)[])[]) : (unit -> 'T -> obj) = () fun () x -> - if JS.TypeOf x ===. JS.Object then + if JS.TypeOf x ===. JS.Object && x !=. null then let o = New [] let tag = x?("$") let tagName, fields = cases.[tag] diff --git a/tests/WebSharper.Sitelets.Tests/Json.fs b/tests/WebSharper.Sitelets.Tests/Json.fs index 8fe4e6a3a..c0e4bacf8 100644 --- a/tests/WebSharper.Sitelets.Tests/Json.fs +++ b/tests/WebSharper.Sitelets.Tests/Json.fs @@ -76,6 +76,7 @@ module Json = | [] Foo | [] Bar | [] Twelve + | [] Null type GenericUnion() = diff --git a/tests/WebSharper.Web.Tests/ClientSideJson.fs b/tests/WebSharper.Web.Tests/ClientSideJson.fs index 34c875c4c..03a67fbb5 100644 --- a/tests/WebSharper.Web.Tests/ClientSideJson.fs +++ b/tests/WebSharper.Web.Tests/ClientSideJson.fs @@ -286,12 +286,14 @@ module ClientSideJson = equal (Json.Serialize Foo |> Json.Parse |> unbox) "foo" equal (Json.Serialize Bar |> Json.Parse |> unbox) "bar" equal (Json.Serialize Twelve |> Json.Parse |> unbox) 12 + equal (Json.Serialize Null |> Json.Parse |> unbox) null } Test "deserialize union with constants" { equal (Json.Deserialize (Json.Stringify "foo")) Foo equal (Json.Deserialize (Json.Stringify "bar")) Bar equal (Json.Deserialize (Json.Stringify 12)) Twelve + equal (Json.Deserialize (Json.Stringify null)) Null } Test "serialize System.DateTime" { @@ -462,6 +464,7 @@ module ClientSideJson = equalAsync (f Foo) Foo equalAsync (f Bar) Bar equalAsync (f Twelve) Twelve + equalAsync (f Null) Null } let now = System.DateTime.Now