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

Consistency of error message when deserializing null to value vs. reference type #87

Closed
cmeeren opened this issue Jan 29, 2021 · 0 comments

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Jan 29, 2021

Deserializing null to a non-option-wrapped type produces a different error message depending on whether the target type is a value or reference type:

let options = JsonSerializerOptions()
options.Converters.Add(JsonFSharpConverter())
let deserialize<'a> (str: string) = JsonSerializer.Deserialize<'a>(str, options)

type A = { A: int; B: string }

deserialize<A> """{ "A": 2, "B": null }"""
// System.Text.Json.JsonException: A.B was expected to be of type String, but was null.

deserialize<A> """{ "A": null, "B": "a" }"""
// System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $ | LineNumber: 0 | BytePositionInLine: 4. Path: $ | LineNumber: 0 | BytePositionInLine: 6. ---> System.InvalidOperationException: Cannot get the value of a token type 'Null' as a number.

Perhaps this could be made consistent? Something like:

System.Text.Json.JsonException: Null is not allowed here. <path, line number, etc.>

(Of course, the path/line number/position is wrong, as detailed in #37).

I don't care that much about this issue, I just noticed it and wanted to mention it.

@cmeeren cmeeren changed the title Consistency of error message when deserializing null to a value type Consistency of error message when deserializing null to value vs. reference type Jan 29, 2021
@Tarmil Tarmil closed this as completed in bef23df May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant