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

Exception thrown when deserializing null value even though IgnoreNullValues = true #44125

Closed
FStapenhorst opened this issue Nov 1, 2020 · 2 comments

Comments

@FStapenhorst
Copy link

Description

Trying to deserialize a class having a bool property. I am getting an exception:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (The JSON value could not be converted to System.Boolean. Path: $.results[0].taskSnap | LineNumber: 0 | BytePositionInLine: 598.)
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at UnitTests.General.General.GetActivities() in E:\Entwicklung\matterialcore-client-library\UnitTests\General\General.cs:line 140

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
JsonException: The JSON value could not be converted to System.Boolean. Path: $.results[0].taskSnap | LineNumber: 0 | BytePositionInLine: 598.

Inner Exception 2:
InvalidOperationException: Cannot get the value of a token type 'Null' as a boolean.

This happens even though the deserializer options are set to IgnoreNullValues = true.

Configuration

Windows 10 64 bit
Visual Studio 19 16.8.0 Preview 6
System.Text.Json version 5.0.0-rc.2.20475.5
Project Type: Library .Net 5.0

Class

public class Activity
{
   public Activity()
    {}

    [JsonPropertyName("id"), JsonInclude]
    public long Id { get; internal set; }

    [JsonPropertyName("taskSnap"), JsonInclude]
    public bool TaskSnap { get; internal set; }
}

JSon String to deserialize

{
    "id": 14114,
     "taskSnap": null			
}

Deserialization:

var x = JsonSerializer.Deserialize<Activity>(jsonString, new JsonSerializerOptions { IgnoreNullValues = true } );

Other information

If I change the property as follows, it works:

    [JsonPropertyName("taskSnap"), JsonInclude]
    public bool? TaskSnap { get; internal set; }
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Nov 1, 2020
@FStapenhorst
Copy link
Author

Please find attached example project.
The IgnoreNullValues serialization option seems to work with strings but not with bool.
Might by by design?

JSonTests.zip

@layomia
Copy link
Contributor

layomia commented Nov 2, 2020

This is a duplicate of #30795. Please see that issue for more info. Also see documentation about a workaround - https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#deserialize-null-to-non-nullable-type.

Note that this is not a regression from .NET 3.x.

@layomia layomia closed this as completed Nov 2, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Nov 2, 2020
@layomia layomia added this to the 6.0.0 milestone Nov 2, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants