4

However I try I get the following warning.

Severity Code Description Project File Line Suppression State Warning CS8600 Converting null literal or possible null value to non-nullable type.

The code is as follows.

HttpResponseMessage response = await _httpClient.PutAsync(url, requestContent);
string? userResponse = await response.Content.ReadAsStringAsync();
JsonSerializerOptions? options = new JsonSerializerOptions
{
  PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

//if (userResponse.Length > 0)
//{
  user = JsonSerializer.Deserialize<GetUserById>(userResponse, options);
//}

Looked at some posts like this and this, but could not figure out how to resolve.

Update.

I removed the if condition altogether, but still getting the warning.

Update 2

Added the null check as suggested. But still getting that warning.

Waring in C#

VivekDev
  • 20,868
  • 27
  • 132
  • 202

4 Answers4

9

The JsonSerializer.Deserialize returns nullable GetUserById. You are assigning the nullable value to a nonnullable field/property.

You have two options:

nestor10
  • 474
  • 4
  • 12
4

This can be solved by throwing an exception for null values. By setting the type of user as GetUserById (not GetUserById?), you are stating that it cannot be null, so throwing an exception at the earliest possible point will be the correct choice.

GetUserById user =
    JsonSerializer.Deserialize<GetUserById>(userResponse, options) ??
    throw new InvalidOperationException();

Note that JsonSerializer.Deserialize returns null in a special case when the string equals "null". See https://stackoverflow.com/a/72279451.

You can also use null-forgiving operator starting with C# 8. This is generally not recommended as it makes the program to fail at a later point if the value happens be null.

GetUserById user = JsonSerializer.Deserialize<GetUserById>(userResponse, options)!;
Jatin Sanghvi
  • 1,928
  • 1
  • 22
  • 33
  • Imo it's a design flaw that Deserialize even returns a nullable reference though, as that method throws exceptions in any case of parsing errors anyway instead of returning null. – Vinz Feb 02 '23 at 22:50
1

I believe it is warning you that userResponse is null and that it needs to be checked for a null state. If you do a null check that warning should go away.

Do this for you do the userResponse.Length Try this:

HttpResponseMessage response = await _httpClient.PutAsync(url, requestContent);
var userResponse = (string?)null;
userResponse = await response.Content.ReadAsStringAsync();
JsonSerializerOptions? options = new JsonSerializerOptions
{
  PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

if (userResponse != null)
{
  user = JsonSerializer.Deserialize<GetUserById>(userResponse, options);
}
mathis1337
  • 1,426
  • 8
  • 13
1

JsonConvert.DeserializeObject can return null so the receiving variable should be nullable eg:

if (userResponse != null)
{
  var user? = JsonSerializer.Deserialize<GetUserById>(userResponse, options);
  if (user != null)
  {
    // Do stuff with non null user here...
  }
}

The simplest way for me to check for the null returned object in one go is just:

if (JsonSerializer.Deserialize<GetUserById>(userResponse, options) is GetUserById user)
{
  // Do stuff with non null user here...
}
Christo Carstens
  • 741
  • 7
  • 14