question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] In which cases JsonConvert.DeserializeObject<T>(string, JsonSerializerSettings) actually returns null?

See original GitHub issue

I’m working with .NET core 3.1, C# 8 and nullable reference types enabled.

From the class library I’m writing, I’m referencing the version 12.0.3 of Newtonsoft JSON.

I noticed that, by calling JsonConvert.DeserializeObject<T>(string, JsonSerializerSettings?), I can get a null reference (Visual Studio analyzers detect a possible dereferencing of a null reference).

Notice that I’m calling the overload which takes a string and an instance of JsonSerializerSettings. I’m only using the JsonSerializerSettings in order to handle the possible deserialization errors (via the Error property).

The github source code confirms that the overload I’m calling can possible return a null reference, via the MaybeNull attribute: take a look here for a confirmation.

My question is: in which cases newtonsoft JSON returns a null reference when deserializing a JSON string to a .NET type ?

Usually it returns an object of the given type populated or having its properties at the default value for their type, I have never encountered a case where null is returned instead.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lechu445commented, Mar 24, 2021

@EnricoMassone you are right, thank you for the answer.

In RFC 7159 there is such definition:

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

so, null, 1, "text", true are all valid JSONs.

1reaction
TylerBrinkleycommented, Jul 20, 2020

I think the single string parameter overload just got missed when the library was annotated.

I’d imagine with custom converters you could conceivably return null from a non "null" input as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In which cases JsonConvert.DeserializeObject<T> actually ...
My question is: in which cases newtonsoft JSON returns a null reference when deserializing a JSON string to a . NET type ?...
Read more >
DeserializeObject<Object> with empty string returns ...
In my case, I have a TypeConverter that takes the string and creates ... DeserializeObject<TestsEmptyString>(str); // returns null var ...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Newtonsoft.Json can serialize or deserialize numbers represented by JSON strings (surrounded by quotes). For example, it can accept: {" ...
Read more >
Jsonconvert.deserialize to class returns null
Try this: 1. Synchronize the property name in the class and in the JSON file. 2. Add [JsonProperty] attribute to the property in...
Read more >
Newton json ignore null. NET to specifically serialize DateTim
DeserializeObject to avoid the null values, as mentioned here and here: var convertedMessage = JsonConvert. When that happens, the key is not recognized...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found