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

Update doc for exceptions thrown during serialization #33400

Merged
merged 2 commits into from
Mar 13, 2020

Conversation

steveharter
Copy link
Member

The doc for the various serialization APIs was not complete or correct.

@steveharter steveharter added documentation Documentation bug or enhancement, does not impact product or test code area-System.Text.Json labels Mar 9, 2020
@steveharter steveharter self-assigned this Mar 9, 2020
Copy link

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@layomia layomia added this to the 5.0 milestone Mar 10, 2020
@@ -21,6 +21,10 @@ public static partial class JsonSerializer
/// <typeparamref name="TValue"/> is not compatible with the JSON,
/// or when there is remaining data in the Stream.
/// </exception>
/// <exception cref="NotSupportedException">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure to update these manually in https://github.com/dotnet/dotnet-api-docs as well.

cc @carlossanlop

@@ -21,6 +21,10 @@ public static partial class JsonSerializer
/// <typeparamref name="TValue"/> is not compatible with the JSON,
/// or when there is remaining data in the Stream.
/// </exception>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only case when we would throw an NSE for this API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe so. What else were you thinking?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing specific came to mind, but I thought we might have other checks around types we don't support/etc. or if a type has NSE characteristics (internal ctors, non-default ctors, fields, private setters, etc.), we'd throw as well. Maybe audit all places we throw NSE and verify they all fall under the "JsonConverter doesn't exist" camp.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about ctor checks -- I do think however that it still means the "converter is not compatible" since you could in theory have a converter that would support non-default ctors and not throw NSE. i.e. the current "built-in" converter doesn't support non-default ctors.

I plan on leaving as-is except perhaps append the "other serializable members" per above

/// </exception>
/// <exception cref="JsonException">
/// Thrown when the JSON is invalid,
/// <paramref name="returnType"/> is not compatible with the JSON,
/// or when there is remaining data in the Stream.
/// </exception>
/// <exception cref="NotSupportedException">
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="returnType"/>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd also throw NSE if any of the types within the object graph of returnType don't have a JsonConverter, not just returnType.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I considered something like "There is no compatible JsonConverter for {0} or any of its properties" but that extra verbage I thought was implied. A converter includes anything it does including delegating to other converters on its properties, which also have converters, etc.

Also we don't explain that any exception is possible since we may call user-provided converters, but from past discussions didn't attempt to explain that either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that extra verbage I thought was implied.

The fact that we state for returnType, makes it ambiguous. I think being explicit could be useful. Up to you :)

Also we don't explain that any exception is possible since we may call user-provided converters, but from past discussions didn't attempt to explain that either.

Agreed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better? I'm no writer :P

"There is no compatible JsonConverter for {0} or its serializable members."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@@ -105,6 +109,10 @@ public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializ
/// <exception cref="ArgumentException">
/// <paramref name="reader"/> is using unsupported options.
/// </exception>
/// <exception cref="NotSupportedException">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// <exception cref="InvalidOperationException">
/// The configured <see cref="JsonConverter"/> for <paramref name="typeToConvert"/> returned an invalid converter.
/// </exception>
/// <exception cref="NotSupportedException">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do the checks happen that result in NSE here?

Copy link
Member Author

@steveharter steveharter Mar 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a converter is not yet cached, it is created from this method and currently the collection-based converters will throw NSE for not supported collections like multidimensional array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he collection-based converters will throw NSE for not supported collections like multidimensional array.

What about the non-collection-based converters/other converters? Is it ever possible that a converter couldn't be created for a particular type (one that we don't support today)?

For example, what is the exception that we throw if someone happen to have a BigInteger property in their model? Presumably it would be this documented NSE. Is that what happens?

@steveharter steveharter merged commit 5757764 into dotnet:master Mar 13, 2020
@steveharter steveharter deleted the UpdateDocsForExceptions branch March 13, 2020 18:57
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants