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
Exception when deserializing quoted numbers with constructors using synchronous API #43587
Comments
I was able to repro this and I'm preparing a fix. |
The issue here is that the mechanism to honor the number handling of properties during synchronous deserialization, after object construction with parameters isn't properly set. This leads to a situation where custom number handling may not be honored, depending on the ordering of the JSON properties. This failure characteristic is hard to explain/predict and we should consider porting the fix in a servicing release. The workaround here for 5.0 is to set a custom converter for any failing properties as done in the description above. |
Reopening for servicing consideration in 5.0. |
I'm also having trouble getting System.Text.Json deserialize "true" and "false" into a bool field of an anonymous type, even using JsonNumberHandling.AllowReadingFromString (accepting "bool" is debatably a number). But I rely on this behavior that the Newstonsoft library implements due to the some of the 3rd party APIs we communicate with (that send back bools as |
Closing as fixed for 5.0 by #45452. |
@wizofaus - To support reading quoted booleans with System.Text.Json's serializer, you'll need to register a custom converter which has logic to parse them - https://dotnetfiddle.net/XBS4WZ. using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
JsonSerializerOptions options = new() { Converters = { new BooleanConverter() } };
// true/false literals
string json = "true";
bool val = JsonSerializer.Deserialize<bool>(json, options);
Console.WriteLine(val);
json = "false";
val = JsonSerializer.Deserialize<bool>(json, options);
Console.WriteLine(val);
// quoted true/false literals
json = @"""true""";
val = JsonSerializer.Deserialize<bool>(json, options);
Console.WriteLine(val);
json = @"""false""";
val = JsonSerializer.Deserialize<bool>(json, options);
Console.WriteLine(val);
// Invalid data.
json = @"""True""";
//json = "1";
val = JsonSerializer.Deserialize<bool>(json, options);
Console.WriteLine(val);
}
public class BooleanConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.True:
return true;
case JsonTokenType.False:
return false;
case JsonTokenType.String:
return reader.GetString() switch
{
"true" => true,
"false" => false,
_ => throw new JsonException()
};
default:
throw new JsonException();
}
}
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}
} |
Ah great, thank you @layomia , I figured it might need a custom converter but was only starting to get familiar with the library so assumed it might have built-in functionality. |
Description
The following error is getting thrown when I attempt to deserialize a JSON string with quoted numbers (though I specified that numbers can be read from strings in the json serializer options) into a class with a constructor:
Given the following JSON response from an API:
Where the "album.userplaycount" and the "album.wiki" properties can be omitted in the response.
I have created the following classes:
Note that no property has been created for the "album.wiki" deliberately.
Using the code:
no errors are thrown when the following JSON is deserialized
However, if I deserialize the JSON:
I get the error mentioned above.
No Errors are thrown if:
wiki
property is moved before theuserplaycount
property:Album
class is removed.userplaycount
property is omitted in the API response.Configuration
I am running this code on:
Other information
This issue is related to #30255.
I have created a small repo to demonstrate this issue here: https://github.com/nathanpovo/Issue-With-Quoted-Numbers
Workaround
No errors are thrown if the following converter is used with the
UserPlayCount
property in the album class.The text was updated successfully, but these errors were encountered: