Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for deserialization of untyped null values #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions FSharp.Json.Tests/AsJson.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ module AsJson =
let actual = Json.deserialize<AsJsonOptionalRecord> """{"value":null}"""
let expected = { AsJsonOptionalRecord.value = None }
Assert.AreEqual(expected, actual)

[<Test>]
let ``AsJson member deserialization untyped - null`` () =
let config = JsonConfig.create (jsonFieldNaming = Json.lowerCamelCase, allowUntyped = true )
let actual = Json.deserializeEx<obj list> config """[ {"value":null} ]"""
let expected = [ Map.ofList [ "value", None ] ]
Assert.AreEqual(expected[0], actual[0])
4 changes: 3 additions & 1 deletion FSharp.Json/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ module internal Core =
| JsonValue.Record _ -> typeof<Map<string, obj>>
| JsonValue.Array _ -> getListType typeof<obj>
| JsonValue.Boolean _ -> typeof<bool>
| _ -> null
| _ -> typeof<unit>

let rec deserialize (config: JsonConfig) (path: JsonPath) (t: Type) (jvalue: JsonValue): obj =
let deserializeEnum (path: JsonPath) (t: Type) (jsonField: JsonField) (jvalue: JsonValue): obj =
Expand Down Expand Up @@ -368,6 +368,8 @@ module internal Core =
JsonValueHelpers.getDateTimeOffset CultureInfo.InvariantCulture path jvalue :> obj
| t when t = typeof<Guid> ->
JsonValueHelpers.getGuid path jvalue :> obj
| t when t = typeof<unit> ->
None :> obj
| t when t.IsEnum ->
deserializeEnum path t jsonField jvalue
| t when isTuple t || isList t || isArray t || isMap t || isRecord t || isUnion t ->
Expand Down