Skip to content
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])
6 changes: 4 additions & 2 deletions FSharp.Json/Core.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
let enumMode = getEnumMode config jsonField
match enumMode with
| EnumMode.Value ->
match baseT with

Check warning on line 81 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Incomplete pattern matches on this expression.

Check warning on line 81 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Incomplete pattern matches on this expression.
| t when t = typeof<int> ->
let enumValue = decimal (value :?> int)
JsonValue.Number enumValue
Expand Down Expand Up @@ -291,7 +291,7 @@
| 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 All @@ -299,7 +299,7 @@
let enumMode = getEnumMode config jsonField
match enumMode with
| EnumMode.Value ->
match baseT with

Check warning on line 302 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Incomplete pattern matches on this expression.

Check warning on line 302 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Incomplete pattern matches on this expression.
| baseT when baseT = typeof<int> ->
let enumValue = JsonValueHelpers.getInt path jValue
Enum.ToObject(t, enumValue)
Expand Down Expand Up @@ -371,7 +371,9 @@
| t when t = typeof<TimeSpan> ->
JsonValueHelpers.getTimeSpan path jValue :> obj
| t when t = typeof<Guid> ->
JsonValueHelpers.getGuid path jValue :> obj
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 Expand Up @@ -522,7 +524,7 @@
| JsonValue.String caseName ->
FSharpValue.MakeUnion (caseName |> getUnionCaseInfo path t, null)
| JsonValue.Record fields ->
match jsonUnion.Mode with

Check warning on line 527 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Enums may take values outside known cases. For example, the value 'enum<UnionMode> (3)' may indicate a case not covered by the pattern(s).

Check warning on line 527 in FSharp.Json/Core.fs

View workflow job for this annotation

GitHub Actions / build

Enums may take values outside known cases. For example, the value 'enum<UnionMode> (3)' may indicate a case not covered by the pattern(s).
| UnionMode.CaseKeyDiscriminatorField ->
let caseKeyFieldName, caseKeyFieldValue = mustFindField path jsonUnion.CaseKeyField fields
let caseNamePath = caseKeyFieldName |> JsonPathItem.Field |> path.createNew
Expand Down
Loading