-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"/>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe so. What else were you thinking? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
[return: MaybeNull] | ||
public static TValue Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions? options = null) | ||
{ | ||
|
@@ -43,13 +47,17 @@ public static TValue Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerial | |
/// <param name="returnType">The type of the object to convert to and return.</param> | ||
/// <param name="options">Options to control the behavior during parsing.</param> | ||
/// <exception cref="System.ArgumentNullException"> | ||
/// Thrown if <paramref name="returnType"/> is null. | ||
/// <paramref name="returnType"/> is null. | ||
/// </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"/>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The fact that we state
Agreed. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
/// </exception> | ||
public static object? Deserialize(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions? options = null) | ||
{ | ||
if (returnType == null) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,10 @@ internal static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerial | |
/// <exception cref="ArgumentException"> | ||
/// <paramref name="reader"/> is using unsupported options. | ||
/// </exception> | ||
/// <exception cref="NotSupportedException"> | ||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <typeparamref name="TValue"/>. | ||
/// </exception> | ||
/// <remarks> | ||
/// <para> | ||
/// If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/> | ||
|
@@ -95,7 +99,7 @@ public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializ | |
/// <param name="returnType">The type of the object to convert to and return.</param> | ||
/// <param name="options">Options to control the serializer behavior during reading.</param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// Thrown if <paramref name="returnType"/> is null. | ||
/// <paramref name="returnType"/> is null. | ||
/// </exception> | ||
/// <exception cref="JsonException"> | ||
/// Thrown when the JSON is invalid, | ||
|
@@ -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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reminder: Port these to api docs so they become visible on the docs site: |
||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <paramref name="returnType"/>. | ||
/// </exception> | ||
/// <remarks> | ||
/// <para> | ||
/// If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,13 @@ internal JsonConverter DetermineConverter(Type? parentClassType, Type runtimePro | |
/// <returns> | ||
/// The converter for the given type. | ||
/// </returns> | ||
/// <exception cref="InvalidOperationException"> | ||
/// The configured <see cref="JsonConverter"/> for <paramref name="typeToConvert"/> returned an invalid converter. | ||
/// </exception> | ||
/// <exception cref="NotSupportedException"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do the checks happen that result in NSE here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 |
||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <paramref name="typeToConvert"/>. | ||
/// </exception> | ||
public JsonConverter GetConverter(Type typeToConvert) | ||
{ | ||
if (_converters.TryGetValue(typeToConvert, out JsonConverter? converter)) | ||
|
There was a problem hiding this comment.
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